simplexml_load_file Document is empty

2019-07-18 04:33发布

问题:

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.