2015-09-01 55 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