How do I use arrays in cURL POST requests
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);
?>
How to pass curl post request where there is an array in data?
The data payload is just JSON? Wouldn't you simply include the array as an element of the data object?
curl --digest --user abc -H 'Content-Type: application/json' --request POST -d ' {"url":"www.okta.com", "groups": ["g1", "g2"]}' url
How to post array within array with curl
Neither of form submission formats support nested (multi-dimensional) arrays. In other words you cannot send nested arrays in POST request as form-encoded data (CURL uses application/x-www-form-urlencoded
format by default). Most likely you've misinterpreted API specs. Perhaps the API accept data in other format, for instance, JSON which allows any level of nesting.
Passing form array data in CURL
CURLOPT_POSTFIELDS => array(
'first_name' => $_POST['user']['first_name'],
'last_name' => $_POST['user']['last_name'],
'conditions[]' => '1',
'conditions[]' => '2',
'conditions[]' => '3'
),
That conditions[]
“syntax” works for form field names, PHP will then automatically create an array out of those parameters.
It does not work in code - you have just overwritten the key conditions[]
three times here, so of course only the last value survives.
You simply want something like 'conditions[]' => ['1', '2', '3']
in that place.
Sending a multidimensional array using cURL and foreach in php
Thanks to This awesome reply I understood I must use $POST
only.
sender:
<?php
function post_to_url($url, $data) {
$post = curl_init();
curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($post, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($post);
curl_close($post);
}
if (isset($_POST['submit'])){
$name = addslashes(strip_tags($_POST["name"]));
$address = addslashes(strip_tags($_POST["address"]));
$email = addslashes(strip_tags($_POST["email"]));
$job = addslashes(strip_tags($_POST["job"]));
$list = array (
array('name', "$name"),
array('address', "$address"),
array('email', "$email"),
array('job', "$job"),
array(''),
array('options', 'status', 'link','time', 'note','check' )
);
// Merge the 2 arrays together
$final_array = array_merge($list, $_POST['prefs']);
// Send through CURL
post_to_url("http://192.168.10.10/receiver.php", $final_array);
header("Location:index.php");
exit;
}
?>
receiver:
<?php
$csv = fopen( "/home/user/file.csv", 'w+' );
foreach ($_POST as $fields) {
fputcsv($csv, $fields, ";");
}
fclose($csv);
?>
And now it works.
how do i POST an empty array with curl in c?
Empty arrays don't really exist in URL-encoded parameters. When you send an array, it's sent as:
name[]=firstElement&name[]=secondElement&name[]=thirdElement
An empty array means you don't send any of these, but then there's no parameter at all.
It's the responsibility of the server code to handle the nonexisting parameter and treat it as an empty array.
When you write
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "emptyArr[]");
it's being treated as
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "emptyArr[]=");
so you're creating an array with one element whose value is an empty string.
You should simply leave out the parameter entirely, and the server should treat it as empty.
How can I post array field data via cURL?
You are doing concatenation over the array inside the foreach
loop.
Your Array:
'fruit' => array($fruit)
And when doing a concatenation like below, you are getting Array
as plain text inside the $value
:
$fields_string .= $key.'='.$value.'&';
So ultimately you are posting: first_name=Joe&last_name=Jones&fruit=Array
My suggestion is to use http_build_query()
and remove your manual post data building.
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($fields));
Related Topics
Passing an Array as Arguments, Not an Array, in PHP
Submit an HTML Form With Empty Checkboxes
I Cannot Get My Login Form to Connect Interact Properly With MySQL Database
MySQL - Ignore Insert Error: Duplicate Entry
Any Way to Return PHP 'Json_Encode' With Encode Utf-8 and Not Unicode
PHP Get All the Images from Url Which Width and Height ≫=200 More Quicker
Warning: Cannot Modify Header Information - Headers Already Sent by Error
Get First 100 Characters from String, Respecting Full Words
PHP X86 How to Get Filesize of ≫ 2 Gb File Without External Program
Laravel Migration: Unique Key Is Too Long, Even If Specified
How to Get the Current Page'S Full Url on a Windows/Iis Server
How to Change Envelope from Address Using PHP Mail
PHP Expression ≪≪≪Eob
How to Get Rid of Eval-Base64_Decode Like PHP Virus Files