how to use Curl in php

cURL is a command-line application for performing requests using a variety of protocols including HTTP. cURL is often used by developers to test Google Data services(cURL is interact with google services), as it supports the HTTP functionality required to interact with the APIs.

how to set header Curl in php:-curl_setopt($ch,CURLOPT_HTTPHEADER,array('HeaderName: HeaderValue'));

code for post:

<?php

$data = array(‘custumer_name’ => ‘John’,
’email’ => ‘john@example.com’,
‘mobile_number’ => ‘1234567890’,
);
curlUsingPost(‘http://localhost/yii/blog/index.php/api/requests’, $data);
$data;
$url = ‘http://localhost/yii/blog/index.php/api/requests’;

?>

<?php

function curlUsingPost($url, $data)
{

if(empty($url) OR empty($data))
{
return ‘Error: invalid Url or Data’;
}
//url-ify the data for the POST
$fields_string = ”;
foreach($data as $key=>$value) { $fields_string .= $key.’=’.$value.’&’; }
$fields_string = rtrim($fields_string,’&’);

//die(‘error’);
//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($data));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10); # timeout after 10 seconds, you can increase it
curl_setopt($ch,CURLOPT_HTTPHEADER,array(‘X_REQUES_USERNAME: demo’,’X_REQUES_PASSWORD: demo’));
// curl_setopt($ch,CURLOPT_HEADER,true);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); # Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_USERAGENT , “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)”); # Some server may refuse your request if you dont pass user agent

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

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

//close connection
curl_close($ch);
return $result;
}?>

 

contact us