PHP has been gifted with a CURL library. But most of the php developers new in the community are not aware of the power of CURL.
Before I show you with some code for which you must be very anxious about, let us go through with some of the benefits whch are provided by CURL to php community.
1. You can easily get file content form a Remote Host.
2. Seamlessly integrate you script with an API and fetch values/result from API.
3. Send / Post values to most popular CRM system like Salesforce
4. Payment Gateway integration. Integrate with most popular gateway Authorize.Net
5. And may more…
So what is the code for CURL-
In the code below I have taken a case that a user login is required to be authenticated from a remote website.
Our PHP code has a username and password filled in by a user and posted to a file where our CURL code will authenticate the username and password.
//Check user login through a remote API
$api_url = “https://ssl.somedomain.com/sserv/login.jsp”;
$post_values = array
“Id” => “110063″,
“login_user” => “firstname.lastname@example.org”,
“login_password” => “userpass”
$fields = “”;
foreach( $post_values as $key => $value ) $fields .= “$key=” . urlencode( $value ) . “&”;
$ch = curl_init($sf_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, rtrim( $fields, “& ” ));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$api_reponse = curl_exec($ch);
Similarly you can send values to the payment gateway and receive a response form the gateway for the user transaction.
Simple. Were you expecting something more?