2016-02-29 58 views
1

我有以下代碼。從URL獲取數據時發生。 PHP

<?php 
    error_reporting(0); 
    $url = 'https://www.inmateaid.com/prison-search/all?&page=1'; 
    $output = file_get_contents($url); 
    $doc = new DOMDocument(); 
    $doc->loadHTML($output); 
    $selector = new DOMXPath($doc); 
    $anchors = $selector->query("/html/body//div[@class='media']//div/h4//a"); 
    foreach($anchors as $a) 
    { 
     $output = file_get_contents($a->getAttribute("href")); 
     echo 'hi'; 
    } 
?> 

如果我們看下面的代碼產生10個錨

$anchors = $selector>query("/html/body//div[@class='media']//div/h4//a");

所以應如何打印10倍?但它只打印3次。

當我在for循環中註釋行時,它會打印10次。

我錯過了什麼嗎?

+0

我剛剛嘗試了你的代碼,並且按照預期,我得到了10次'hi'。這是您正在使用的實際完整代碼嗎? – jszobody

+0

是的,我得到了10倍'嗨'與試用代碼.. –

+0

我再次檢查,令人驚訝的是,這次它打印4次。 – Pankaj

回答

0

這是由於max_execution_time = 30。我增加了時間,問題得到修復。