Friday, 24 October 2014

What is CURL in PHP?

Why we need PHP CURL ?
To send HTTP GET requests, simply we can use file_get_contents() method.

file_get_contens('http://hayageek.com')
But sending POST request and handling errors are not easy with file_get_contents().

Sending HTTP requests is very simple with PHP CURL.You need to follow the four steps to send request.
step 1). Initialize CURL session
1
$ch = curl_init();
step 2). Provide options for the CURL session
1
2
3
curl_setopt($ch,CURLOPT_URL,"http://hayageek.com");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, true); //if you want headers
CURLOPT_URL -> URL to fetch
CURLOPT_HEADER -> to include the header/not
CURLOPT_RETURNTRANSFER -> if it is set to true, data is returned as string instead of outputting it.

step 3). Execute the CURL session
1
$output=curl_exec($ch);
step 4). Close the session

curl_close($ch);
Note: You can check whether CURL enabled/not with the following code.




$ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
//  curl_setopt($ch,CURLOPT_HEADER, false);
    $output=curl_exec($ch);
    curl_close($ch);
    return $output;

No comments:

How calulation total value for HTML input text?

<script> $j(document).ready(function(){ $j(":text").keyup(function(){ if (isNaN($j(this).val())) { alert(...