2014-09-04 89 views
0

我想通過循環從多個URL獲取數據。在第一個循環中,我獲取數據,但在此之後,瀏覽器顯示「沒有數據接收」消息。我試圖延遲功能,但它不起作用。我該怎麼辦?我總是得到第一頁,但之後我沒有收到數據。這是我的代碼:PHP CURL:在循環中抓取多個頁面

public function Crawl(){ 

    $url=array(0=>array("ABCD"=> 
     array(0=>"http://abcd.com" 
     )), 
     1=>array("XYZ"=> 
     array(0=>"xyz.com" 
     )) 
     ); 

     foreach ($url as $key => $value) {     
      $this->Cron($value);     
      sleep(5); 
     } 

} 


public function Cron($url=null){   

    ob_start(); 
    set_time_out(0); 
    foreach($url as $urlkey=>$urlvalue){ 

     for($prodcount=0;$prodcount<count($urlvalue);$prodcount++){ 

     $ch = curl_init(); // Initialising cURL 
     curl_setopt($ch, CURLOPT_URL, $urlvalue[$prodcount]); // Setting cURL's URL option with the $url variable passed into the function 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Setting cURL's option to return the webpage data 
     $data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable 
     curl_close($ch); // Closing cURL 
     //echo $data; // Returning the data from the function 

     $html = str_get_html($data); 

     echo $html; 

     ob_flush(); 
    } 

} 
} 

回答

0

我認爲這可能是一個問題,該html是不正確的形成。

嘗試增加標籤和最重要的:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
+0

謝謝@Uxio,我嘗試,但仍然得到同樣的信息「沒有數據接收」。可能是頁面在第一次循環後沒有準備好。不知道如何解決這個問題。 – 2014-09-04 07:42:42