I'm trying to make a post call like this to return a jwt bearer token. But in php using cURL. So I can save the bearer token to a variable for use in the API calls.
$.ajax({
type: 'POST',
url: 'http://www.your-site.com/siteguard/api/prepare-public-jwt',
data: { api_key: 'YOUR_PUBLIC_API_KEY' },
dataType: 'json',
success: function (data) {
//Returns encoded JWT
console.log(data);
}
});
Php Code Im using
$url = $this->getTokenUrl();
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Content-Type: application/json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = ["api_key" => "xOMjVpz83rxDKjJUX9qNClB2BwadcRWjm09YSCdasdabdasdasdasdgTR8fuvR7jQHP8ZVpbOOmdXqKEt0AVX"];
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
The Return is this
{"code":401,"status":"error","response":"Invalid Public API key","link":"http:\/\/localhost\/siteguard\/api\/prepare-public-jwt"}"
I was able to achieve it use this
$url = $this->getTokenUrl();
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $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 => 'POST',
CURLOPT_POSTFIELDS => 'xOMjVpz83rxDKjJUX9qNClB2BwadcRWjm09YSCdasdabdasdasdasdgTR8fuvR7jQHP8ZVpbOOmdXqKEt0AVX',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded'
),
));
$response = curl_exec($curl);
$obj = json_decode($response);
curl_close($curl);
return $obj->response;
Related
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;
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);
I am trying to make a php curl request to my laravel api
Here's the curl function
function CallAPI($method, $url, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
curl_setopt($curl, CURLOPT_HEADER, array('Accept:application/json', 'X-Requested-With:XMLHttpRequest' )
);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
print curl_error($curl);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
Here's where I call it :
require_once ADJEMINPAY_PLUGIN_DIR.'includes/class-adjeminpay-functions.php';
$url = "https://api.adjeminpay.net/v1/auth/getToken/";
$postData = array(
'firstName' => 'Lady',
'lastName' => 'Gaga',
'submit' => 'ok'
);
$result = CallAPI("POST", $url, $postData);
var_dump($result);
Here's the result :
string(454) "HTTP/2 301 date: Fri, 04 Dec 2020 12:12:03 GMT server: Apache location: https://api.adjeminpay.net/public/v1/auth/getToken content-length: 258 content-type: text/html; charset=iso-8859-1
Moved Permanently
The document has moved here.
"
Looks like laravel doesn't get that it's an api request and keeps changing the url to https://api.adjeminpay.net/ public/ v1/auth/getToken/ ; it also tries to redirect to https://api.adjeminpay.net/
Help kudasai TT
Sooo, turns out the syntax/version of curl I was using was defective/uncomplete.
Went over to PostMan and executed the same request then copipasted the php -curl code which looks like this :
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'yourUrl',
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 =>'{
"apikey": "qsmdlfjqs",
"application_id": "qsdlfqsmlj",
"grant-type": "qmdsfqsdf"
}',
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Content-Type: application/json',
': ',
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
And it works fine now.
Arigathanks \(^^)/
By the way How to generate PHP -curl code from POSTMAN.
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
I would like to ask you how to use PHP request to API with authorized header.
API end point example is
Quotation Service : POST https://api.website.com/v4/quotation
Contenttype: application/json; charset=utf8
Accept: application/json
Authorization: hmac $id:$milliSeconds:$signature
{
"serviceType": "CARD",
"specialRequests": [
"ROUNDTRIP"
]}
as you mention, they use hmac as an authorization in a header and request parameters is serviceType, specialRequest
I try using PHPCurl to make a request but i'm not a good on it. Please give me and advice how to use Curl with this API.
edit:
<?php
$customerId = "585a-SAMPLE-980dfe0097"; //by provider
$privKey = "MC4CAQACBQDSk4ghAgMB---SAMPLE---QIDAOPFAgMAk7QIDAMYq"; //by provider
$requestTime = (int)(microtime(true) * 1000);
$signature = hash_hmac('sha256', $requestTime, $privKey);
$headers = array(
"Authorization: hmac $customerId:$requestTime:$signature"
);
$params = array(
'serviceType' => 'CARD',
'specialRequests' => array('ROUNDTRIP')
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.samplesite.com/v1/quotations",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => $headers,
));
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Regards,
Hmm ... maybe something like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.website.com/v4/quotation');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
/* if you want to use SSL certificate, otherwise delete this */
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CAPATH , '/path/to/certificate');
$auth = hash_hmac('sha1', $id.':'.$milisecond.':'.$signature, <ENCRYPTION KEY>, true);
$headers = array(
'Authorization: '.$auth
);
$params = array(
'serviceType' => 'CARD',
'specialRequests' => array('ROUNDTRIP')
);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);