Most PHP developers have heard of the
CURL extension for PHP or even used it. However it is mostly used in a basic form: to retrieve content from other websites or (RESTful) webservices. Ofcourse PHP itself offers several functions (like fopen or fsockopen) for getting this content, but they are all very basic. It is easy to run into limitations, for example you might want to define the request method or set another user agent (if you're building a webspider). This is where the curl extension kicks in. It is a separate library that has to be compiled with PHP in order to use it. The Curl extension has many functions and options which offer the developer more flexibility than the standard PHP functions.
Let me show you a simple example of using Curl to get the content of another website.
PHP:
-
-
- $ch = curl_init ();
-
-
- curl_setopt ( $ch , CURLOPT_URL , 'http://' );
- curl_setopt ( $ch , CURLOPT_TIMEOUT , 30 );
- curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , true );
-
-
- $output = curl_exec ( $ch );
-
-
- echo $output ;
-
-
- echo '
'
; - print_r ( curl_getinfo ( $ch ));
- echo '' ;
-
-
- curl_close ( $ch );
-
- ?>
A good tutorial which covers the basics of using curl can be found
here .
Besides using curl for getting the content of other websites, it is also possible to use curl for multithreading in PHP. PHP has no native support for multithreading like Java. Each PHP request is a separate thread. There are some workarounds like using pcntl_fork, starting multiple commandline php processes using the exec command or even using ajax. Another possibility is using the Curl library. Besides the basic functions described above Curl offers the "multi" functions for retrieving content from several url's at the same time. Let's take a look at these functions using an example:
PHP:
-
-
- $mh = curl_multi_init ();
- $handles = array();
-
- for( $i = 0 ; $i < 5 ; $i ++)
- {
-
- $ch = curl_init ();
-
-
-
- curl_setopt ( $ch , CURLOPT_URL , "" .( $i + 1 ));
- curl_setopt ( $ch , CURLOPT_HEADER , 0 );
- curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , true );
- curl_setopt ( $ch , CURLOPT_TIMEOUT , 30 );
-
-
- curl_multi_add_handle ( $mh , $ch );
-
-
- $handles [] = $ch ;
- }
-
-
- $running = null ;
- do
- {
- curl_multi_exec ( $mh , $running );
-
- usleep ( 250000 );
- } while ( $running > 0 );
-
-
- for( $i = 0 ; $i < count ( $handles ); $i ++)
- {
-
- $output .= curl_multi_getcontent ( $handles [ $i ]);
-
-
- curl_multi_remove_handle ( $mh , $handles [ $i ]);
- }
-
-
- echo $output ;
-
-
- curl_multi_close ( $mh );
-
-
- ?>