Take the 2-minute tour ×

I am using the PHP and parsing url content from web and I know two method are there for that file_get_contents(url) and curl. I know we have more option with curl so i am using that But i just wanted to know the process behind the curl. How it works when we make a curl request from scrach in brief

share|improve this question
did you try anything ? –  user1614526 Sep 6 '12 at 5:52
here is a way to do it with file_gets_content stackoverflow.com/a/2445332/560299 –  Ibu Sep 6 '12 at 5:55
I tried both the things but now i am using curl only because we are posting some variable to url at the time of parsing –  vishalg Sep 6 '12 at 5:56
ok wait i am posting –  user1614526 Sep 6 '12 at 5:58
It uses the libcurl library, which is a very powerful library for making HTTP requests and processing the responses. It's the library that goes with the curl command from the shell. For extensive details see curl.haxx.se/libcurl –  Barmar Sep 6 '12 at 6:01

3 Answers 3

up vote 2 down vote accepted

The PHP cURL package just exposes the cURL/libcurl API (which are written in C) in PHP. cURL is really useful for moving data across all sorts of protocols, and has a lot of nice options. On the other hand, file_get_contents is one of the base PHP file operations, and it relies on the kernel to try to find the resource requested. In general, cURL will be a better choice, though often it takes a few more lines of code. One issue with file_get_contents() is that in some cases the connection is left open after the request is made, so the function call will block the script until the request times out and you may see a lot of lag.





share|improve this answer




//set POST variables
$fields = array(

//url-ify the data for the POST

//open connection
$ch = curl_init();


//execute post
$result = curl_exec($ch);

//close connection

For more understanding use following references:

  1. Curl Introduction with exmples

  2. Using cURL to automate HTTP jobs

  3. Curl exmples

may this help you.

share|improve this answer

Curl basically use to make a REST request .

here is the simple example for post request using curl

        curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
        $buffer = curl_exec($curl_handle);
        $getit =  json_decode($buffer, true);

now the first line of the code is use to initialize the curl in second line we are defining the remote url $GLOBAL_SMS_URL(in my case )

third line i am defining the timeout in seconds

i am passing the headers in 4th line

one important thing if you want to pass the curl body use this

 curl_setopt($curl_handle, CURLOPT_POSTFIELDS,$json);

where $json will contain your curl request body

or if you want to pass some parameter along url

        $data = array(
                "Username" => "56y5768",
                "Pwd" => "tr54656y",
                "PhoneNumber" => $phone,
                "PhoneMessage" => $text
        $getdata =  http_build_query($data) . "\n";
        $GLOBAL_SMS_URL = $SMS_API_BASE_URL.$getdata;

hope it helps

share|improve this answer

Your Answer


