Created
March 14, 2023 14:48
-
-
Save joekolade/ae5899910fd2ce41a9dbebf49426817b to your computer and use it in GitHub Desktop.
[curl call] PHP curl call
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
| /** | |
| * Basic Curl-Call | |
| * @see http://codular.com/curl-with-php | |
| */ | |
| $url = '###URL###'; | |
| $token = '122321231'; | |
| // Get cURL resource | |
| $curl = curl_init(); | |
| $authorization = "Authorization: Bearer ".$token; // Prepare the authorisation token | |
| curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization )); // Inject the token into the header | |
| // Set some options - we are passing in a useragent too here | |
| curl_setopt_array($curl, array( | |
| CURLOPT_RETURNTRANSFER => 1, // Return the response as a string instead of outputting it to the screen | |
| CURLOPT_URL => $url, // URL to send request to | |
| CURLOPT_POST => 1, // Send request as POST | |
| CURLOPT_POSTFIELDS => array(), // Array of data to POST in request | |
| //CURLOPT_USERAGENT => 'PHP Curl Call', // Useragent string to use for request | |
| //CURLOPT_CONNECTTIMEOUT => 300, // Number of seconds to spend attempting to connect | |
| //CURLOPT_TIMEOUT => 60, // Number of seconds to allow cURL to execute | |
| //CURLOPT_FAILONERROR => false, // request failure on HTTP response >= 400; instead of delivering HTML | |
| )); | |
| // Send the request & save response to $resp | |
| $resp = curl_exec($curl); | |
| // Close request to clear up some resources | |
| curl_close($curl); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment