2016-03-04 72 views
0

我想使用file_get_contents解析網頁。對於方法後,我可以解析哪些是列表中的第一頁。在此之後,頁面內容通過分頁列出。PHP使用GET請求在分頁頁面上使用file_get_contents解析網頁

如何用GET請求解析分頁頁面的內容?

例如:http://sample.com/results?page=3

代碼:

$data = array(
     'page' => 1 
     ); 

    $opts = array(
     'http'=>array(
     'method'=> 'GET', 
     'headers' => $http_response_header, 
     'content' => http_build_query($data), 
     'timeout' => 100 
     ) 

    ); 

    $context = stream_context_create($opts); 

    $listfile1 = file_get_contents("http://sample.com/results?",false, $context); 

我得到0記錄分頁的頁面。

+0

我會推薦使用cURL來代替更復雜的URL解析示例,就像你的。 – divix

+0

@divix謝謝。 我曾經用cURL試過,結果也一樣。 file_get_contents在循環內執行。是否需要在每個循環後關閉任何流或會話? 這是因爲如果我嘗試使用URL [鏈接](http://sample.com/results?page=2),首先在循環中返回解析的第二個頁面,該頁面是正確的,然後是空的。 – Gins

+0

確保傳遞了所有必需的標題(如果有的話)。 – divix

回答

0

我能夠使用上面列出的相同功能,有一個自定義函數定義了打破分頁。

但我也改變了網址。

 $listfile1 = file_get_contents("sample.com/results?page=".$number,false, $context);