Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a GET request from PHP?

Tags:

http

php

get

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?

like image 679
Veera Avatar asked Jun 06 '09 05:06

Veera


People also ask

Can you send () GET request?

Yes, you can send any HTTP headers with your GET request. For example, you can send user authentication data in the Authorization header, send browser cookies in the Cookie header, or even send some additional details about your request in custom headers like X-Powered-By or X-User-IP.

How do I send a POST request using PHP?

php $url = "https://reqbin.com/echo/post/form"; $curl = curl_init($url); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $headers = array( "Content-Type: application/x-www-form-urlencoded", ); curl_setopt($curl, CURLOPT_HTTPHEADER, $ ...

What is get post and request in PHP?

The POST method transfers information via HTTP headers. The information is encoded as described in case of GET method and put into a header called QUERY_STRING. The POST method does not have any restriction on data size to be sent. The POST method can be used to send ASCII as well as binary data.


2 Answers

Unless you need more than just the contents of the file, you could use file_get_contents.

$xml = file_get_contents("http://www.example.com/file.xml"); 

For anything more complex, I'd use cURL.

like image 53
Sasha Chedygov Avatar answered Oct 22 '22 23:10

Sasha Chedygov


For more advanced GET/POST requests, you can install the CURL library (http://us3.php.net/curl):

$ch = curl_init("REMOTE XML FILE URL GOES HERE"); // such as http://example.com/example.xml curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, 0); $data = curl_exec($ch); curl_close($ch); 
like image 38
James Skidmore Avatar answered Oct 22 '22 21:10

James Skidmore