Last active
February 16, 2025 16:07
-
-
Save Kristjan-Reinsberg/db8edcec2b73a8f3d07988426188e402 to your computer and use it in GitHub Desktop.
Use Curl Post request to get JWT (Json Web Token) and make request with that token
This file contains hidden or 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
| /** | |
| * @author Krisjan Reinsberg | |
| * @Use Curl Post request to get JWT (Json Web Token) and make request with that token | |
| **/ | |
| function http_post($url, $jsonData) | |
| { | |
| $ch = curl_init($url); | |
| $jsonDataEncoded = json_encode($jsonData); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
| curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); | |
| curl_setopt($ch, CURLOPT_POST, 1); | |
| curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded); | |
| $result = curl_exec($ch); | |
| curl_close($ch); | |
| return json_decode($result); | |
| } | |
| function http_get_with_jwt($url, $token) | |
| { | |
| $ch = curl_init($url); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
| 'Content-Type: application/json', | |
| 'Authorization: Bearer ' . $token | |
| )); | |
| $result = curl_exec($ch); | |
| //$info = curl_getinfo($ch); | |
| curl_close($ch); | |
| return json_decode($result) ; | |
| } | |
| $payload = [ | |
| 'username' => 'xxx', | |
| 'password'=> 'yyy' | |
| ]; | |
| //1. Request the token with logins | |
| $asked_token = http_post("https://zzz.crmcloudservices.com/api/v1/auth", $payload ); | |
| //2. Now use the token wih your request | |
| $query_result = http_get_with_jwt("https://zzz.crmcloudservices.com/api/v1/contacts", $asked_token ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment