0

I want to read and parse the xml data from an url.My url is:"http://xml.gamebookers.com/sports/bandy.xml".I can access xml data from browser.However,when i attempt to read it by using php it doesnt work.It errors like this:

Warning: file_get_contents(http://xml.gamebookers.com/sports/bandy.xml): failed to open stream: Connection timed out in

How can i fix this?Any comments on this?

Thanks in advance..

Hüseyin BABAL
  • 15,400
  • 4
  • 51
  • 73
  • I whould suggest you do the following: Make sure the file exists at the url you are trying to open and get contents. Make sure the allow_url_fopen is ON. – Andreas Dec 19 '10 at 11:12
  • The url is available because i can access it from browser and allow_url_fopen is on.I tried another solution like this:I use file_ get_contents with stream parameter(for proxy) and i used return value of file_get_contents in simplexml_load.However the return value of file_get_contents is string so simplexml_load had error because of string parameter instead of xml file parameter – Hüseyin BABAL Dec 19 '10 at 13:15

2 Answers2

2

Please see here for an answer:

This error is most likely connected to too many (HTTP) redirects on the way from your script to the file you want to open. The default redirection limit should be 20. As 20 redirects are quite alot there could be some error in the filename itself (causing e.g. the webserver on the other end to do some spellcheck-redirects) or the other server is misconfigured or there are some security measures in place or...

If you feel the need to extend the 20 redirects you could use a stream context.

$context = array(
    'http'=>array('max_redirects' => 99)
);
$context = stream_context_create($context);
// hand over the context to fopen()
$data = file_get_contents('http://xml.gamebookers.com/sports/bandy.xml', false, $context);
// ...

Please see:

Community
  • 1
  • 1
Stefan Gehrig
  • 82,642
  • 24
  • 155
  • 189
  • Thanks Stefan i solved it by using curl with proxy options.I got the xml datas.However it is very slow.Is there any way to pass parameter to xml url ?For example:"http://xml.gamebookers.com/sports/bandy.xml?name=footbal" ?I want to use such a function in order to filter result.I dont need whole result – Hüseyin BABAL Dec 19 '10 at 15:21
  • No - at least not if the XML file is really an XML file. If it's some sort of REST endpoint (which means there's a script running behind that URL) it might be possible to pass additional parameters to the URL. If it's just a file, it'll be downloaded as a file. – Stefan Gehrig Dec 19 '10 at 16:21
1

Try the snippet: $request_url = 'http://xml.gamebookers.com/sports/bandy.xml';

$xml = simplexml_load_file($request_url) or die("feed not loading");

/* Then just parsing out child node of your xml. for example */

foreach($xml->children() as $child) { echo $child->getName().":  ".$child."
"; }

Hope this help

PS: Open your PHP.INI and look for allow_url_fopen = On // make sure it is ON

dbwebtek
  • 245
  • 3
  • 5
  • Hi dbwebtek thanks for your care but i got following error: Warning: simplexml_load_file(http://xml.gamebookers.com/sports/bandy.xml): failed to open stream: Redirection limit reached, aborting in /var/www/fbDemo/rss.php on line 13 Warning: simplexml_load_file(): I/O warning : failed to load external entity "http://xml.gamebookers.com/sports/bandy.xml" in /var/www/fbDemo/rss.php on line 13 feed not loading – Hüseyin BABAL Dec 19 '10 at 12:10
  • By the way my application is in localhost and i am using proxy.Without proxy i cannot reach that xml service from browser – Hüseyin BABAL Dec 19 '10 at 12:13
  • I used a stream like this(because i am using proxy): $default_opts = array( 'http'=>array( 'method'=>"GET", 'header'=>"Accept-language: en\r\n" . "Cookie: foo=bar", 'proxy'=>"tcp://myProxyIp:myPort" ) ); and then i use fie_get_contents() function with stream parameter and it worked.However is there any way to use stream parameter in simple_xml_load? – Hüseyin BABAL Dec 19 '10 at 12:39
  • I just tested from my end with the code snippet and get the data back with show the DATE...so I guess it must be configuaration on your machine. Mine is Ubuntu 10.04, full LAMP stacked – dbwebtek Dec 19 '10 at 19:49