2011-06-10 109 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);