使用 php 制作 xml feed 的本地副本时遇到问题

发布于 2024-12-05 22:52:06 字数 1018 浏览 0 评论 0原文

我正在尝试保存 xml 文件的本地副本,然后使用简单的 xml 打开它,但我遇到了一些错误..这是我的代码:

$feedURL = "https://gdata.youtube.com/feeds/api/users/manitobachildhealth/favorites";

//$xml = file_get_contents("$feedURL");
$xml = file_get_contents($feedURL);
file_put_contents("video.xml", $xml);


// read feed into SimpleXML object
//$sxml = simplexml_load_file($feedURL);
$sxml = simplexml_load_file('video.xml');

我收到的错误如下:

Warning: file_get_contents(https://gdata.youtube.com/feeds/api/users/manitobachildhealth/favorites) [function.file-get-contents]: failed to open stream: Result too large in D:\wamp\www\videos2.php on line 48

我不确定为什么结果会太大,它只返回 6kb 的 xml。我做错了什么?

更新: 这是使用 WAMP 服务器在 Windows 平台上运行的 - 并不理想,但我坚持使用它。

更新2: 我尝试使用curl 和fwrite 来实现类似的结果,如下所示,但它不会将xml 文件写入本地服务器。但它不会给我任何错误。

更新3: 这显然是托管环境的一个非常具体的问题,但我不确定从哪里开始寻找问题。使用curl 在基于Linux 的开发服务器上运行良好,但在基于Windows 的生产服务器上会导致问题。如果您能提供解决此问题的额外帮助,我们将不胜感激!

I'm trying to save a local copy of an xml file, and then open it with simple xml, but i'm getting some errors.. here's my code:

$feedURL = "https://gdata.youtube.com/feeds/api/users/manitobachildhealth/favorites";

//$xml = file_get_contents("$feedURL");
$xml = file_get_contents($feedURL);
file_put_contents("video.xml", $xml);


// read feed into SimpleXML object
//$sxml = simplexml_load_file($feedURL);
$sxml = simplexml_load_file('video.xml');

The error i'm getting is as follows:

Warning: file_get_contents(https://gdata.youtube.com/feeds/api/users/manitobachildhealth/favorites) [function.file-get-contents]: failed to open stream: Result too large in D:\wamp\www\videos2.php on line 48

I'm not sure why it would be too large of a result, it only returns 6kb of xml. what am i doing wrong?

Update:
This is running on a windows platform using WAMP server - not ideal, but i'm stuck with it.

Update 2:
I've tried using curl and fwrite to achieve a similar result, as suggested below, but it won't write the xml file to the local server. It doesn't give me any errors though.

update 3:
This is obviously a very specific problem with the hosting environment, but I'm not sure where to start looking for the problem. Using curl works great on a linux-based dev server, but is causing problems on this windows-based production server. An extra help in troubleshooting this issue would be most appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦行僧 2024-12-12 22:52:06

问题的正确答案:

您可能遇到与此问题相同的问题:CURL 和 HTTPS,“无法解析主机”(DNS 问题)


其他详细信息:

您可以使用 SimpleXML 加载和保存

$xml = new SimpleXMLElement('https://gdata.youtube.com/feeds/api/users/manitobachildhealth/favorites', NULL, TRUE);
$xml->asXML('video.xml');

我已经测试过的 xml 数据上面的代码在 WAMP 服务器中,它工作正常。

更新:
如果上面返回错误消息“[simplexmlelement.--construct]:I/O警告:无法加载外部实体......”,则可能是您的服务器不允许包含外部数据或php文件/脚本不允许拥有正确的许可。

请尝试以下操作:
1.回显xml文件的内容。

$xml = new SimpleXMLElement('https://gdata.youtube.com/feeds/api/users/manitobachildhealth/favorites', NULL, TRUE);
echo htmlentities($xml->asXML());

如果您设法检索 xml 内容并将其打印到浏览器,则您的服务器允许包含外部内容,并且很可能是文件权限问题。确保文件/脚本有权创建 xml 文件。

如果上述方法仍然不起作用,请尝试使用 cURL。

function getPageContent($options)
{
    $default = array(
        'agent' => $_SERVER['HTTP_USER_AGENT'],
        'url' => '',
        'referer' => 'http://'.$_SERVER['HTTP_HOST'],
        'header' => 0,
        'timeout' => 5,
        'user' => '',
        'proxy' => '',
    );
    $options = array_merge($default, $options);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $options['url']);
    curl_setopt($ch, CURLOPT_HEADER, $options['header']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if ($options['proxy'] != '') {
        curl_setopt($ch, CURLOPT_PROXY, $options['proxy']);
    }
    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $options['timeout']);
    curl_setopt($ch, CURLOPT_REFERER, $options['referer']);
    curl_setopt($ch, CURLOPT_USERAGENT, $options['agent']);
    if ($options['user'] != '') {
        curl_setopt($ch, CURLOPT_PROXYUSERPWD, $options['user']);
    }

    $result = array();
    $result['content'] = curl_exec($ch);
    $result['info'] = curl_getinfo($ch);
    $result['error'] = curl_error($ch);

    curl_close($ch);

    return $result;
}

$result = getPageContent(array(
    'proxy' => '[ip or address]:[port]', // if needed 
    'user' => '[username]:[password]',   // if needed
    'url' => 'http://gdata.youtube.com/feeds/api/users/manitobachildhealth/favorites'
));

if (empty($result['error'])) {
    // ok

    // content of xml file
    echo htmlentities($result['content']);

    // file
    $filename = 'video.xml';
    // Open File
    if (!$fp = fopen($filename, 'wt')) {
        die("Unable to open '$filename'\n\n");
    }
    // write content to file
    fwrite($fp, $result['content']);
    // close file
    fclose($fp);

} else {

    // failed
    echo '<pre>';
    echo 'Error details;';
    print_r ($result['error']);
    echo '<hr />Other info:';
    print_r ($result['info']);
    echo '</pre>';
}

Correct answer for the question:

It is possible you are having the same problem as of this question: CURL and HTTPS, "Cannot resolve host" (DNS-Issue)


Other Details:

You can use SimpleXML to load and save the xml data

$xml = new SimpleXMLElement('https://gdata.youtube.com/feeds/api/users/manitobachildhealth/favorites', NULL, TRUE);
$xml->asXML('video.xml');

I have tested the code above in a WAMP server and it works fine.

Update:
If the above returns error message "[simplexmlelement.--construct]: I/O warning : failed to load external entity ...." It's possible that your server does not allow to include external data or the php file/script does not have the right permission.

Try the following:
1. echo the content of the xml file.

$xml = new SimpleXMLElement('https://gdata.youtube.com/feeds/api/users/manitobachildhealth/favorites', NULL, TRUE);
echo htmlentities($xml->asXML());

If you managed to retrieved the xml content and print it to the browser, then your server is allowing to include external content and most likely the problem with the file permission. Make sure file/script have the right to create xml file.

If the above still does not work try using cURL.

function getPageContent($options)
{
    $default = array(
        'agent' => $_SERVER['HTTP_USER_AGENT'],
        'url' => '',
        'referer' => 'http://'.$_SERVER['HTTP_HOST'],
        'header' => 0,
        'timeout' => 5,
        'user' => '',
        'proxy' => '',
    );
    $options = array_merge($default, $options);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $options['url']);
    curl_setopt($ch, CURLOPT_HEADER, $options['header']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if ($options['proxy'] != '') {
        curl_setopt($ch, CURLOPT_PROXY, $options['proxy']);
    }
    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $options['timeout']);
    curl_setopt($ch, CURLOPT_REFERER, $options['referer']);
    curl_setopt($ch, CURLOPT_USERAGENT, $options['agent']);
    if ($options['user'] != '') {
        curl_setopt($ch, CURLOPT_PROXYUSERPWD, $options['user']);
    }

    $result = array();
    $result['content'] = curl_exec($ch);
    $result['info'] = curl_getinfo($ch);
    $result['error'] = curl_error($ch);

    curl_close($ch);

    return $result;
}

