Click here to Skip to main content
14,838,368 members
Articles / Programming Languages / PHP
Tip/Trick
Posted 22 Aug 2015

Tagged as

Stats

18.1K views
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

Introduction

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.

Background

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 cubebackup.com/support.php .

  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.

PHP
// using file_get_contents to submit a support ticket 

   $post_array = array (
       "email" => "someone@gmail.com",
       "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('http://www.cubebackup.com/ticketsubmit.php', false, $context);

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

 

  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. 

PHP
// using CURL lib to do the same job

 $post_array = array (
      "email" => "someone@gmail.com",
      "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, 'http://www.cubebackup.com/ticketsubmit.php');
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
 curl_setopt($ch, CURLOPT_POST, TRUE);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

 $res = curl_exec($ch);
 curl_close($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.

PHP
  //PECL_HTTP implemention
 
  $problemDesc="error happend on backing up Google Drive files... bla bla bla";
  $post_string = "email=someone@gmail.com&problem=".$problemDesc;

  $res = http_post_data('http://www.cubebackup.com/ticketsubmit.php', $post_string); 
  if ($res === FALSE) { 
    // ... }
  else {
    //...
  }

Conclusion:

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.

License

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

Share

About the Author

jerry_wangjh
Software Developer (Senior) CubeBackup.com
China China
A coder of C++, C#, Java and Web.
I am the chief author of GDocsDrive - a Google Docs client.
Currently I am working on CubeBackup, which is a Google Apps data backup solution.

Comments and Discussions

 
-- There are no messages in this forum --