I'm planning to use PHP for a simple requirement. I need to download a XML content from a URL, for which I need to send HTTP GET request to that URL.
How do I do it in PHP?
I'm planning to use PHP for a simple requirement. I need to download a XML content from a URL, for which I need to send HTTP GET request to that URL.
How do I do it in PHP?
Depending on whether your php setup allows fopen on URLs, you could also simply fopen the url with the get arguments in the string (such as http://example.com?variable=value )
Edit: Re-reading the question I'm not certain whether you're looking to pass variables or not - if you're not you can simply send the fopen request containg http://example.com/filename.xml - feel free to ignore the variable=value part
http_get
should do the trick. The advantages ofhttp_get
overfile_get_contents
include the ability to view HTTP headers, access request details, and control the connection timeout.I like using fsockopen open for this.
Unless you need more than just the contents of the file, you could use
file_get_contents
.For anything more complex, I'd use cURL.
In the other hand, using REST API of other servers are very popular in PHP. Suppose you are looking for a way to redirect some HTTP requests into the other server (for example getting an xml file). Here is a PHP package to help you:
So, getting the xml file:
Remember that if you are using a proxy you need to do a little trick in your php code: