PHP Classes

load_url

Recommend this page to a friend!

      PHP HTTP protocol client  >  All threads  >  load_url  >  (Un) Subscribe thread alerts  
Subject:load_url
Summary:Script stops if load_url() function can't load address
Messages:2
Author:Orion Tiller
Date:2009-06-01 18:40:03
Update:2009-06-02 08:14:00
 

  1. load_url   Reply   Report abuse  
Picture of Orion Tiller Orion Tiller - 2009-06-01 18:40:03
I have a php file that stops running in the middle if there is an unsuccessful load_url() function call. Is there a way to make the rest of the code execute even if the url can't be reached? Thanks


code
...
$url = "http://www.someurlthatistemporarilydown.com/submit.php";
$result = load_url($url);

//if the url is temporarily down then the code stops and the rest of the code doesn't execute
...
rest of code





Here is the load_url function that is declared before the call to it.
require("http.php");

function load_url( $url ) {
$returnvalue = "";
set_time_limit(0);

$http=new http_class;

$http->timeout=0;
$http->data_timeout=0;
$http->debug=0;
$http->html_debug=0;
$http->follow_redirect=0;
$http->user_agent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";

$error=$http->GetRequestArguments($url,$arguments);

$error=$http->Open($arguments);
if($error) die($error);

$error=$http->SendRequest($arguments);
if($error) die($error);

$headers=array();
$error=$http->ReadReplyHeaders(&$headers);
if($error) die($error);

for(Reset($headers),$header=0;$header<count($headers);Next($headers),$header++) {
$header_name=Key($headers);
if(GetType($headers[$header_name])=="array")
{
for($header_value=0;$header_value<count($headers[$header_name]);$header_value++)
if ($header_name == "location") { return load_url($headers[$header_name][$header_value]); }
}
else
if ($header_name == "location") {
$redir = $headers[$header_name];
$regexp = "http(s?)\:\/\/(.*)\/(.*)";
if(preg_match("/$regexp/siU", $redir, $matches)) {
$url = $redir;
return load_url($url);
} else {
if(preg_match("/$regexp/siU", $url, $matches)) {
$url = "http://".$matches[2].str_replace(" ", "%20", $redir);
return load_url($url);
} else {
return "An Error Has Occurred.";
}
}
}
}

for(;;) {
$error=$http->ReadReplyBody($body,1000);
if ($error!="" || strlen($body)==0)
break;
$returnvalue .= $body;
}
return $returnvalue;
}

  2. Re: load_url   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2009-06-02 08:14:01 - In reply to message 1 from Orion Tiller
If you call the die function, it will stop your script.

You should consider just returning an empty string instead of calling die, when for some reason there was an error.