Created
July 28, 2023 15:03
-
-
Save ethanpil/e12f111c764f11aef597f1e706780d11 to your computer and use it in GitHub Desktop.
PHP JWT OAuth2 Request Boilerplate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//https://stackoverflow.com/a/48896992/933782 | |
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 | |
} | |
$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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment