By using this site you agree to the use of cookies.

Learn more

Share via:

PHP: HTTP Requests (Using the Build-in File- Functions)

How to send HTTP requests using the build-in file functions of PHP.

Edited: 2017-05-06 22:42

PHP article image

There is a few ways to perform HTTP requests in PHP, while using a library like cURL is probably one of the most popular ways to do it, you can also use functions such as file_get_contents and fopen.

In this tutorial we will explain the fopen, fread, and file_get_contents functions. While these functions, by their name, do not indicate that they can be used for HTTP requests, they do actually work quite well for this, and they are also fairly easy to use.

Another function we will be using is stream_context_create, this function is used to create the HTTP headers associated with the request.

There should be no disadvantage to using the build-in functions instead of cURL, and unlike what is often claimed, they can be used for both POST and GET requests. However, some hosting companies might disable allow_url_fopen, which will break scripts relying on these methods.

GET Requests

To perform a simple get request, we will first create a $handle variable. We can use this to reference the request when storing the downloaded data in the $contents variable using stream_get_contents.

<?php
$handle = fopen("https://beamtic.com/", "rb");
$contents = stream_get_contents($handle);
fclose($handle);
?>

XX automatically downloads a file or web page, however, if you wish to do it manually, for finer control over the process, you may use a while loop

<?php
$handle = fopen("https://beamtic.com/", "rb");
$contents = '';
while (!feof($handle)) {
  $contents .= fread($handle, 8192);
}
fclose($handle);
echo $contents;
?>

In this case the last argument in the fread function is equal to the chunk size, this is usually not larger than 8192 (8*1024). Keep in mind that this can be larger or smaller, but may also be limited by the system PHP is running on. You can optimize your PHP scripts by not using a larger chunk-size than that of your storage device.

One of the simplest ways to download a file is to use file_get_contents. It requires far less code than using the other methods, but it offers less control.

<?php
$homepage = file_get_contents('https://beamtic.com/');
echo $homepage;
?>

POST Requests

Sending a post requests is not much harder than sending get. We just have to use the stream_context_create function to add the headers required to perform the post.

Again, we can do this both with file_get_contents and fopen. But lets use file_get_contents for now:

$sURL = "https://beamtic.com/Examples/http-post.php"; // The POST URL
$sPD = "name=Jacob&bench=150"; // The POST Data
$aHTTP = array(
  'http' => // The wrapper to be used
    array(
    'method'  => 'POST', // Request Method
    // Request Headers Below
    'header'  => 'Content-type: application/x-www-form-urlencoded',
    'content' => $sPD
  )
);
$context = stream_context_create($aHTTP);
$contents = file_get_contents($sURL, false, $context);

echo $contents;

The $sURL variable contains the POST URL.

The $sPD variable contains the data you want to post. Formatting should match the content-type header.

The $aHTTP array has all of the options, including headers, which will be passed on to stream_context_create.

You can also perform post requests with the fread function.

$sURL = "http://beamtic.com/Examples/http-post.php"; // The POST URL
$sPD = "name=Jacob&bench=150"; // The POST Data
$aHTTP = array(
  'http' => // The wrapper to be used
    array(
    'method'  => 'POST', // Request Method
    // Request Headers Below
    'header'  => 'Content-type: application/x-www-form-urlencoded',
    'content' => $sPD
  )
);
$context = stream_context_create($aHTTP);
$handle = fopen($sURL, 'r', false, $context);
$contents = '';
while (!feof($handle)) {
  $contents .= fread($handle, 8192);
}
fclose($handle);

echo $contents;

Request Headers

If you got multiple headers, remember to separate them with \r\n

The content-type header tells the server how the posted data is formatted. The application/x-www-form-urlencoded content-type is often used by web applications, but you can also encounter multipart/form-data. If an application does not support the content-type you specify, the POST will usually just fail.

The content header contains the actual data that you want to post. This should be formatted in accordance with the content-type you are using.

Alternatively, you may also add options and headers as shown below, this time adding a referer header:

$aHTTP['http']['method']  = 'GET';
$aHTTP['http']['header']  = "User-Agent: My PHP Script\r\n";
$aHTTP['http']['header'] .= "Referer: https://beamtic.com/\r\n";

Additional request headers will be added in the same way, by adding a single Carriage return "\r" and line feed "\n" at the end of each header, followed by whatever header you want to add. Once you have added all the headers, you should end up with something like the below (full script):

$sURL = "https://beamtic.com/Examples/http-request-headers.php"; // The POST URL

$aHTTP['http']['method']  = 'GET';
$aHTTP['http']['header']  = "User-Agent: My PHP Script\r\n";
$aHTTP['http']['header'] .= "Referer: https://beamtic.com/\r\n";

$context = stream_context_create($aHTTP);
$contents = file_get_contents($sURL, false, $context);

echo $contents;