我在PHP中使用cURL来运行一个可能需要一个多小时才能运行的脚本。出于调试目的,我希望能够通过查看屏幕(这不是公共站点)查看请求的进度,看看发生了什么。我尝试了一些东西,但没有运气。我不需要很多信息,比如'now loading id 123'
我尝试过ob_flush,但显然不再支持:http://php.net/ob_flush
我也尝试过使用CURLOPT_PROGRESSFUNCTION
,但是没有很多文档,我无法使用它。我的代码非常简单:
$sql = "select item_number from products order by id desc";
$result_sql = $db->query($sql);
while($row = $result_sql->fetch_assoc())
{
//I'd like it to display this as it loads
print '<br>Getting data for item_number: '.$row['item_number']
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,"http://targetsite.com//".$row['item_number']);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSLVERSION, 3);
//curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 ( .NET CLR 3.5.30729)");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
}
有什么建议吗?我真的不挑剔,只是最简单的方法,会很快告诉我发生了什么事。
答案 0 :(得分:1)
假设您使用的是PHP&gt; = 5.3(以前的版本不支持CURLOPT_PROGRESSFUNCTION
),您可以这样使用它:
function callback($download_size, $downloaded, $upload_size, $uploaded)
{
// do your progress stuff here
}
$ch = curl_init('http://www.example.com');
// This is required to curl give us some progress
// if this is not set to false the progress function never
// gets called
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
// Set up the callback
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'callback');
// Big buffer less progress info/callbacks
// Small buffer more progress info/callbacks
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
$data = curl_exec($ch);