4 Ways To Make Remote Requests In PHP

Making remote requests is an important aspect of web applications and nowadays most web applications work entirely through remote calls to external Apis. Remote requests is useful when you need to fetch data from external sources like for example the Facebook, Twitter, and Google login that you see in most websites works through remote http calls to external servers and there is many many more of these services.

PHP has alot of ways to make remote requests some of these is limited to simple cases but there are others that can be used to not only fetch data but to send special headers along with the request.

 

1. Using file_get_contents():

with file_get_contents() function you can fetch contents of any file located locally and can be used also to grab contents of external file. file_get_contents() takes the name of the file or url and returns the file contents as a string.

In the above code we passed file_get_contents() the url of external json webservice and returns the contents as a string.

 

2. Using fopen():

In contrast with file_get_contents() that read the whole file and stores it as string . fopen() can be used to open a file and read it in chunks this is useful in large files that need to be processed line by line. but note here that allow_url_fopen directive must be on for this to work.

As you see in the above we checked if fopen() is able to read the file and then iterate the file contents and read it with fgets() so we supplied the number of characters as the second parameters to fgets() until we read the whole file.

 

3. Using cURL:

The previous two methods of getting data is limited to certain requests but if you need the flexibility in remote calls you should use php cURL.

Before you work with cURL check your phpinfo if cURL is installed and enabled just create a new php file with the following contents:

Search for cURL if it is installed then you are ready to go.

cURL allows transfer of data across a wide variety of protocols, and is a very powerful system. It’s widely used as a way to send data across websites, including things like API interaction and oAuth.

The basic idea behind the cURL functions is that you initialize a cURL session using the curl_init(), then you can set all your options for the transfer via the curl_setopt(), then you can execute the session with the curl_exec() and then you finish off your session using the curl_close()

 

In the above examples we create two requests the first makes a GET request while the second make a POST request. As you see first we Initialized a curl session with curl_init() then Set various options for the session, then execute and close. there are a lot of options that you can deal with refer to the curl documentation at php.net.

 

4. Using Guzzlephp:

Guzzlephp is another powerful object oriented library built on top of cURL designed for making remote requests and that can make all types of requests including restful calls (GET, POST, PUT, PATCH, DELETE).

Requirements:

  • PHP 5.5.0
  • To use the PHP stream handler, allow_url_fopen must be enabled in your system’s php.ini
  • To use the cURL handler, you must have a recent version of cURL >= 7.19.4 compiled with OpenSSL and zlib.

Installation

you can install guzzlephp with composer as follows:

After installing, you need to require Composer’s autoloader:

You can send requests with Guzzle using a GuzzleHttp\ClientInterface object.

Creating a Client

Clients are immutable in Guzzle 6, which means that you cannot change the defaults used by a client after it’s created.

The client constructor accepts an associative array of options:

base_uri

(string|UriInterface) Base URI of the client that is merged into relative URIs. Can be a string or instance of UriInterface. When a relative URI is provided to a client, the client will combine the base URI with the relative URI using the rules described in RFC 3986, section 2.

 

Sending Requests

Magic methods on the client make it easy to send synchronous requests:

Using Responses

You can get the status code and reason phrase of the response:

You can retrieve headers from the response:

The body of a response can be retrieved using the getBody method. The body can be used as a string, cast to a string, or used as a stream like object.

There are a lot of things you can do with guzzlephp including fetching data and uploading files refer to guzzlephp documentation for full list of options.

Share this: