PHP curl POST token in variable - php

I can't figure out how to store some data in a variable and put it in the request header.
CURLOPT_URL => 'https:url_for_request',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{"_cloudId": 99999}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Accept: application/json',
'Authorization: User {token}'
),
));
I need to have the token in the $token variable but I can't figure it out.

Related

PHP CURL Req. Json Format And Using Variables

How to use variable on postfields json type on php ?
I set deger variable 123 but curl don't send this data please help me
<?php
$deger=123;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://serviceurl',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"iprocessnumber": "$deger",
"activationStatus": "SUCCESS",
"code": "100",
"message": "Test mesajı"
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
You could use that CURLOPT_POSTFIELDS to be pointed to a JSON encoded array.
$postFields holds the encoded fields.
<?php
$deger=123;
$curl = curl_init();
$postFields= [
"iprocessnumber"=>$deger,
"activationStatus"=>"SUCCESS",
"code"=>"100",
"message"=>"Test mesajı"
];
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://serviceurl',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>json_encode($postFields),
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
Hope this helps.

PHP redirect not working when receiving a redirect URL

So I have the code below, and it does the following:
request a key via a curl command
the key is then send back to another URL -
this command then returns a URL which I am trying to redirect too.
When it attempts to redirect, I get the following error : Warning: Header may not contain more than a single header, new line detected in C:\xampp\htdocs\blahblah\testapi.php on line 50
var_dump returns: string(39372) "https://pay.testdomain.com/eft?payment_key=0b1f024ccac63c7ce19ffad8b53a8d32"
<?php
ob_start();
#Get key
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://pay.testdomain.com/api/login-admin',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('email' => 'user#user.com','password' => 'mypassword'),
CURLOPT_HTTPHEADER => array(
'Accept: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
$data = json_decode($response,true);
$key = $data['data']['token'];
#Get redirect URL
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://pay.testdomain.com/api/payment-redir',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('amount' => '100.5555','merchant_ref' => '2222','payment_method' => 'eft'),
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer ' . $key
),
));
$response = curl_exec($curl);
curl_close($curl);
header("Location: " . $response );
exit;
ob_end_flush();

How to pass access token stored in variable in PHP to an array

I was able to find the access token from Azure
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://login.microsoftonline.com/12baf------------/oauth2/token',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'grant_type=client_credentials&client_id=XXXXX-57b4-XXXXb-XXXX-13ca1f5eec6e&client_secret=XXXXX&resource=https%3A%2F%2Fmanagement.azure.com%2F',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded',
'Cookie: fpc=AlXXXXXXXXpKm43KbQ52MOkk8jRRAQAAAI8Us9cOAAAA; stsservicecookie=estsfd; x-ms-gateway-slice=prod'
),
));
$json = curl_exec($curl);
$data=json_decode($json,true);
curl_close($curl);
$token=$data['access_token'];
But when I am trying to pass the variable in CURLOPT_HTTPHEADER it's showing an invalid token.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://management.azure.com/subscriptions/XXXXXXX-1faf-4756-a709-1af49be58e56/resourcegroups?api-version=2020-06-01',
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(
'Authorization: Bearer '$token'
),
));
$response = curl_exec($curl);
echo $response
?>
How can I pass the token in the above array or there is an easy way to do it? Can anyone please help here.
I able to resolve it as below
$authorization = "Authorization: Bearer ".$token;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://management.azure.com/subscriptions/xxxxxxx-4756-a709-1af49be58e56/resourcegroups?api-version=2020-06-01',
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' , $authorization)
),
);
$response = curl_exec($curl);
echo $response
?>

PHP cURL not return a response, POSTMAN returns response

I am running a cURL request that looks like this,
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
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(
"Content-Type: application/json",
"Authorization: Bearer " . $token
)
));
$response = curl_exec($curl);
die(var_dump($response));
Would there be any reason for the my cURL in PHP not returning a response?

php pass var to curl request

I'm having a bit of an issue with curl. It simply wont take $variables.. Kindly assist. The rest of the curl request is correct and I've managed to isolate it
Here is what I've tried
CURLOPT_POSTFIELDS => "{\"msisdn\":$msisdn, \"transactionId\": $_id, \"transactionAmount\": $amount}",
$payload = json_encode(array("msisdn"=> $s_msisdn,"transactionId"=>$_id, "transactionAmount"=>$_amount));
...
CURLOPT_POSTFIELDS => $payload,
EDIT: Full request
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "5001",
CURLOPT_URL => "http://123.123.23.11:5001/demo",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"msisdn\":"'.$_msisdn.'", \"transactionId\": "'.$_id.'", \"transactionAmount\": "'.$_amount.'"}",
CURLOPT_HTTPHEADER => array(
"authorization: Basic abcdefgh",
"content-type: application/json",
),
));
Here is what you can use. Your JSON string is not got the correct quotations. I personally find it easier to use an array then json_encode it, it saves having to worry about escaping variables.
$data = array (
"msisdn" => $_msisdn,
"transactionId" => $_id,
"transactionAmount" => $_amount,
);
$jsonData = json_encode($data);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "5001",
CURLOPT_URL => "http://123.123.23.11:5001/demo",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $jsonData,
CURLOPT_HTTPHEADER => array(
"authorization: Basic abcdefgh",
"content-type: application/json",
),
));
This is more closer to your code with the JSON fixed.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "5001",
CURLOPT_URL => "http://123.123.23.11:5001/demo",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"msisdn\": \"{$_msisdn}\", \"transactionId\": \"{$_id}\", \"transactionAmount\": \"{$_amount}\"}",
CURLOPT_HTTPHEADER => array(
"authorization: Basic abcdefgh",
"content-type: application/json",
)
));

Categories