Connect to API that needs an access token with PHP (without Laravel) - php

I'm trying to access to an API that needs and Access Token but I don't know how to implement the token part. This is how I connect to the API. What is wrong in my code?
$ch = curl_init();
$authorization = 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJub25jZSI6Im5aTHhod...';
$headers = array(
'Content-type: application/json',
$authorization,
);
curl_setopt($ch, CURLOPT_URL, "https://graph.microsoft.com/v1.0/me/onenote/notebooks");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$res = curl_exec($ch);
curl_close($ch);
echo $res;

Just a matter of constructing the header, give this a try:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://graph.microsoft.com/v1.0/me/onenote/notebooks',
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 eyJ0eXAiOiJKV1QiLCJub25jZSI6Im5aTHhod'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

Related

CURL Post request yields in error '{"error":"invalid_request"}' (length=27)

CURL Post request yields in error '{"error":"invalid_request"}' (length=27)
here is my code
$url="https://...................";
$authorization=""; //base64 string TE1TQXBpOkxNUContinue...=
$curl = curl_init();
$auth_data = array(
'scope' => 'LMSApi LMSRead',
'grant_type' => 'client_credentials'
);
$ops=array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST =>false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_POSTFIELDS=>$auth_data,
CURLOPT_HTTPAUTH=> CURLAUTH_BASIC,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
'Content-Type'=> 'application/x-www-form-urlencode',
'Authorization'=> 'Basic '.$authorization
)
);
curl_setopt_array($curl,$ops);
$response = curl_exec($curl);
var_dump($response);
curl_close($curl);
when using postman it works but returns always false while using above code. don't know what I am doing wrong.curl_error($ch) results in empty string
help is required
if someone have the same problem, following is the solution.
passing 'grant_type=client_credentials&scope=specify scope' directly to CURLOPT_POSTFIELDS instead of an array solved my problem. the code now looks like as bellow.
$url="https://something.com";
$headerpost=array(
'Content-Type: application/x-www-form-urlencoded',
'Authorization: Basic yourbase64string'
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials&scope=LMSApi LMSRead');
curl_setopt($ch, CURLOPT_HTTPHEADER,$headerpost);
$result = curl_exec($ch);
$jsonresult=json_decode($result,true);
curl_close($ch);

Send data to api

Im need to send a Username and password to api, but y cant connect, i tried in postman and is fine, i need send a json like this
Json
i have this code(sorry is the first time i tried to send data to api)
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://192.168.0.2/apilocation',
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 =>'{
"username" : "oswaldo",
"password" : "1"
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_setopt($response, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($response, CURLOPT_RETURNTRANSFER, true);
curl_setopt($response, CURLOPT_RETURNTRANSFER, TRUE);
curl_close($curl);
var_dump($response);
But is returning false :(, please help
I think data is should be like this
CURLOPT_POSTFIELDS => json_encode(array("username" => "", "password" => ""))
Also you can try to add this header
'accept: application/json',
maybe a cleaner way
$ch = curl_init("http://192.168.0.2/apilocation");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array("username" => "", "password" => "")));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);

KOHA cURL PHP API Implementation

Trying to use cURL in interfacing a PHP application with Koha, is there a cURL approach on doing this?
Tried the following;
<?php
$url = "http://opac.test.com/api/v1/oauth/token";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "User:Password");
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
echo $data;
curl_close($ch);
?>
I got a Bye response.
This was my solution, to anyone who may encounter the same problem:
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "yourdamain/api/v1/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 => "grant_type=client_credentials&client_id=yourcliedid&client_secret=yoursecretkey",//these you generate from your koha account
CURLOPT_HTTPHEADER => [
"content-type: application/x-www-form-urlencoded"
],
]);
$response = json_decode(curl_exec($curl));
$err = curl_error($curl);
curl_close($curl);
$token = $response->access_token;
echo "<pre>";
print_r($token);
echo "</pre>";
?>

How do I pass my generated API token as my authorization bearer

