Tuesday 17 September 2013

CURL using fetch values from other sites

<?php
// Using username and password fetch values from third party site
//Sample Program for fetch values from another URL
$data_login = array("username" => "aaaa","password"=>"bbb");//SignIn using username and passsword                                                                    
$data_user = json_encode($data_login);                                                                                  
$ch_login = curl_init('http://www.abc.com/sigin');//SignIn URL                                                                     
curl_setopt($ch_login, CURLOPT_CUSTOMREQUEST, "POST");                                                                    
curl_setopt($ch_login, CURLOPT_POSTFIELDS, $data_user);                                                                 
curl_setopt($ch_login, CURLOPT_RETURNTRANSFER, true);                                                                     
curl_setopt($ch_login, CURLOPT_HTTPHEADER, array(                                                                         
    'Content-Type: application/json',                                                                               
    'Content-Length: ' . strlen($data_user))                                                                      
);                                                                                                                  
$session_value = curl_exec($ch_login);
$session_value = json_decode($session_value);//Split json values
$session_value = $session_value->sessionId; //store session values
$data_values = array("sessionId" => $session_value ,"sku"=>"1");//parameter passing                                                                   
$data_string = json_encode($data_values);                                                                                  
$ch = curl_init('http://www.abc.com/Items');//Item URL                                                                     
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                    
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                     
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                         
    'Content-Type: application/json',                                                                               
    'Content-Length: ' . strlen($data_string))                                                                      
);                                                                                                                  
echo $result = curl_exec($ch); // output
?>