php - Can't load file from URL, but works with local path
Get the solution ↓↓↓I am trying to load an XML file, but for some reason I can't use a URL.
allow_url_fopen
is set toOn
.
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 triedfile_get_contents
:
$xml = file_get_contents("https://example.corn/test.xml");
and evencURL
:
$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 withvar_dump($xml);
, which always showsbool(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 samephp.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?
Answer
Solution:
Turns out that I had myopenssl.cafile
property set to some location that didn't exist.
For some reason no errors were showing when using the commands above.
Answer
Solution:
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);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: invalid argument supplied for foreach() laravel
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.