I am wondering how do I make this code support arrays? At the moment the images
array only seems to send the first value.
Here is my code:
<?php //extract data from the post extract($_POST); //set POST variables $url = 'http://api.example.com/api'; $fields = array( 'username' => "annonymous", 'api_key' => urlencode("1234"), 'images[]' => urlencode(base64_encode('image1')), 'images[]' => urlencode(base64_encode('image2')) ); //url-ify the data for the POST foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } rtrim($fields_string, '&'); //open connection $ch = curl_init(); //set the url, number of POST vars, POST data curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_POST, count($fields)); curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); //execute post $result = curl_exec($ch); echo $result; //close connection curl_close($ch); ?>
and this is what is received at the api
VAR: username = annonymous VAR: api_key = 1234 VAR: images = Array array(3) { ["username"]=> string(10) "annonymous" ["api_key"]=> string(4) "1234" ["images"]=> array(1) { // this should contain 2 strings :( what is happening? [0]=> string(8) "aW1hZ2Uy" } }
What is happening to the second value in images[]
?
You could use http_build_query: $fields = array( 'username' => "annonymous", 'api_key' => urlencode("1234"), 'images' => array( urlencode(base64_encode('image1')), urlencode(base64_encode('image2')) ) ); $fields_string = http_build_query($fields);
curl -u is equivalent to CURLOPT_USERPWD in php-curl. You set it with curl_setopt, as the others. --data is CURLOPT_POSTFIELDS , which you are already sending. But in your case you'd want to populate with the json you want to send.
We send a get request to the example.com url and set a timeout for this request. From the code, we also specify the content type, which is application/ json . In the final stage, we send the request using the curl_exec() method. We also store any possible errors using the curl_error() method.
You are just creating your array incorrectly. You could use http_build_query:
$fields = array( 'username' => "annonymous", 'api_key' => urlencode("1234"), 'images' => array( urlencode(base64_encode('image1')), urlencode(base64_encode('image2')) ) ); $fields_string = http_build_query($fields);
So, the entire code that you could use would be:
<?php //extract data from the post extract($_POST); //set POST variables $url = 'http://api.example.com/api'; $fields = array( 'username' => "annonymous", 'api_key' => urlencode("1234"), 'images' => array( urlencode(base64_encode('image1')), urlencode(base64_encode('image2')) ) ); //url-ify the data for the POST $fields_string = http_build_query($fields); //open connection $ch = curl_init(); //set the url, number of POST vars, POST data curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_POST, 1); curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); //execute post $result = curl_exec($ch); echo $result; //close connection curl_close($ch); ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With