[請益] curl抓google資料中斷

看板PHP作者 (mewtwo)時間16年前 (2009/08/26 12:03), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
結果還是有問題,只好再次上來請益各位先進。 以下的程式目的是往google丟不斷變換的關鍵字, 再分析結果。 但是老是在不明處中斷, 我設了set_time_limit及CURLOPT_TIMEOUT也沒用…… 能否請高手指出錯誤之處,或是指導一下如何設錯誤處理? ===程式== <?php set_time_limit(1800); $fileName = fopen("./xxx.txt", "r" ); $numLine = 0; while( $line = fgets($fileName) ){ $nameArray[$numLine] = trim($line); //echo $numLine.$line; $numLine++; } fclose($fileName); /* for( $i = 0; $i < $numLine; $i++ ){ echo $nameArray[$i]."<br/>"; } */ $output = fopen( "./output_BMP.txt", "w" ); for( $i = 0; $i < $numLine; $i++ ){ fputs( $output, $nameArray[$i]."\t" ); for( $j = 0; $j < $numLine; $j++ ){ echo $i." "; if( $i < $j ){ $str1 = str_replace( " ", "+", $nameArray[$i] ); $str2 = str_replace( " ", "+", $nameArray[$j] ); $url = "http://www.google.com/search?hl=en&q=".$str1."+".$str2; echo $url; $ch = curl_init(); $timeout = 1800; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout ); curl_setopt($ch, CURLOPT_TIMEOUT, 1800 ); curl_setopt($ch, CURLOPT_USERAGENT, "Yahoo Bot" ); //curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); //curl_setopt($ch, CURLOPT_USERPWD, US_NAME.":".US_PWD); //sleep(1); $contents = curl_exec($ch); curl_close($ch); //echo $contents; $target = "swrnum="; // length = 7 $contents = strstr( $contents, $target ); //echo $contents; $swrnum = substr($contents, strlen($target), strpos($contents, "\"" ) - strlen($target) ); //echo " = ".$swrnum."<br/>"; fputs( $output, $swrnum."\t" ); } // end if else{ fputs( $output, "0"."\t" ); } // end else echo "<br/>"; } // end for $j fputs( $output, "\n" ); echo "<br/>"; } // end for $i fclose($output); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title> test </title> </head> <body> </body> </html> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 128.211.252.99
文章代碼(AID): #1AbBG7RC (PHP)
文章代碼(AID): #1AbBG7RC (PHP)