【发布时间】:2016-04-29 01:05:07
【问题描述】:
PHP 的 curl 令人惊讶地无法调试且晦涩难懂。我在使用 cURL 下载 JSON API 数据时遇到问题。我想看看 cURL 到底是什么发送到远程 HTTP 服务器。
目前我唯一的调试选项是临时将请求发送到一些将输入写入标准输出的简单 HTTP 服务器。我需要编写该服务器来调试 curl!
我做什么:
function get_data($url) {
$ch = curl_init();
echo "Download: $url.\n";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// I hoped to get some debug info
// but this setting has no effect
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HEADER, array(
'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0',
'X-Purpose: Counting downloads.'
));
echo "Sending: \n".curl_getinfo($ch, CURLINFO_HEADER_OUT);
$data = curl_exec($ch);
var_dump($data);
echo curl_error($ch)." ".curl_errno($ch);
curl_close($ch);
return $data;
}
如何获取 cURL 作为文本发送的数据?
【问题讨论】: