Usage of curlpost data

Source: Internet
Author: User
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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.