The use of curlpost data has recently encountered a very strange problem. when sending a post request, if the POSTFIELDS type is string, it will time out, but the type is array. PHPcode $ ch = curl_init (); curl_setopt ($ Use curl post data problem
A very strange problem recently
When a post request is sent, if the POSTFIELDS type is string, it will time out, but if the type is array, no problem.
PHP code
$ Ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_POST, true); // timeout when the data type is string; // if it is array ('param' => $ data_string), no problem occurs. Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data_string); curl_setopt ($ ch, CURLOPT_TIMEOUT, 3); curl_exec ($ ch); curl_close ($ ch );
------ Solution --------------------
When an array is passed to CURLOPT_POSTFIELDS, cURL encodes the data into multipart/form-data, and then transmits a URL-encoded string, the data is encoded into application/x-www-form-urlencoded.
This is the only difference between them. timeout is a network factor. I think it is irrelevant to the code.
In addition, your TIMEOUT adjustment is indeed a little small, and TIMEOUT is just as common
------ Solution --------------------
So what is your string?
------ Solution --------------------
Find some columns on the internet to see if the usage is incorrect. If they are all correct, it can only be explained for other reasons.
Http://hi.baidu.com/chenxiandong1988/blog/item/56f3b0282bd0fde6e7cd4087.html
------ Solution --------------------
I tried your code locally and did not find any problems.
PHP code
$data_string = "ispost=ok&msg=post";$data = array("ispost"=>"ok","msg"=>"post");$ch = curl_init();curl_setopt($ch, CURLOPT_URL, "http://localhost/my/www/getdata.php");curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);curl_setopt($ch, CURLOPT_TIMEOUT, 30);$result = curl_exec($ch);curl_close($ch);echo $result;