PHP Simplexml在本地主机上失败 [英] PHP Simplexml failing on localhost

查看:66
本文介绍了PHP Simplexml在本地主机上失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编辑一个我的混搭,其中我通过它们的API(xml)来获取last.fm数据.

I'm editing a mashup of mine where I fetch last.fm data through their API (xml).

由于某种原因,我的本地主机无法连接到该文件:它出现以下错误.

For some reason, my localhost can't connect to the file: it gives the following error.

无法打开流:连接 尝试失败,因为已连接 派对后未正确回应 时间段或已建立 连接失败,因为已连接 主机未响应.在 C:\ Web \ Apache \ Apache2 \ htdocs \ soctfm \ user.php 在第35行

failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\Web\Apache\Apache2\htdocs\soctfm\user.php on line 35

I/O警告:无法加载外部 实体

I/O warning : failed to load external entity

问题是,它可以在我的主服务器上正常工作.当我尝试从本地主机获取xml时失败.

The problem, is, is that it works fine on my main server. When I try to fetch the xml from my localhost it fails.

allow_url_fopen均打开.事实并非如此.可能是PHP版本错误.

allow_url_fopen is on for both servers. So it isn't that. It could be a PHP version error.

在我的本地主机上使用PHP 5.1.4.

Using PHP 5.1.4 on my localhost.

在我的主服务器上使用PHP 5.2.5.

Using PHP 5.2.5 on my main server.

我必须添加,我认为这可能是问题所在,我正在大学网络中工作.这意味着,我们必须通过代理连接到Internet.

I must add, which I think could be the problem, I'm working from my university internet. This means, we have to connect through a proxy to the internet.

最后,您会看到一切正常. XML和代码可以在我的主服务器上完美运行,但是在我的本地主机上却失败了,我也不知道为什么.

You see, in the end, everything is supposed to work fine. The XML and code works perfectly on my main server, but manages to fail on my localhost, and I don't know why.

我还能做更多的调试吗?可以是代理吗?

Is there any more debugging I can do? Could it be the proxy?

推荐答案

如果像您说的那样,您位于代理后面,则您的

If, like you say, you are behind a proxy, your simplexml_load_file will probably fail : PHP will not be able to connect directly to the remote server, and won't be able to download the XML file.

两种可能的解决方案:

  • 首先,使用其他来获取数据(例如curl,configureg以使用代理;请参见可以与 curl_setopt );然后只有使用 simplexml_load_string .
  • 或者,使用 stream_context_create 配置使用代理的流.
    • 关于这一点,您可以在此处看到我的答案:这是您遇到的相同问题
    • 尽管如此,您可能必须先使用file_get_contents来获取XML数据,然后才使用simplexml_load_string进行加载,因为simplexml_load_file似乎没有接受流上下文作为参数. :-(
    • first, fetch the data, with something else (like curl, configureg to use the proxy ; see the options you can use with curl_setopt) ; and only then, use simplexml_load_string.
    • Or, use stream_context_create to configure a stream that uses the proxy.
      • About that, you can see my answer there : it was the same problem you are experiencing
      • Though, you'll probably have to fetch the XML data first, with file_get_contents, and, only then, use simplexml_load_string to load it, as simplexml_load_file doesn't seem to accept a stream context as parameter :-(

      我想第二种解决方案应该很好用;并且不需要太多的工作:只需在您位于localhost上时配置流上下文即可.
      我想,这意味着无需处理卷曲就可以减少工作量;-)

      The second solution should work quite nice, I suppose ; and shouldn't require too much work : just a stream context to configure when you are on localhost.
      Which means less work than having to deal with curl, I suppose ;-)

      玩得开心!

      这篇关于PHP Simplexml在本地主机上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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