I get my bearer token from an API end point and set the following:
$authorization = "Bearer 080042cad6356ad5dc0a720c18b53b8e53d4c274"
Next I want to use CURL to access the secure endpoint however I am unsure on how or where to set the Bearer token.
I have tried this but but it does not work:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization )); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS,$post); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $result = curl_exec($ch); curl_close($ch); return json_decode($result);
EDIT:
According to the documentation, I am supposed to be using the bearer token as such: https://apigility.org/documentation/auth/authentication-oauth2
GET /oauth/resource HTTP/1.1 Accept: application/json Authorization: Bearer 907c762e069589c2cd2a229cdae7b8778caa9f07
Sending the Bearer Token with a Curl POST request is similar to sending the Bearer Token with a Curl GET request. POST data is passed with the -d command-line option, and the authorization header and the bearer token are passed with the -H command-line option.
Bearer tokens enable requests to authenticate using an access key, such as a JSON Web Token (JWT). The token is a text string, included in the request header. In the request Authorization tab, select Bearer Token from the Type dropdown list. In the Token field, enter your API key value.
Optional bearerFormat is an arbitrary string that specifies how the bearer token is formatted. Since bearer tokens are usually generated by the server, bearerFormat is used mainly for documentation purposes, as a hint to the clients. In the example above, it is "JWT", meaning JSON Web Token.
Replace:
$authorization = "Bearer 080042cad6356ad5dc0a720c18b53b8e53d4c274"
with:
$authorization = "Authorization: Bearer 080042cad6356ad5dc0a720c18b53b8e53d4c274";
to make it a valid and working Authorization header.
This is a cURL function that can send or retrieve data. It should work with any PHP app that supports OAuth:
function jwt_request($token, $post) { header('Content-Type: application/json'); // Specify the type of data $ch = curl_init('https://APPURL.com/api/json.php'); // Initialise cURL $post = json_encode($post); // Encode the data array into a JSON string $authorization = "Authorization: Bearer ".$token; // Prepare the authorisation token curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization )); // Inject the token into the header curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, 1); // Specify the request method as POST curl_setopt($ch, CURLOPT_POSTFIELDS, $post); // Set the posted fields curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // This will follow any redirects $result = curl_exec($ch); // Execute the cURL statement curl_close($ch); // Close the cURL connection return json_decode($result); // Return the received data }
Use it within one-way or two-way requests:
$token = "080042cad6356ad5dc0a720c18b53b8e53d4c274"; // Get your token from a cookie or database $post = array('some_trigger'=>'...','some_values'=>'...'); // Array of data with a trigger $request = jwt_request($token,$post); // Send or retrieve data
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With