2

I want to parse xml file from another server.

<?php

    $xml = simplexml_load_file('http://example_page.com/api/test.xml');

?>

And this code work only if this file is on this same server as page, but I have got xml file on another server.

Warning from webpage:

Warning: simplexml_load_file(http://example_page.ugu.pl/api/test.xml) [function.simplexml-load-file]: failed to open stream: Connection refused in /virtual/z/y/example_page.ugu.pl/index.php on line 14

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://example_page.com/api/test.xml" in /virtual/z/y/example_page.ugu.ugu.pl/index.php on line 14

P.S. allow_url_fopen is still on.

3 Answers 3

3

Seems like there is some redirection problem in the url you are accessing, and most probably simplexml_load_file() doesn't follow redirects...

So the solution would be to use file_get_contents() or cUrl...

As file_get_contents() is easier, I am showing that only...

The code would have to be something like:

<?php

    $xml = simplexml_load_string(file_get_contents('http://example_page.ugu.pl/api/test.xml'));

?>

More:

<?php

    $xml = simplexml_load_string(file_get_contents('http://jakdojade.pl/pages/api/outputs/schedules.xml'));

?>

^ That too, totally works!

Sign up to request clarification or add additional context in comments.

5 Comments

Using this I have got still error. Warning: file_get_contents(example_page.com/api/test.xml) [function.file-get-contents]: failed to open stream: Connection refused in /virtual/z/y/examle_page.com/index.php on line 24 P.S. I'm sure that allow_url_fopen is on, coz I checked it using phpinfo();
The reason is nothing, but the web page you are trying to access is... down... And... really tbh, did you try to access example_page.com/api/test.xml from your browser?
And so, does the code works... I tried the link you provided and it worked like a charm :)
Is there any option that hostinger.com don't support it or block it? Am I using bad hosting? Which one free hosting support it?
To recommend one, I would probably suggest you AWS... there is a free plan there....
0

You should use CURL (if is active).

$url = "http://www.you-web-here.com/";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$xmlString = curl_exec($curl);

Comments

0

PHP Documentation:

Note:
Libxml 2 unescapes the URI, so if you want to pass e.g. b&c as the URI parameter a, you have to call simplexml_load_file(rawurlencode('http://example.com/?a=' . urlencode('b&c'))). Since PHP 5.1.0 you don't need to do this because PHP will do it for you.

So. which version are you used?

Or, you can try like this:

$xml_content = file_get_content('http://example_page.com/api/test.xml');
xml = simplexml_load_string($xml_content);

1 Comment

Ah,sorry, is file_get_contents, not file_get_content, lost a s chart

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.