2015-09-01 52 views
-2

當我WampServer開始我的代碼,我得到這個錯誤:
錯誤在簡單的HTML DOM


Warning: file_get_contents(http://www.fragrantica.com/designers/A-Perfume- 
Organic.html): failed to open stream: A connection attempt failed because the 
connected party did not properly respond after a period of time, or established 
connection failed because connected host has failed to respond. in 
F:\wamp\www\atr\fragantica\simple_html_dom.php on line 76 


我測試一些東西前,它適合我的URL 3分的,但之後給我
!這個錯誤。我用用simple_html_dom version.My代碼最新版本我瓦帕本地服務器是一個有點複雜,但可以讀...

function connect($furl,$fsname){ 
$fup=fopen("$furl","r"); // open file of urls for read 
$fname=fopen("$fsname","r"); // open file of file's names for set name 
$i=0; 
while(!feof($fup)){ 
    $url=trim(fgets($fup)); // read lines from furl file 
    $name=trim(fgets($fname)); 
    $fdoc=fopen("$name.txt","w"); // make a new file for put contents in it 
    $html=file_get_html("$url"); // read contents from favurites html page 
    foreach($html->find("div.perfumeslist p") as $tag){ 
     foreach($tag->find("a") as $alink){ 
      $perlink="http://www.fragrantica.com".$alink->href; 
      fwrite($fdoc,"##PERFUME_LINK:##".$perlink."\n"."\n"); 
     } 
     foreach($tag->find("img") as $im){ 
      fwrite($fdoc,"##THUMB_SRC:##".$im->src."\n"); 
     } 
     foreach($tag->find("span.mtext") as $sp){ 
      fwrite($fdoc,"##SEX:##".$sp->innertext."\n"); 
     } 
     $perfume=file_get_html("$perlink"); 
     foreach($perfume->find("div") as $disc){ 
      if(strcmp($disc->itemprop,"description")===0){ 
       fwrite($fdoc,"##DESCRIPTION:##".$disc->innertext."\n"); 
      } 
     } 
     foreach($perfume->find("div#mainpicbox img") as $per){ 
      $pic=$per->src; 
      fwrite($fdoc,"##MAINPICURL:##".$pic."\n"); 
     } 
     foreach($html->find("div") as $tag){ 
      if(strcmp($tag->style,"width: 230px; float: left; text-align: center; clear: left;")===0){ 
       foreach($tag->find("p") as $notes){ 
        fwrite($fdoc,"##NOTES:##".$notes->innertext."\n"."\n"); 
       } 
      } 

     } 


    fwrite($fdoc,"___________________________________________________________________"."\n"); 
} 
     fclose($fdoc); 
    } 
    fclose($fup); 
    fclose($fname); 
} 

關於我的代碼:
在這個函數中,我讀取兩個文件:一個用於我的文本文件的名稱,另一個用於URL文件的
。首先從這些文件中逐行讀取,並且直到
結束文件,然後使用file_get_html並獲取標籤和屬性
得到它的鏈接源和內文...

+0

試圖用捲曲操作 – Tushar

+0

我需要這個代碼快!我沒有足夠的時間來學習捲曲庫。 @Tushar – Khaatam

+0

URL在這裏或在您的代碼中分成兩行? – chris85

回答

1
$ch = curl_init(); 
$timeout = 20; 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 

$fileContents = curl_exec($ch); 
curl_close($ch); 

// Create a DOM object 
$html = new simple_html_dom(); 
// Load HTML from a string 
$html->load($fileContents); 

嘗試用這個,我還沒有執行此所以我不知道

+0

是的,你可以用這個查詢 – Tushar

+0

發生這個錯誤!'最大執行時間超過120秒'我該怎麼辦? @Tushar – Khaatam

+0

剛更新的答案是 $ timeout = 20; curl_setopt($ ch,CURLOPT_CONNECTTIMEOUT,$ timeout); 請檢查 – Tushar