XML Feed - 有效的HTTP标头用户代理? [英] XML Feed - Valid HTTP Header User Agent?

查看:140
本文介绍了XML Feed - 有效的HTTP标头用户代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用下面的脚本加载外部Feed,但生成的文件始终为空。提供XML的服务不提供任何支持,但声称我应该发送有效的HTTP头用户代理,而不是解释有效的HTTP头用户代理应该是什么。

I am trying to load an external feed using the script below, but the file that is generated is always empty. The service who offers the XML doesn't offer any support, but claims that I should send 'a valid HTTP Header User Agent', not explaining what a valid HTTP Header User Agent should be.

<?php
$feed = "[FEEDURL]"; //Hey Stackoverflow, I removed the URL on purpose
$content = file_get_contents($feed);
$dir = dirname($_SERVER['SCRIPT_FILENAME']);
$fp = fopen($dir.'/feedcopy.txt', 'w');
fwrite($fp, $content);
fclose($fp);
?>

任何人都有线索?此脚本适用于其他XML提要,因此不应该是问题。非常感谢提前。

Anyone got a clue? This script works with other XML feeds, so that shouldn't be the problem. Many thanks in advance.

推荐答案

您需要使用有效的上下文资源和stream_context_create(),如下所示:

You need to use a valid context resource with stream_context_create() like so:

<?php
$feed = "[FEEDURL]"; //Hey Stackoverflow, I removed the URL on purpose

$options = array(
  'http' => array(
    'method' => "GET",
    'header' => "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17\r\n" // Chrome v24
  )
);
$context = stream_context_create($options);

$content = file_get_contents($feed, false, $context);

$dir = dirname($_SERVER['SCRIPT_FILENAME']);
$fp = fopen($dir.'/feedcopy.txt', 'w');
fwrite($fp, $content);
fclose($fp);
?>

这篇关于XML Feed - 有效的HTTP标头用户代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