开发者

CURL alternative to the built-in "file_get_contents()" function

开发者 https://www.devze.com 2023-03-12 16:02 出处:网络
So from my understanding this should be fairly simple as I should only need to change the original fileget contents code, and the rest of the script should still work? I have commented out the old fil

So from my understanding this should be fairly simple as I should only need to change the original fileget contents code, and the rest of the script should still work? I have commented out the old file get contents and added the curl below.

after changing from file get contents to cURL the code below does not output

    //$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, $matche开发者_如何转开发s3);

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...";
}


If you have a problem with your script you need to debug it. For example:

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

Then you will get an output what $data is. Depending on the output you can further decide where to look next for the cause of the malfunction.


The following function works great, just pass it a 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;
}

TIP: New lines and carriage returns can be replaced with one line of code.

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

精彩评论

暂无评论...
验证码 换一张
取 消