skip to Main Content

I am trying to load an XML file, but for some reason I can’t use a URL.

allow_url_fopen is set to On.

I used this, which didn’t work:

$xml = simplexml_load_file("https://example.corn/test.xml");

But this does work:

$xml = simplexml_load_file("../test.xml");

I also tried file_get_contents:

$xml = file_get_contents("https://example.corn/test.xml");

and even cURL:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.corn/test.xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xml = curl_exec($ch);          
curl_close($ch);

For each example I am displaying the output with var_dump($xml);, which always shows bool(false) when using a URL and shows the correct XML data when using a local path.

I would just use the local path, but this is just for a test and the actual XML file I am going to fetch is from a different site.

I tested it on a local server and it worked, but on my hosted server, it doesn’t work at all. They are using the same php.ini. My hosted server is using php-fpm, I’m unsure if that could be a factor, or if there is something that has to be changed with it.

This might be a configuration error that I am unaware of, because the code should work, and the link definitley exists, because I can click on it and see the correct xml in the browser.

What could be causing this issue?

2

Answers


  1. Chosen as BEST ANSWER

    Turns out that I had my openssl.cafile property set to some location that didn't exist.

    For some reason no errors were showing when using the commands above.


  2. I think the specified url is not valid or doesn’t exist

    • Read about curl in the PHP docs

       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
       curl_setopt($ch, CURLOPT_HEADER, 0);
       curl_exec($ch);
       curl_close($ch);
      
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search