$result = getPageContent(array(
    'proxy' => '[ip or address]:[port]', // if needed 
    'user' => '[username]:[password]',   // if needed
    'url' => 'http://gdata.youtube.com/feeds/api/users/manitobachildhealth/favorites'
));

if (empty($result['error'])) {
    // ok

    // content of xml file
    echo htmlentities($result['content']);

    // file
    $filename = 'video.xml';
    // Open File
    if (!$fp = fopen($filename, 'wt')) {
        die("Unable to open '$filename'\n\n");
    }
    // write content to file
    fwrite($fp, $result['content']);
    // close file
    fclose($fp);

} else {

    // failed
    echo '<pre>';
    echo 'Error details;';
    print_r ($result['error']);
    echo '<hr />Other info:';
    print_r ($result['info']);
    echo '</pre>';
}
青萝楚歌 2024-12-12 22:52:06

您是否尝试过使用curl和fwrite来获取内容并将它们写入本地文件?

$ch = curl_init("https://gdata.youtube.com/feeds/api/users/manitobachildhealth/favorites");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);       
curl_close($ch);

fwrite("video.xml",$output);

Have you tried using curl and fwrite to get the contents and write them to a local file?

$ch = curl_init("https://gdata.youtube.com/feeds/api/users/manitobachildhealth/favorites");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);       
curl_close($ch);

fwrite("video.xml",$output);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文