how to get cookie value from curl response in php - php

I am calling this API by curl:
$url = '/api/auth/login';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"fbUserID\": \"$fbID\", \"fbAccessToken\": \"$Token\"}",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$jsonresult = json_decode($response);
$err = curl_error($curl);
My question is how can I get the cookie value in response header, I am testing call in POSTMAN API so I am getting cookie value.
Can you please help?

Add this code segment
$cookie=dirname(__FILE__)."/cookie.txt";
curl_setopt ($curl, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($curl, CURLOPT_COOKIEFILE, $cookie);
It will look like this finally
$cookie=dirname(__FILE__)."/cookie.txt";
$url = '/api/auth/login';
$curl = curl_init();
curl_setopt_array($curl,
array( CURLOPT_URL => $url
, CURLOPT_COOKIEJAR => $cookie
, CURLOPT_COOKIEFILE => $cookie
, CURLOPT_RETURNTRANSFER => true
, CURLOPT_ENCODING => ""
, CURLOPT_MAXREDIRS => 10
, CURLOPT_TIMEOUT => 30
, CURLOPT_CUSTOMREQUEST => "POST"
, CURLOPT_POSTFIELDS => "{\"fbUserID\": \"$fbID\", \"fbAccessToken\": \"$Token\"}"
,CURLOPT_HTTPHEADER => array(
"accept: application/json",
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$jsonresult = json_decode($response);
$err = curl_error($curl);

Related

PHP cURL request headers isn't working with a global variable

I've set a global variable to equal an access token, and when I print_($session_id) the token is visible. What I'd like to do is pass that variable into my CURLOPT_HTTPHEADER, and have it equal to "Session" in the function get_field_data_id() below:
function get_session_id() {
global $session_id;
$curl = curl_init();
$session_url = "/auth";
curl_setopt_array($curl, array(
CURLOPT_URL => $session_url ,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Accept: application/json",
"username: USER",
"password: PASS",
"authorizationkey: KEY"
),
));
$response = curl_exec($curl);
curl_close($curl);
$session_id = $response;
} add_filter( 'gform_after_submission_4', 'get_session_id' );
function get_field_data_id() {
global $session_id;
$curl = curl_init();
$file_url = "URL";
curl_setopt_array($curl, array(
CURLOPT_URL => $file_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Accept: application/json",
"Session: {$session_id}"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;}
However when I run this function, I get an error message that the session ID is invalid. What am I missing?
{"Message":"Invalid Authorization. Session ID is invalid. "}

How to get OAuth 2.0 using PHP curl with client credentials as grant_type?

What I have been trying:
$header = array(
"Authorization :Basic $authorization",
"Content-Type: application/json"
);
//option 1
$param='{
"grant_type" : "client_credentials"
}';
//option 2
$param = array("Grant_type: client_credentials",);
//option 3
$param = "grant_type=client_credentials";
//option 4
$a = array(
'grant_type' => 'client_credentials');
$param = http_build_query($a);
$ch = curl_init( $url );
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$param);
curl_setopt($ch,CURLOPT_VERBOSE, true);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,120);
curl_setopt($ch,CURLOPT_TIMEOUT,120);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
$content = curl_exec( $ch );
$err = curl_error($ch);
curl_close( $ch );
I already tried all the option above, but keep getting error like this:
response_type or grant_type is required
How to pass grant_type in curl concept? Or is it me who using it wrongly?
Found answer from this link https://auth0.com/docs/api-auth/tutorials/client-credentials
Need to add this following line tho:
CURLOPT_SSL_VERIFYHOST =>false,
CURLOPT_SSL_VERIFYPEER => false,
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://xxy",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST =>false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET",
CURLOPT_HTTPHEADER => array(
"content-type: application/x-www-form-urlencoded"
),
));

CURLOPT_HTTPHEADER does not set

Using curl to call API GET method with headers
but header not working
Response -- Failed to connect to 52.172.133.9: Permission denied
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "25002",
CURLOPT_URL => "https://api.eko.in:25002/ekoicici/v1/customers/mobile_number:8734818474?initiator_id=8734818474",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 60,
CURLOPT_HTTP_VERSION => "CURL_HTTP_VERSION_1_1",
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"developer_key: $developerKey",
"secret-key: $secret_key",
"secret-key-timestamp: $secret_key_timestamp"
)
));
$response = curl_exec($curl);
var_dump($response);
$err = curl_error($curl);
var_dump($err);
var_dump("developerKey--".$developerKey);
var_dump("secret_key--".$secret_key);
var_dump("secret_key_timestamp--".$secret_key_timestamp);
curl_close($curl);
when hitting the request on this URL it requires key that's pass on the header but the header does not set on curl.
how to fix that's a problem...

Is a variable allowed in authorization bearer in place of a token?

