我需要阅读一个xml,我没有找到另一种方法来阅读它,我刚刚编辑了$ movie这个,我得到了这个错误信息:
Warning: file_get_contents(http://news.google.com.mx/news?hl=es&gl=mx&q=harry potter&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&um=1&ie=UTF-8&output=rss) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request
我收到此错误,这是我的代码,错误是在网址中有人可以帮助,如何解决?
$url =
$data = file_get_contents('http://news.google.com.mx/news?hl=es&gl=mx&q='.$movie.'&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&um=1&ie=UTF-8&output=rss');
$xml = new SimpleXMLElement($data);
$channel = array();
$channel['title'] = $xml->channel->title;
foreach ($xml->channel->item as $item)
{
//echo $article['title'] = $item->title;
//echo $article['link'] = $item->link;
echo $article['pubDate'] = $item->pubDate;
echo $article['description'] = (string) trim($item->description);
}
答案 0 :(得分:2)
如果你urlencode()有网址参数(例如$movie
),它应该按预期工作。
示例:
$data = file_get_contents('http://news.google.com.mx/news?hl=es&gl=mx&q='. urlencode($movie) .'&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&um=1&ie=UTF-8&output=rss')
答案 1 :(得分:2)
您需要在urlencode中包含$movie
。我还添加了一个内容类型标题,以便您可以指定该Feed中使用的UTF-8编码,并为非英语字符正确呈现文本。
<?php
header('Content-Type:text/html;charset=utf-8');
$movie = 'harry potter';
$url =
$data = file_get_contents('http://news.google.com.mx/news?hl=es&gl=mx&q='.urlencode($movie).'&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&um=1&ie=UTF-8&output=rss');
$xml = new SimpleXMLElement($data);
$channel = array();
$channel['title'] = $xml->channel->title;
foreach ($xml->channel->item as $item)
{
//echo $article['title'] = $item->title;
//echo $article['link'] = $item->link;
echo $article['pubDate'] = $item->pubDate;
echo $article['description'] = (string) trim($item->description);
}
?>