Sunday, December 22, 2013

Curl with headers and posting some values with it

        $email=$_REQUEST['email'];
$pass=$_REQUEST['pass'];

$fields_string="email=".$email."&password=".$pass."&isEmailVerified=false";
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
 
CURLOPT_URL => 'https://int.mylinkables.com/consumerapi/consumer',

CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$headr = array();
$accesstoken="bfcb2d9f-ab6a-41d1-82db-0a5fba85201b";
$headr[] = 'api: 1.0.0';
$headr[] = 'Accept: application/json';
$headr[] = 'Authorization: Bearer '.$accesstoken;
curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);
curl_setopt($ch, CURLOPT_POST,true);
$file_contents  = curl_exec($ch);

if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo 'Operation completed without any errors';

}


curl_close($ch);
$json = json_decode($file_contents, true);

//printing some values from json

        print '<br>';
print "statusCode=".$json[statusCode];
print '<br>';
print "consumerToken=".$consumerToken=$json[consumerToken];
print '<br>';

Using Curl With PHP

$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => 'https://int.mylinkables.com/consumerapi/consumer',
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
curl_setopt($ch, CURLOPT_TIMEOUT, 60);

//Using Header with Curl
curl_setopt($ch, CURLOPT_HEADER, true);
$headr = array();
$accesstoken="0_1c178f77-7d30-4a49-a1df-e1f9cf021d0b";
$headr[] = 'Accept: application/json';
$headr[] = 'api: 1.0.0';
$headr[] = 'Authorization: Bearer '.$accesstoken;
curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
//curl_setopt($ch, CURLOPT_POST,true);
$file_contents  = curl_exec($ch);

if(curl_exec($ch) === false)
{
    echo 'Curl error: ' . curl_error($ch);
}
else
{
    echo 'Operation completed without any errors';
}

// Close handle
curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
curl_close($ch);