最近遇到一个很奇怪的问题
在发送post请求时,如果POSTFIELDS的类型为string的话,就会超时,但类型为array就没问题。$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
// 数据为string类型时,超时;
// 如果是array('param' => $data_string)就没问题。
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_exec($ch);
curl_close($ch);

解决方案 »

  1.   

    传递一个数组到CURLOPT_POSTFIELDS,cURL会把数据编码成 multipart/form-data,而然传递一个URL-encoded字符串时,数据会被编码成 application/x-www-form-urlencoded。
    这是它们唯一的区别,超时是网络因素我觉得,和代码无关
    另外你的TIMEOUT调的的确有点小了,超时就像家常便饭
      

  2.   


    都是本地的请求,而且被请求的页面只是print_r($_SERVER);如果数据是array的话,一点问题也没有,改为string则必超;时间设为30s都会超时。
      

  3.   

    比如'hello'。
    另外,如果不显示的设置为POST请求(删掉curl_setopt($ch, CURLOPT_POST, true);语句),可以成功发送,但请求类型会变为GET;array的话则正常,为POST请求。
      

  4.   

    网上找些列子看看,是不是用法不对。如果都正确,只能说明是其它原因导致的http://hi.baidu.com/chenxiandong1988/blog/item/56f3b0282bd0fde6e7cd4087.html
      

  5.   

    你的字符串怎么写的啊,这个有格式要求的吧,比如uname=aaa&pwd=bbbb
      

  6.   

    我用你的代码在本地试了一下,并没有发现问题。
    $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;
    getdata.php里面print_r($_REQUEST);
      

  7.   

    重新编译安装了一遍curl和php,没有这个问题了。
    谢谢各位!