橫幅廣告

2013年11月19日 星期二

Don't assign an array type value to URLOPT_POSTFIELDS in curl

If you are not planing to upload a file, don't to assign an array type value to URLOPT_POSTFIELDS in curl.

Today, I got a problem about using curl to do a login action.
Something happened. The system didn't work because the first character in the password was a @ character.

See the reference: http://php.net/manual/en/function.curl-setopt.php , and please find the statement of CURLOPT_POSTFIELDS section.

You will find a statement as following:
If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, value must 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.

So, you/me should use String type rather than Array type assigning values to URLOPT_POSTFIELDS  if you aren't planing to upload a file.

The solution can be found in this web page: http://www.brandonchecketts.com/archives/array-versus-string-in-curlopt_postfields

Hope it also help you.