I am developing a web application. My main application is a REST application. Unlike traditional web development, my website connects to my rest application to POST, DELETE, PUT or GET data.
Now, when i POST my username and password to my REST application through a desktop application called Postman(https://www.getpostman.com/) and then GET the account data, it works perfectly fine and I am able to retrieve the data.
But when my website connects to my REST app through cURL library. It is not able to GET any data because the website shows not logged in.
I don't know why Postman and my website are behaving differently even though they are getting data from same REST application.
Note that the sessions are in REST application only, so my website doesnt use any native sessions.
Here are my functions in my website:
function process_api_get($base_url,$extension)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $base_url . $extension,
CURLOPT_CUSTOMREQUEST => 'GET'
));
$resp = curl_exec($curl);
curl_close($curl);
// echo '<pre>';
// print_r($resp);
// exit;
return json_decode($resp);
}
function process_api_post($input,$base_url,$extension)
{
$obj = json_encode($input);
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $obj);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0');
curl_setopt($ch, CURLOPT_URL, $base_url . $extension);
//
if (isset($_COOKIE[session_name()]))
curl_setopt($ch, CURLOPT_COOKIE, session_name().'='.$_COOKIE[session_name()].'; path=/');
session_write_close();
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
}
function process_api_put($input,$base_url,$extension)
{
$obj = json_encode($input);
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $obj);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0');
curl_setopt($ch, CURLOPT_URL, $base_url . $extension);
if (isset($_COOKIE[session_name()]))
curl_setopt($ch, CURLOPT_COOKIE, session_name().'='.$_COOKIE[session_name()].'; path=/');
session_write_close();
$result = curl_exec($ch);
curl_close($ch);
session_start();
return json_decode($result);
}
function process_api_delete($input,$base_url,$extension)
{
$obj = json_encode($input);
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_POSTFIELDS, $obj);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $base_url . $extension);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
}
Aucun commentaire:
Enregistrer un commentaire