PHP Simplexml failing on localhost

此生再无相见时 提交于 2019-12-11 04:09:38

问题


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.

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

and

I/O warning : failed to load external entity

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 is on for both servers. So it isn't that. It could be a PHP version error.

Using PHP 5.1.4 on my localhost.

Using PHP 5.2.5 on my main server.

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.

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?


回答1:


It's almost certainly your proxy server. If you use curl you can set it up to go through your proxy.




回答2:


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.

Two possible solutions :

  • 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 :-(

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 ;-)

Have fun !



来源:https://stackoverflow.com/questions/1366824/php-simplexml-failing-on-localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!