1. Go to this page and download the library: Download frog/php-curl-sai library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
frog / php-curl-sai example snippets
use FROG\PhpCurlSAI\SAI_Curl;
use FROG\PhpCurlSAI\SAI_CurlInterface;
class SDKClass {
// Define the variable to hold the cURL instance that will make a connection
protected $cURL;
/** Pass in the interface implemented by both the stub and actual class
* to allow for detection of methods to be used by your IDE
**/
public function __construct(
SAI_CurlInterface $cURL = null
) {
// If no curl instance is provided, the one that makes the actual connection shall be used
if ($cURL == null) $this->cURL = new SAI_Curl();
else
$this->cURL = $cURL;
}
public function get_data(
string $access_token
){
$auth_headers = [
"Authorization: Bearer {$access_token}",
"Content-Type: application/json",
];
$options = [
CURLOPT_URL => 'https://cool.url/data',
CURLOPT_HTTPHEADER => $auth_headers,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
];
// Setup a cURL handle via the interface
$ch = $this->cURL->curl_init();
// Add curl options via the interface
$this->cURL->curl_setopt_array($ch, $options);
// Execute the reqeust via the interface
$response = $this->cURL->curl_exec($ch);
if ($response === false) {
// Retrieve the error via the interface
$result = $this->cURL->curl_error($ch);
} else {
// Decode the result if need be
$result = json_decode($response);
}
// Close the handle via the interface
$this->cURL->curl_close($ch);
return $result;
}
public function authorize(
string $email,
string $password,
){
$request_body = [
'email' => '[email protected]',
'password' => "12345678",
];
$options = [
CURLOPT_URL => 'https://cool.url/token',
CURLOPT_HTTPHEADER => ["Content-Type: application/json"],
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($request_body),
];
// Setup a cURL handle via the interface
$ch = $this->cURL->curl_init();
// Add curl options via the interface
$this->cURL->curl_setopt_array($ch, $options);
// Execute the reqeust via the interface
$response = $this->cURL->curl_exec($ch);
if ($response === false) {
// Retrieve the error via the interface
$result = $this->cURL->curl_error($ch);
} else {
// Decode the result if need be
$result = json_decode($response);
}
// Close the handle via the interface
$this->cURL->curl_close($ch);
return $result;
}
}
use FROG\PhpCurlSAI\SAI_CurlStub;
public function stub_get_data() {
// Create an instance of the stub class which implements SAI_CurlInterface
$cURL = new SAI_CurlStub();
$sdk = new SDKClass($cURL);
$request_body = [
'email' => '[email protected]',
'password' => "12345678",
];
$options = [
CURLOPT_URL => 'https://cool.url/token',
CURLOPT_HTTPHEADER => ["Content-Type: application/json"],
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($request_body),
];
// Set the response you want returned when the first cURL call is to be made
$cURL->setResponse(
'{"access_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c","token_type":"Bearer","expires_in":3600}',
// Pass in the cURL options (Needed to get the desired output)
$options,
);
$token_result = $sdk->authorize(
'[email protected]',
"12345678"
);
$auth_headers = [
"Authorization: Bearer {$token_result->access_token}",
"Content-Type: application/json",
];
$options = [
CURLOPT_URL => 'https://cool.url/data',
CURLOPT_HTTPHEADER => $auth_headers,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
];
// Set the response you want returned when the second cURL call is to be made
$cURL->setResponse(
'{"ref":"ac980b8e-afe5-49b8-b348-d0af00e2f556","date":"2021-02-21 22:20:32","code":"-11","MessageDescription":"MESSAGE REFERENCE LONGER THAN ALLOWED LENGTH"}',
// Pass in the cURL options (Needed to get the desired output)
$options,
);
$result = $sdk->get_data($token_result->access_token);
}
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.