function feedSearch($url) {
if($html = @DOMDocument::loadHTML(file_get_contents($url))) {
$xpath = new DOMXPath($html);
$feeds = $xpath->query("//head/link[@href][@type='application/rss+xml']/@href");
if($feeds->length != 0){
$url = rtrim($url, '/');
if(strpos($url, 'https://')){
$url = ltrim($url, 'https://');
return $feedURL = $url . "/feed";
}else{
return $feedURL = $url . "/feed";
}
}
}
return false;
}
if(feedSearch($url)){
$xml = feedSearch($url);
}else{
echo $url . " is not a valid feed URL.";
die();
}
上面的代码在我的本地主机中运行良好,但在我的服务器中效果不佳。在服务器中,它将死亡。我不知道我的服务器中缺少什么。如何在PHP中调试版本问题?
您需要
在服务器的 php.ini 文件中添加allow_url_fopen = On
。
或者,如果您无权访问 php.ini 文件,则可以在 .htaccess 文件中添加php_value allow_url_fopen On
。
或者正如@Ohgodwhy指出的那样,最好使用 curl。您可以使用 curl 创建自己的函数,然后使用它代替 file_get_contents
:
function get_contents_from_url($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}