如何从CURL捕获响应

时间:2014-05-11 22:00:04

标签: php curl

我使用下面这段代码通过网址将数据发送到另一台服务器,并且运行成功。我想从服务器捕获响应并处理它,但我似乎无法捕获它。

CODE

$url="http://www.example.com/com_spc/api.php?username=".urlencode($uname)."&password=".urlencode($pwd);

  $ch = curl_init(); // create cURL handle (ch)
if (!$ch) {
   die("Couldn't initialize a cURL handle");
 }
     // set some cURL options
     $ret = curl_setopt($ch, CURLOPT_URL, $url);
     $ret = curl_setopt($ch, CURLOPT_HEADER,         0);
     $ret = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
     $ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
     $ret = curl_setopt($ch, CURLOPT_TIMEOUT,        30);

     // execute
     $ret = curl_exec($ch);

    if (empty($ret)) {
         // some kind of an error happened
    die(curl_error($ch));
        curl_close($ch); // close cURL handler
    } else {
        $info = curl_getinfo($ch);
       curl_close($ch); // close cURL handler

       if (empty($info['http_code'])) {
            die("No HTTP code was returned"); 
       } else {


        }

   }                    

1 个答案:

答案 0 :(得分:2)

确保将CURLOPT_RETURNTRANSFER设置为1.否则curl_exec将不会返回任何内容