How to send a GET request from PHP?

PhpHttpGet

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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);

Solution 3 - Php

http_get should do the trick. The advantages of http_get over file_get_contents include the ability to view HTTP headers, access request details, and control the connection timeout.

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

Solution 4 - Php

Remember that if you are using a proxy you need to do a little trick in your php code:

(PROXY WITHOUT AUTENTICATION EXAMPLE)

<?php
$aContext = array(
    'http' => array(
        'proxy' => 'proxy:8080',
        'request_fulluri' => true,
    ),
);
$cxContext = stream_context_create($aContext);

$sFile = file_get_contents("http://www.google.com", False, $cxContext);

echo $sFile;
?>

Solution 5 - 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

Solution 6 - Php

Guzzle is a very well known library which makes it extremely easy to do all sorts of HTTP calls. See https://github.com/guzzle/guzzle. Install with composer require guzzlehttp/guzzle and run composer install. Now code below is enough for a http get call.

$client = new \GuzzleHttp\Client();
$response = $client->get('https://example.com/path/to/resource');

echo $response->getStatusCode();
echo $response->getBody();

Solution 7 - Php

I like using fsockopen open for this.

Solution 8 - Php

On the other hand, using the REST API of servers is very popular in PHP. You can suppose all URLs are parts of a REST API and use many well-designed PHP packages.

Actually, REST API is a way to use services from a site.

So, there are many PHP packages developed to simplify REST API call. For example here is a very nice one:

https://github.com/romanpitak/PHP-REST-Client

Using such packages helps you to fetch resources easily.

So, getting the xml file (that you mentioned about) is as easy as:

$client = new Client('http://example.com');
$request = $client->newRequest('/filename.xml');
$response = $request->getResponse();
echo $response->getParsedResponse();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVeeraView Question on Stackoverflow
Solution 1 - PhpSasha ChedygovView Answer on Stackoverflow
Solution 2 - PhpJames SkidmoreView Answer on Stackoverflow
Solution 3 - PhpWilliam BrendelView Answer on Stackoverflow
Solution 4 - PhppepeView Answer on Stackoverflow
Solution 5 - PhpZxaosView Answer on Stackoverflow
Solution 6 - PhpMark BaaijensView Answer on Stackoverflow
Solution 7 - PhppbreitenbachView Answer on Stackoverflow
Solution 8 - PhpMostafaView Answer on Stackoverflow