为什么服务器无法获取网站的标题


why server could not fetch title of website?

我正试图获得一个网站的标题。这段代码在我的电脑上运行得很好,但在服务器上运行得不顺利。在服务器上,它无法获取url内容。在我的电脑上,它很容易重定向。

<?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();
?>

服务器上的输出是http://sendmysms.bugs3.com/cricket/fetch.php

似乎没有启用fopen包装器。正如您在file_get_contents的php文档的注释部分中所看到的,allow_url_fopen必须设置为true才能使用file_get-contents打开url。请尝试在服务器上运行以下操作,看看是否可以将file_get_contents与url一起使用。

echo "urls ";
echo (ini_get('allow_url_include')) ? "allowed" : "not allowed";
echo " in file_get_contents.";

如果上面写着"file_get_contents中不允许url",那么您需要通过php.ini、.htaccess文件、apacheconfig或类似的文件来更新设置。也就是说,如果您想继续使用file_get_contents来访问url。另一种选择是,如果安装了php-ccurl扩展,则使用curl。

p.S.我知道这是对file_get_contents的调用的问题,因为你可以看到他的脚本在他设置$html变量后会回显它。他在服务器上的脚本链接没有输出任何html,这告诉我这是抓取html而不是html解析器的问题。