2011-06-10 7 views
2

원본 파일get 내용 코드 만 변경하면됩니다. 따라서 나머지 스크립트는 여전히 작동해야합니다. 이전 파일의 내용을 주석으로 처리하고 아래에 컬을 추가했습니다.내장 된 "file_get_contents()"함수 대신 CURL을 사용합니다.

내용은 아래의 코드를 컬하세요 파일에서 변경 한 후하지 않습니다 당신이 스크립트에 문제가있는 경우 당신은 그것을 디버깅해야

//$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

왜 file_get_contents 대신 cURL을 사용 하시겠습니까? – Halcyon

+0

@Frits van Campen, Curl이 좋습니다. 많은 웹 서버 호스트는 file_get_contents(); – joakimdahlstrom

+0

그래서 코드를 변경하면 실제로 변경해야합니다. 재미있어! – hakre

답변

1

출력. 예를 들어 :

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

은 그럼 당신은 $data가 무엇인지 출력을 얻을 것이다. 출력에 따라 오작동의 원인을 찾을 위치를 결정할 수 있습니다.

+0

var_dump를 사용하여 bool (false)를 얻습니다. 변수를 에코하면 전체 페이지가 표시되지만 배열이 출력되지 않습니다. – MSD

+0

요청이 실패 했으므로 데이터가 표시되지 않습니다. 잘못된 반환 값의 의미에 대해 알아 보려면 PHP Manual : ['curl_exec'] (http://www.php.net/function.curl-exec) – hakre

+1

curl_exec은 오류시 false를 반환합니다. 'curl_error ($ ch)'로 에러 메세지를 얻을 수있다. –

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); 
관련 문제