2011-06-10 105 views
2

因此從我的理解來看,這應該相當簡單,因爲我應該只需要更改原始文件集內容代碼,並且腳本的其餘部分仍然可以工作?我已經註釋掉了舊文件獲取內容並添加了下面的捲曲。CURL替代內置的「file_get_contents()」函數

從文件中更改獲取內容捲曲下面的代碼後不會輸出

//$data = @file_get_contents("http://www.city-data.com/city/".$cityActualURL."-".$stateActualURL.".html"); 
//$data = file_get_contents("http://www.city-data.com/city/Geneva-Illinois.html"); 

//Initialize the Curl session 
$ch = curl_init(); 
$url= "http://www.city-data.com/city/".$cityActualURL."-".$stateActualURL.".html"; 
//echo "$url<br>"; 
$ch = curl_init(); 
    $timeout = 5; 
    curl_setopt($ch,CURLOPT_URL,$url); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); 
    $data = curl_exec($ch); 
    curl_close($ch); 
//echo $data; 

$details = str_replace("\n", "", $data); 
$details = str_replace("\r", "", $details); 


$detailsBlock = <<<HTML 
~<div style='clear:both;'></div><br/><b>(.*?) on our <a href='http://www.city-data.com/top2/toplists2.html'>top lists</a>: </b><ul style='margin:10px;'>(.*?)<div style='bp_bindex'>~ 
HTML; 

$detailsBlock2 = <<<HTML 
~<br/><br/><b>(.*?) on our <a href='http://www.city-data.com/top2/toplists2.html'>top lists</a>: </b><ul style='margin:10px;'>(.*?)</ul></td>~ 
HTML; 

$detailsBlock3 = <<<HTML 
~<div style='clear:both;'></div><br/><b>(.*?) on our <a href='http://www.city-data.com/top2/toplists2.html'>top lists</a>: </b><ul style='margin:10px;'>(.*?)</ul></td>~ 
HTML; 

preg_match($detailsBlock, $details, $matches); 
preg_match($detailsBlock2, $details, $matches2); 
preg_match($detailsBlock3, $details, $matches3); 

if (isset($matches[2])) 
{ 
    $facts = "<ul style='margin:10px;'>".$matches[2]; 
} 

elseif (isset($matches2[2])) 
{ 
    $facts = "<ul style='margin:10px;'>".$matches2[2]; 
} 

elseif (isset($matches3[2])) 
{ 
    $facts = "<ul style='margin:10px;'>".$matches3[2]; 
} 

else 
{ 
    $facts = "More Information to Come..."; 
} 
+0

爲什麼要使用cURL而不是file_get_contents? – Halcyon 2011-06-10 19:57:02

+0

@Frits van Campen,因爲建議使用Curl。許多網絡服務器主機關閉使用file_get_contents()的選項; – joakimdahlstrom 2011-06-10 20:00:30

+0

所以你改變代碼W/O實際上需要改變它?玩的開心! – hakre 2011-06-10 20:01:42

回答

1

如果你有你的腳本問題,需要調試。例如:

$data = curl_exec($ch); 
var_dump($data); die(); 

然後你會得到一個輸出什麼$data是。根據輸出情況,您可以進一步決定下一步查找故障原因。

+0

我使用var_dump獲取bool(false)。 如果我回顯變量我得到整個頁面,但它不會做的是輸出數組 – MSD 2011-06-10 20:03:52

+0

好吧,那麼難怪你沒有得到任何數據,因爲你的請求失敗。要了解錯誤返回值的含義,請訪問PHP手冊:['curl_exec'](http://www.php.net/function.curl-exec) – hakre 2011-06-10 20:06:47

+1

curl_exec在出錯時返回false。你可以用'curl_error($ ch)'得到錯誤信息。 – 2011-06-10 20:13:40

1

下面的函數很好用,只是傳遞一個URL。

function file_get_data($url) { 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser. 
    curl_setopt($ch, CURLOPT_URL, $url); 
    $data = curl_exec($ch); 
    curl_close($ch); 
    return $data; 
} 

提示:可以用一行代碼替換新行和回車符。

$details = str_replace(array("\r\n","\r","\n"), '', $data);