cURL is a PHP library which allows you to connect and communicate to many different types of servers with many different types of protocols. You can use it for php curl post method request or get method request.
Using cURL you can:
- Implement payment gateways’ payment notification scripts.
- Download and upload files or complete web page.
- Login to other websites and access members only sections.
PHP cURL library is definitely the odd man out. Unlike other PHP libraries where a whole plethora of functions is made available, PHP cURL wraps up a major parts of its functionality in just four functions.
A typical PHP cURL usage follows the following sequence of steps.
curl_init – Initializes the session and returns a cURL handle which can be passed to other cURL functions.
curl_opt – This is the main work horse of cURL library. This function is called multiple times and specifies what we want the cURL library to do.
curl_exec – Executes a cURL session.
curl_close – Closes the current cURL session.
Below are some examples which should make the working of cURL more clearer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <?php function post($Url,$post_elements){ if (!function_exists('curl_init')){ die('CURL is not installed!'); } $flag=false; $elements=''; foreach ($post_elements as $name=>$value) { if ($flag) $elements.='&'; $elements.="{$name}=".urlencode($value); $flag=true; } $ch = curl_init(); // set URL to download curl_setopt($ch, CURLOPT_URL, $Url); // set referer: curl_setopt($ch, CURLOPT_REFERER, "http://www.google.com/"); // user agent: curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0"); // remove header? 0 = yes, 1 = no curl_setopt($ch, CURLOPT_HEADER, 0); // should curl return or print the data? true = return, false = print curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // timeout in seconds curl_setopt($ch, CURLOPT_TIMEOUT, 10); //post data curl_setopt($ch, CURLOPT_POSTFIELDS, $elements); // download the given URL, and return output $output = curl_exec($ch); // close the curl resource, and free system resources curl_close($ch); // print output return $output; } ?> |
Call this function by:-
$url=”http://example.com”;
$data['post_data1']=”test”;
$data['post_data2']=”103″;
$result=post($url,$data);
Information is power and now I’m a !@#$ing dicatotr.