Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use arrays in cURL POST requests

Tags:

post

php

curl

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[]?

like image 829
Goulash Avatar asked Nov 28 '12 02:11

Goulash


People also ask

How do I post an array in curl?

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);

How to pass data in php curl?

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.

How is curl implemented in laravel?

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.


1 Answers

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); ?> 
like image 111
Benjamin Powers Avatar answered Sep 28 '22 07:09

Benjamin Powers