NotionCommotion Posted April 7, 2017 Share Posted April 7, 2017 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¶2=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. Quote Link to comment Share on other sites More sharing options...
Solution Jacques1 Posted April 7, 2017 Solution Share Posted April 7, 2017 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. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted April 7, 2017 Author Share Posted April 7, 2017 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¶m2=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 Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted April 7, 2017 Share Posted April 7, 2017 I'm not sure what kind of documentation you expect. cURL accepts arrays only for the sake of convenience, so that the programmer doesn't have to do the encoding herself. That's it. There is no special array processing. Quote Link to comment Share on other sites More sharing options...
requinix Posted April 7, 2017 Share Posted April 7, 2017 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. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted April 7, 2017 Author Share Posted April 7, 2017 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted April 7, 2017 Share Posted April 7, 2017 Yes, I translated what's in ext/curl/interface.c (search for CURLOPT_POSTFIELDS). Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted April 7, 2017 Author Share Posted April 7, 2017 I've never dove into the source code before, but I can now see where it might sometimes be helpful. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.