4

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

vishalg
  • 417
  • 2
  • 7
  • 18
  • here is a way to do it with file_gets_content http://stackoverflow.com/a/2445332/560299 – Ibu Sep 06 '12 at 05: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 06 '12 at 05:56
  • 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 http://curl.haxx.se/libcurl/ – Barmar Sep 06 '12 at 06:01

3 Answers3

4

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.

References:

http://php.net/manual/en/book.curl.php

http://en.wikipedia.org/wiki/CURL

http://www.php.net/manual/en/ref.filesystem.php

Sam Grondahl
  • 2,049
  • 1
  • 18
  • 25
3

curl is a client to get documents/files from or send documents to a server, using any of the supported protocols (HTTP, HTTPS, FTP, GOPHER, DICT, TELNET, LDAP or FILE). The command is designed to work without user interaction or any kind of interactivity.

curl offers a busload of useful tricks like proxy support, user authentication, ftp upload, HTTP post, SSL (https:) connections, cookies, file transfer resume and more.

Example:

//set POST variables
$url = 'http://domain.com/get-post.php';
$fields = array(
            'lname'=>urlencode($last_name),
            'fname'=>urlencode($first_name),
            'title'=>urlencode($title),
            'company'=>urlencode($institution),
            'age'=>urlencode($age),
            'email'=>urlencode($email),
            'phone'=>urlencode($phone)
        );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

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

//close connection
curl_close($ch);

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.

Community
  • 1
  • 1
Tony Stark
  • 7,818
  • 8
  • 41
  • 63
1

Curl basically use to make a REST request .

here is the simple example for post request using curl

        $curl_handle=curl_init();
        curl_setopt($curl_handle,CURLOPT_URL,$GLOBAL_SMS_URL);
        curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,20);
        curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
        $buffer = curl_exec($curl_handle);
        curl_close($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

user1614526
  • 452
  • 1
  • 4
  • 13