-2

I have this curl code i need to convert it to php

curl -H "Content-Type: application/json" -d '{"command":"sendoffer", 
"steamID":"###############","token":"lkTR4VG2", "itemIDsToRequest":["4942877123","4892501549"],
"message": "Message"}' http://website:1337/

As you can see there is an array along with normal json.

"itemIDsToRequest":["4942877123","4892501549"]

I looked at many question like this and this, but couldn't understand how to implement it.

Im sorry im very new to curl command.

2 Answers 2

2

the array is part of a JSON string that is not interpreted but used as plain string data in CURL so it does not really matter what is in there; use the very same JSON string as from your commandline example, so:

$data = '{"command":"sendoffer", "steamID":"###############","token":"lkTR4VG2", "itemIDsToRequest":["4942877123","4892501549"], "message": "Message"}'
curl_setopt($ch, CURLOPT_POSTFIELDS, $data );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
Sign up to request clarification or add additional context in comments.

4 Comments

Yes he's right. No need to convert to a php array :D Why did i do it ?!
I answered the question strictly, but I'm sure the json_encode call is something that would part of the answer to his next question ;-)
@HansZandbelt im creating array dynamically .. so instead of creating array should i just create a string in that format
well if you have to assemble data dynamically, direct string manipulation is often more painful and error prone than managing an array and finally calling json_encode when you're ready to send it; just consider the string manipulation that you would have to do if you wanted to add a single item to your existing $data as opposed to adding it to an array
0

If your array is

$data = array("command"=>"sendoffer", "steamID"=>"###############","token"=>"lkTR4VG2", "itemIDsToRequest"=>array("4942877123","4892501549"),"message"=>"Message");

Then send it on its way as json format with curl like that :

$url = 'http://website:1337/';
$ch = curl_init( $url );

// Convert array to json string
$data_json = json_encode( $data );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_json );

// Indicate in the header that it is json data
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); 

// Send request
$result = curl_exec($ch);
curl_close($ch);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.