I have a variable in place of a token key. When I use the variable the response is blank. When I use a token key it returns a response. I've made sure that the other variables I set weren't the problem. The problem variable is the one I have labeled $token_json
My website is https://geolyft.com
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
$ltt = $_POST['lat'];
$lng = $_POST['lng'];
$token_url = "https://geolyft.com/authentication.php";
$token_json = file_get_contents($token_url);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.lyft.com/v1/drivers?lat=$ltt&lng=$lng",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer $token_json",
"cache-control: no-cache",
"postman-token: 205a9ff0-102f-7f7a-8f11-b473946cc3fd"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
</body>
</html>
The difference is here
CURLOPT_HTTPHEADER => array(
"authorization: Bearer gAAAAABYXNVPV4B9AEY2bHRZDhB_jw6-hj4ptvUttm45jqroJN0lV5QSZGd-5AL0jrVKod4R5J0Sdxam1Y2nduEO_Lvud8fFGnqAN7shjzgcl9RmLQN5WljEJ9us-Y41_qGr7HT10EH8acseSt-mdO7Dp9MeYaFMkcjIP-IdrIL9uVNll-JypwQgftZ1Bum7gCtQEgpqjGeRuwE4YwE6rLPFALUgZVWNlg==",
"cache-control: no-cache",
"postman-token: 205a9ff0-102f-7f7a-8f11-b473946cc3fd"
),
));
I would rather have this instead of a long token key but with this setup the response I get is blank and I've been stumped for a while now and I can't seem to find an answer perhaps I'm out of my league. Any help is appreciated
CURLOPT_HTTPHEADER => array(
"authorization: Bearer $token_json",
"cache-control: no-cache",
"postman-token: 205a9ff0-102f-7f7a-8f11-b473946cc3fd"
),
));
use:
CURLOPT_HTTPHEADER => array(
"authorization: Bearer ".$token_json,
"cache-control: no-cache",
"postman-token: 205a9ff0-102f-7f7a-8f11-b473946cc3fd"
),
));
The answer to my question was to put the authentication curl request before other curl requests.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.lyft.com/oauth/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"grant_type\"\r\n\r\nclient_credentials\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"scope\"\r\n\r\npublic\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
"authorization: Basic a0JFTE1Oak9QQWY2Om1VZVdwc1lRS0ExUWdfS1dnYTN1OHlHajQxQ3E4LV9X",
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
"postman-token: f5d6e662-1663-e577-2c0d-705f03275fa8"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$json = json_decode($response, true);
$token = $json['access_token'];

JSON body in a CURL POST request in PHP

Working on a CURL Post to a restful backend service. Using RESTClient extension in Firefox I get my intended response.
So I have my Authorization, Accept, and Content-Type Headers set in CURL
$ch = curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_HTTPHEADER => array('User-Agent: Mozilla Firefox', 'Accept: application/json', 'Authorization: Basic v3iYdNlAcGxlKASocYkadkCSCIDpsdkFDJ='),
CURLOPT_URL => 'http://myurl.com/restfulservice/',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $jsonpoststring,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC | CURLAUTH_ANY,
CURLOPT_USERPWD => sprintf('%s;%s', 'foo#bar.com', 'somepassword'),
CURLOPT_SSL_VERIFYPEER => false
)
);
$output = curl_exec($ch);
curl_close($ch);
In RESTClient I have this JSON in the Body
{"authentication":{"identification":{"identstring":"foo#bar.com","identId":1},"security":{"securestring":"Password1", "secureId":3}},"externIdent":{"externAuth":{"hashauth":"K3DvhinSb7H","district":"iad"}}}
My question is: How to include that body in the PHP Curl request.
Pass it in postfields
jsonpoststring='{"authentication":{"identification":{"identstring":"foo#bar.com","identId":1},"security":{"securestring":"Password1", "secureId":3}},"externIdent":{"externAuth":{"hashauth":"K3DvhinSb7H","district":"iad"}}}
';
$ch = curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_HTTPHEADER => array('User-Agent: Mozilla Firefox', 'Accept: application/json', 'Authorization: Basic v3iYdNlAcGxlKASocYkadkCSCIDpsdkFDJ='),
CURLOPT_URL => 'http://myurl.com/restfulservice/',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $jsonpoststring,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC | CURLAUTH_ANY,
CURLOPT_USERPWD => sprintf('%s;%s', 'foo#bar.com', 'somepassword'),
CURLOPT_SSL_VERIFYPEER => false
)
);
$output = curl_exec($ch);
curl_close($ch);
This should do the trick
$data = json_decode('{"authentication":{"identification":{"identstring":"foo#bar.com","identId":1},"security":{"securestring":"Password1", "secureId":3}},"externIdent":{"externAuth":{"hashauth":"K3DvhinSb7H","district":"iad"}}}');
$ch = curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_HTTPHEADER => array('User-Agent: Mozilla Firefox', 'Accept: application/json', 'Authorization: Basic v3iYdNlAcGxlKASocYkadkCSCIDpsdkFDJ='),
CURLOPT_URL => 'http://myurl.com/restfulservice/',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC | CURLAUTH_ANY,
CURLOPT_USERPWD => sprintf('%s;%s', 'foo#bar.com', 'somepassword'),
CURLOPT_SSL_VERIFYPEER => false
)
);
$output = curl_exec

Categories