Click here to Skip to main content
15,390,185 members
Articles / Programming Languages / PHP
Posted 22 Aug 2015

Tagged as


2 bookmarked

Comparison of the HTTP libs in PHP: file_get_contents vs. cURL vs. PECL_HTTP

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
22 Aug 2015CPOL3 min read
In this article, we compared different HTTP libs in PHP, and how to use them


There are several ways in PHP for sending HTTP requests or receiving HTTP responses. The most commonly used methods are:

  1. File Stream:  using  file_get_contents() PHP function.  This function is available in any versions of PHP.
  2. cURL:  cURL/libcurl is a third party lib, which has been added to PHP after the version 4.0.2
  3. PECL_HTTP extension:  PECL extension must be installed before use.


PHP programmers may be confused by all kinds of different HTTP libs:   What's the differences among them? Which one should be chosen in my code?  Actually, when I first started programming on HTTP communications in PHP,  quite a lot of time was cost on digging and comparing these different methods.   So, I want to share my experience and understanding on HTTP coding in this article.

Comparison on HTTP libs

We will compare these three methods in a real example - simulating a support ticket submitting on .

  1.  File Stream method.

file_get_contents function is originally desined for reading the contents of a file into a string. However, it also supports HTTP protocol for sending GET/POST requests.  

It's very convenient and simple to use this method for simple HTTP manipulations. However, due to its simplicity, it is not suitable for complex HTTP communications, such as breakpoint continual transferring, uploading/downloading progress reporting, callback functions, etc.

// using file_get_contents to submit a support ticket 

   $post_array = array (
       "email" => "",
       "problem" => "error happened on backing up Google Drive files...bla bla bla"

   $post_string = http_build_query($post_array);

   $opts = array(
       'http' => array(
           'method' => "POST",
           'header' => "Content-Type: application/x-www-form-urlencoded",
           'content'=> $post_string

   $context = stream_context_create($opts);
   $result = file_get_contents('', false, $context);

   if ($result === FALSE) {
    //  ...
    //  ...


  1. Using curl lib

CURL/libcurl is probably the most widely-used HTTP lib in the programming world.  It was originally coded using C language, and then was ported to a lot of other languages.  I have been using libcurl in C++ heavily for quite some time, and it never fails me!  

No matter in functionalities, performances or coding style, CURL can always meet you expectations. Especially, CURL can handle complicated HTTP manipulations elegantly, such as asynchronous HTTP requests, progress reporting, and so on.

The only problem of curl/libcurl I can think of is it requires some time, several hours at least, to get familiar with its functions and coding style. 

// using CURL lib to do the same job

 $post_array = array (
      "email" => "",
      "problem" => "error happened on backing up Google Drive files...bla bla bla"

 $post_data = http_build_query($post_array);

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, '');
 curl_setopt($ch, CURLOPT_POST, TRUE);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

 $res = curl_exec($ch);

 if ($res === FALSE)  {
   // ... 
 } else {
   // ...
  1. Using PECL_HTTP extension

Actually, PECL_HTTP extension is also based on libcurl. It is just a wrapper of libcurl using PECL extension.  It tried to simplify some operations in curl/libcurl and be more friendly to programmers.  

In most cases, comparing with CURL, it is easier to use PECL extension on HTTP requests/responses. However, there is a big problem in PECL_HTTP extension - it isn't included in most PHP or Linux environments. Which means that you have to download and install this extension manually, and then recompile the PHP source code!  Usually, this is to cumbersome and terrifies most its users.  

Another problem I've found is that PECL_HTTP version 2.0 is not compatible with version 1.0, and its programming style has completly changed.  Obviously, it is still not mature enough for production use.

  //PECL_HTTP implemention
  $problemDesc="error happend on backing up Google Drive files... bla bla bla";
  $post_string = "".$problemDesc;

  $res = http_post_data('', $post_string); 
  if ($res === FALSE) { 
    // ... }
  else {


CURL is still the best HTTP lib in PHP, and it can be used for coding any HTTP related program.

file_get_content() can be the first choice for simple HTTP POST/GET requests,  but it is not suitable for complex HTTP coding.

PECL_HTTP extension looks simpler and more elegant, but it is nor mature and lack of documents.  What's more, it is not available in some environment.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
China China
A coder of C++, C#, Java, and Web.
I work for CubeBackup (, which is a Google Workspace data backup solution.

Comments and Discussions

-- There are no messages in this forum --