Simplexml_load_file 错误 [英] Simplexml_load_file errors

查看:46
本文介绍了Simplexml_load_file 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 PHP 解析这个地址

$url = 'http://pubapi.yp.com/search-api/search/devapi/search?searchloc=91203&term=pizza&format=xml&sort=distance&radius=5&列表计数=10&key=t266jc29dx';$x1 = simplexml_load_file($url);

但这是我不断得到的

<块引用>

警告:simplexml_load_file(http://pubapi.yp.com/search-api/search/devapi/search?searchloc=91203):无法打开流:HTTP 请求失败!HTTP/1.1 500 内部服务器错误...

如您所见,我只获取了一部分 url 参数.

你能帮忙吗?

解决方案

通过一些关于如何通过 cURL 检索 XML 的快速搜索,这就是我为检索和解析 XML 所做的.

我在上面的评论中提到的一个重要原因是,您不能将 URL 直接传递到其中包含与符号 (&) 的 simple_xml_load_string 函数中.它是表示实体的 XML 格式的字符.

';打印_r($xml);echo '</pre>';?>

I'm triying to parse this address in PHP

$url = 'http://pubapi.yp.com/search-api/search/devapi/search?searchloc=91203&term=pizza&format=xml&sort=distance&radius=5&listingcount=10&key=t266jc29dx' ;
$x1 = simplexml_load_file($url);

But here's what I keep getting

Warning: simplexml_load_file(http://pubapi.yp.com/search-api/search/devapi/search?searchloc=91203): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in...

As you can see I'm only getting part of the url parameters.

Can you help?

解决方案

With some quick searching on how to retrieve XML via cURL this is what I did to retrieve and parse the XML.

The big reason as I mentioned above in a comment is that you can't pass URLs directly into the simple_xml_load_string function with ampersands(&) in them. It's a character in the XML format that represents entities.

<?php

function get_xml_from_url($url){
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    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');

    $xmlstr = curl_exec($ch);
    curl_close($ch);

    return $xmlstr;
}

$url = 'http://pubapi.yp.com/search-api/search/devapi/search?searchloc=91203&term=pizza&format=xml&sort=distance&radius=5&listingcount=10&key=t266jc29dx';
$contents = get_xml_from_url($url);

$xml = simplexml_load_string($contents);

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

这篇关于Simplexml_load_file 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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