Reputation: 21
I am trying to get the title of a website. This code works perfectly on my computer but on the server it is not running smoothly. On server it could not fetch the url content. On my computer it is easily redirecting.
<?php
ini_set('max_execution_time', 300);
$url = "http://www.cricinfo.com/ci/engine/match/companion/597928.html";
if(strpos( $url, "companion" ) !== false)
{
$url = str_replace("/companion","",$url);
}
$html= file_get_contents($url);
echo $html;
//parsing begins here:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');
//get and display what you need:
$title = $nodes->item(0)->nodeValue;
$msg1 = current(explode("|", $title));
$msg=rawurlencode($msg1);
echo $msg;
if(empty($msg))
{
echo "no data to send";
}
else
{
header("Location:fullonsms.php?msg=" .$msg);
}
exit();
?>
the output on server is this http://sendmysms.bugs3.com/cricket/fetch.php
Upvotes: 1
Views: 114
Reputation: 804
It appears that the fopen wrappers aren't enabled. As you can see in the notes section of the php docs for file_get_contents, allow_url_fopen must be set to true in order to open a url with file_get_contents. Try running the following on the server to see if you can use file_get_contents with a url.
echo "urls ";
echo (ini_get('allow_url_include')) ? "allowed" : "not allowed";
echo " in file_get_contents.";
If that says 'urls not allowed in file_get_contents' then you'll need to update the setting via the php.ini, a .htaccess file, apache config, or some such equivalent. That is, if you would like to continue using file_get_contents to access the url. Another option is to use curl if you have the php curl extension installed.
P.S. I know this is a problem with the call to file_get_contents since you can see that his script echos the $html variable after he sets it. His link to his script on the server doesn't output any html which tells me this is an issue with grabbing the html rather than the html parser.
Upvotes: 3