Jump to content

Passing multi-dimensional array to curl


NotionCommotion
Go to solution Solved by Jacques1,

Recommended Posts

The second curl_setopt_array() results in an error.  Why?  Where is it documented?  Thanks

 

<?php

$options=[CURLOPT_POST=>1, CURLOPT_POSTFIELDS=>['data'=>123]];
$ch      = curl_init();
curl_setopt_array( $ch, $options );

$options=[CURLOPT_POST=>1, CURLOPT_POSTFIELDS=>['data'=>[123]]];
$ch      = curl_init();
curl_setopt_array( $ch, $options );

$options=[CURLOPT_POST=>1, CURLOPT_POSTFIELDS=>['data'=>http_build_query([123])]];
$ch      = curl_init();
curl_setopt_array( $ch, $options );
Notice: Array to string conversion in /var/www/public/CURLOPT_POSTFIELDS.php on line 9
http://php.net/manual/en/function.curl-setopt.php
CURLOPT_POSTFIELDS The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'. This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, theContent-Type header will be set to multipart/form-data. As of PHP 5.2.0, valuemust be an array if files are passed to this option with the @ prefix. As of PHP 5.5.0, the @ prefix is deprecated and files can be sent using CURLFile. The @ prefix can be disabled for safe passing of values beginning with @ by setting theCURLOPT_SAFE_UPLOAD option to TRUE.
 
Passing an array to CURLOPT_POSTFIELDS will encode the data as multipart/form-data, while passing a URL-encoded string will encode the data as application/x-www-form-urlencoded.

 

 

 

Link to comment
Share on other sites

  • Solution

There's no such thing as an URL parameter array or even multidimensional array. URL parameters are simple key-value pairs. Anything build on top of that (like the param[] syntax in PHP) is nonstandard and must be assembled manually for the target application.

Link to comment
Share on other sites

Okay, so I can only pass simple key-value pairs.
 
And when I pass it ['param1'=>'value1, 'param2'=>'value2'], is PHP behind the scenes converting it to --data "param1=value1&param2=value2"
 
But when I try to pass it ['param1'=>['someArray'] ], PHP convert ['someArray'] to a string resulting in the Array to string conversion notice?
 

 

 

Is this just common knowledge or is it documented?  I guess the following kind of describes it:  or as an array with the field name as key and field data as value

Link to comment
Share on other sites

Here's what cURL (the PHP side of it) does with CURLOPT_POSTFIELDS:

if (is_array($value) || is_object($value)) {
	$postfields = (is_array($value) ? $value : get_object_vars($value));
	foreach ($postfields as $key => $current) {
		$key = (string)$key;
		if (is_object($current) && $current instanceof CURLFile) {
			// stuff...
		}

		$postval = (string)$current;
		// <libcurl function to append $key=$postval to a request>
	}
	// <libcurl function to set request>
} else {
	$str = (string)$value;
	// <libcurl function to set post data to $str>
}
Note

$postval = (string)$current;
to convert the array item to a string, which causes the error you were getting.

 

 

Personally, I always set the POSTFIELDS using a string - turning an array into a string is just one http_build_query() call away.

Link to comment
Share on other sites

Thanks requinix,

 

I tend to agree with you that I should always set POSTFIELDS with a string, and will start doing so in the future.

 

The PHP script you displayed doesn't really exist, does it?  Would this functionality actually be located at https://github.com/php/php-src/tree/master/ext/curl?  I looked, but as I don't know C, couldn't find it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.