I have trying this all day, but couldn't find a way out.
I have two codes; the first one generates an AccessToken that will be used to call an API. I also parsed the Json output so that, I can have only the AccessToken parameter displayed... Here's the code;
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://sandbox.monnify.com/api/v1/auth/login/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Authorization: Basic TUtfVEVTVF9TQUY3SFI1RjNGOjRTWTZUTkw4Q0szVlBSU0JUSFRSRzJOOFhYRUdDNk5M"
));
$response = curl_exec($ch);
$token = json_decode($response, true);
$at = $token['responseBody'];
echo $at['accessToken'];
curl_close($ch);
The above code outputs the token.
Now, this is the code that sends the request below;
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://sandbox.monnify.com/api/v1/bank-transfer/reserved-accounts/ogmic1",
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 eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsibW9ubmlmeS1wYXltZW50LWVuZ2luZSJdLCJzY29wZSI6WyJwcm9maWxlIl0sImV4cCI6MTYwMTE0MDA0MywiYXV0aG9yaXRpZXMiOlsiTVBFX1JFVFJJRVZFX1JFU0VSVkVEX0FDQ09VTlQiLCJNUEVfREVMRVRFX1JFU0VSVkVEX0FDQ09VTlQiLCJNUEVfUkVUUklFVkVfUkVTRVJWRURfQUNDT1VOVF9UUkFOU0FDVElPTlMiLCJNUEVfSU5JVElBTElaRV9QQVlNRU5UIiwiTVBFX1JFU0VSVkVfQUNDT1VOVCIsIk1QRV9DQU5fUkVUUklFVkVfVFJBTlNBQ1RJT04iXSwianRpIjoiNmQ0OTA0ZDQtMzNjYy00Y2NlLWI3OTgtODg3ZjdlOTEwM2JhIiwiY2xpZW50X2lkIjoiTUtfVEVTVF9TQUY3SFI1RjNGIn0.jqjtn8uyVxSL8oVPNHojMRzqiPx__xYTzoPCsi5IJgiXd184Nc6XSp7QaZlbav6NjXQAt8uuYWVuEqZpJoHJSJlvR-QbpV_pkx582EV0cGdTNzFAUgyrRYvWxmFmNeS-XBVk0-8labvDqyLHcpjfoT_FaiBylY2ek99G8WvXxnvLE1STY91blhMb3UCKBykDzVlmX0U6gvWd9Btgb1-Zb18Olt-GHmsYavBDSvSpptSiIleFPsZzL1O_u366DVMmGrCq-1iqYpBBRixU0uslTsp17VJfvHJUwciPSHDDaPK2iH6QP7Nv4jPYMNhs8MW7L67NYO9yohD9qfAOTyWv6g"
),
));
$response = curl_exec($curl);
$objs = json_decode($response, true);
$rp = $objs['responseBody'];
echo "<div class='dlmenu1'> <span style='background:#fff;color:#295f2d;font-size:13px;text-align:center;font-weight:bold;padding:2px;margin-right:3px;'>" .
$rp['accountNumber'] . "</span>";
echo "<span style='background:#fff;color:#295f2d;font-size:13px;text-align:center;font-weight:bold;padding:2px;margin-right:3px;'>(" . $rp['bankName'] . ")</span>";
echo "<span style='background:#fff;color:#295f2d;font-size:13px;text-align:center;padding:2px;margin-right:3px;'>" . " - Instant Funding</span></div>";
curl_close($curl);
I need help on how I can pass the AccessToken generated in the first code to the second one (the two codes combined), so that it can serve as the Authorization Bearer.
I don't mind if the two codes can be combined.
Thanks

Pass Params in Curl Request

I have try curl request with terminal it's working but when i convert that curl request into php code that one passing param not working.
Terminal curl request :
curl --insecure "https://www.zohoapis.in/phonebridge/v3/clicktodial" -X POST -d "clicktodialuri=$clicktodialurl&clicktodialparam=[{'name':'fromnumber','value':'555'}]&zohouser=123456" -H "Authorization: Zoho-oauthtoken 1000.aedb399e2389cfacef60f965af052cbf" -H "Content-Type: application/x-www-form-urlencoded"
Response :
{"message":"ASTPP Clicktodial functionality has been enabled","status":"success","code":"SUCCESS"}
PHP Code :
$zohouser = '6000';
$access_token = '1000.c3c1107b635f1f5b257d831677e077d2';
$cURL = "https://www.zohoapis.in/phonebridge/v3/clicktodial?clicktodialuri=$click_to_dial&clicktodialparam=[{'name':'fromnumber','value':'555'}]&zohouser=$zohouser";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $cURL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"Authorization: Zoho-oauthtoken " . $access_token,
"Content-Type: application/x-www-form-urlencoded",
"cache-control: no-cache"
),
));
$response = json_decode(curl_exec($curl));
$err = curl_error($curl);
print_r($err);
curl_close($curl);
print_r($response);exit;
Not getting any response or error during run this php curl request.
Can you please help me how to pass string as param in php curl request.
$strURL= "https://www.zohoapis.in/phonebridge/v3/clicktodial";
$arrHeader= array(
'Authorization:Zoho-oauthtoken 1000.aedb399e2389cfacef60f965af052cbf'
);
$params= array(
"clicktodialparam"=>"[{\"name\":\"fromnumber\",\"value\":\"555\"}]",
"authorizationparam"=>"{\"name\":\"X-Auth-Token\",\"value\":\"1000.aedb399e2389cfacef60f965af052cbf\"}",
"clicktodialuri" => "$click_to_dial",
"zohouser" => "123456"
);
$ch= curl_init();
curl_setopt_array($ch,array(
CURLOPT_URL =>$strURL,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => $arrHeader,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 0,
CURLOPT_TIMEOUT => 0,
CURLOPT_POSTFIELDS => http_build_query($params)
));
$strResponse= curl_exec($ch);
print_r($strResponse);
echo curl_error($ch);
Referring to this post and using the linked tool we end up with the following code. Since I cannot test this myself I cannot guarantee my answer. It looks like you might be missing the curl post option "curl_setopt($ch, CURLOPT_POST, 1);" which you can use in lieu of the option you used "CURLOPT_CUSTOMREQUEST => "POST"".
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.zohoapis.in/phonebridge/v3/clicktodial');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "clicktodialuri=$clicktodialurl&clicktodialparam=[{'name':'fromnumber','value':'555'}]&zohouser=123456");
$headers = array();
$headers[] = 'Authorization: Zoho-oauthtoken 1000.aedb399e2389cfacef60f965af052cbf';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);

Categories