2013-12-16 50 views
2

我在PHP中使用cURL來運行可能需要一個小時才能運行的腳本。爲了進行調試,我希望能夠通過查看屏幕(這不是公共站點)來查看請求的進度並查看發生了什麼。我嘗試了一些東西,但沒有運氣。我並不需要大量的信息,只是一些像「現在載入ID 123」php cURL進度監視器?

我試過使用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

哪個版本的PHP您使用的? – brandonscript

+0

from php info():PHP Version 5.3.24 – user2029890

回答

1

假設你正在使用PHP> = 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); 

來源:http://pastebin.com/bwb5VKpe

+0

我正在運行5.3。我試過了。它似乎沒有工作。函數中的變量返回0。另外,如果我把函數放在'while'循環之前,我得到的錯誤是它不能識別該函數,如果我把它放在循環中,我會在我已經調用它的第一次迭代之後收到一個錯誤。 – user2029890