PHP无法读取远程文件的内容

时间:2014-12-06 10:49:28

标签: php xml caching cookies

我是PHP编程的新手。我试过并且一直未能获得下面这个独特文件的内容:

http://nghenhacvang.net/playplaylist/5730.xml

使用API​​ get_file_contents。该文件使用CHROME具有以下信息:

Remote Address:23.226.231.225:80
Request URL:http://nghenhacvang.net/playplaylist/5730.xml
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Cookie:PHPSESSID=2g8id3hevm0sqo3qv8ln8n04h2; __utma=107959197.1514810948.1417822471.1417840071.1417854776.3; __utmc=107959197; __utmz=107959197.1417822471.1.1.utmcsr=search.pch.com|utmccn=(referral)|utmcmd=referral|utmcct=/search
Host:nghenhacvang.net
User-Agent:Mozilla/5.0 (Windows NT 6.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36
Response Headersview source
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:660
Content-Type:text/xml
Date:Sat, 06 Dec 2014 10:32:55 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=15, max=100
Pragma:no-cache
Server:Apache/2.2.3 (CentOS)
Set-Cookie:SID=0a2f9b6ce1d6280ca5fa74e118a2e1b6; expires=Thu, 01-Jan-1970 01:00:00 GMT
Vary:Accept-Encoding,User-Agent
X-Powered-By:PHP/5.1.6

1 个答案:

答案 0 :(得分:0)

似乎file_get_contents()在没有设置用户代理的情况下无法正常工作。添加其他流上下文以设置标头用户代理。

$url = 'http://nghenhacvang.net/playplaylist/5730.xml';
$options = array('http' => array('user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'));
$context = stream_context_create($options);
$contents = file_get_contents($url, false, $context);
$xml = simplexml_load_string($contents);

echo '<pre>';
print_r($xml);

Sample Output

您也可以使用curl替代:

$url = 'http://nghenhacvang.net/playplaylist/5730.xml';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$contents = curl_exec($ch);

$xml = simplexml_load_string($contents);

echo '<pre>';
print_r($xml);

奖励:在检索到内容后,这里有一些简单的用法:

foreach($xml->channel->item as $item) {
    $description = (string) $item->description;
    $title = (string) $item->title;
    $jwplayer = $item->children('jwplayer', 'http://rss.jwpcdn.com/');
    $source = (string) $jwplayer->source->attributes()->file;
    $image_url = (string) $jwplayer->image;

    // presentation
    echo "
    Title: $title <br/>
    Description: $title <br/>
    Source: $source <br/>
    Image URL: $image_url <br/>
    <hr/>
    ";
}

Sample Output