simplexml_load_file Document is empty

僤鯓⒐⒋嵵緔 提交于 2019-12-24 00:43:24

问题


I'm pulling from a RSS feed:

http://search.library.utoronto.ca/UTL/index?Ntt=starcraft&Ntk=Anywhere&Ntx=mode+matchallpartial&N=0&Nu=p_work_normalized&Np=1&rss=1

If you navigate to that with a browser, you get a nice xml page.

However, if I do

simplexml_load_file("the above url");

in php, I get

1: parser error : Document is empty
1: parser error : Start tag expected, '<' not found in my_file

So why can the browser get it but not PHP?

If I do file_get_contents("the above url") the function fails with FALSE returned.


回答1:


The site is blocking requests from PHP.

First run

<?php
ini_set('user_agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0');

(or some other valid user agent) and it will work.



来源:https://stackoverflow.com/questions/12957779/simplexml-load-file-document-is-empty

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