PHP code example of echo-five / sparky-api-client-php
1. Go to this page and download the library: Download echo-five/sparky-api-client-php 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/ */
echo-five / sparky-api-client-php example snippets
// Load Composer autoloader.
lient\SparkyApiClient;
// Try to execute the API request.
try {
// Initialize.
$sparkyApi = new SparkyApiClient('SPARKY_API_HOST', 'SPARKY_API_KEY');
// Do a request.
$sparkyApi->request('post', '/endpoint', [
'foo' => 'Bar',
'biz' => 'Buz',
]);
// Get the request response.
$requestResponse = $sparkyApi->getResponse();
// Dump.
var_dump($requestResponse);
}
catch (Exception $e) {
// Show error message.
echo 'Error: ' . $e->getMessage();
}
// Initialize with signature key (private key) for signed requests.
$sparkyApi = new SparkyApiClient('SPARKY_API_HOST', 'SPARKY_API_KEY', 'SPARKY_API_SIGNATURE_KEY');
// Load Composer autoloader.
lient\SparkyApiClient;
// Try to execute the API request.
try {
// Initialize.
$sparkyApi = new SparkyApiClient('SPARKY_API_HOST', 'SPARKY_API_KEY');
// Start the debugging mode.
$sparkyApi->debugStart();
// Do a request.
$sparkyApi->request('post', '/endpoint', [
'foo' => 'Bar',
'biz' => 'Buz',
]);
// Stop the debugging mode.
$sparkyApi->debugStop();
// Get the request response.
$requestResponse = $sparkyApi->getResponse();
// Dumps.
var_dump($requestResponse);
var_dump($sparkyApi->debugGet());
}
catch (Exception $e) {
// Show error message.
echo 'Error: ' . $e->getMessage();
}
// Do a request and get the request response.
$requestResponse = $sparkyApi->request('post', '/endpoint')->getResponse();
$requestResponse = $sparkyApi->getResponse(); // Return a PHP object.
$requestResponse = $sparkyApi->getResponse(true); // Return a PHP object.
$requestResponse = $sparkyApi->getResponse(false); // Return a raw JSON string.
// Request:
$sparkyApi->request('post', '/endpoint', [
'foo' => 'Bar',
'biz' => 'Buz',
]);
// Response:
stdClass Object
(
[status] => 200
[data] => stdClass Object
(
[foo] => Bar
[biz] => Buz
)
[messages] => Array
(
[0] => stdClass Object
(
[type] => info
[text] => The request has input data.
)
[1] => stdClass Object
(
[type] => info
[text] => The request has 2 data keys.
[data] => stdClass Object
(
[keys] => 2
)
)
)
)
// Get the request response status.
$requestResponseStatus = $sparkyApi->getResponseStatus();
$requestResponseHeaders = $sparkyApi->getResponseHeaders(); // Return a parsed array.
$requestResponseHeaders = $sparkyApi->getResponseHeaders(true); // Return a parsed array.
$requestResponseHeaders = $sparkyApi->getResponseHeaders(false); // Return a raw headers string.
// Get the request response data.
$requestResponseData = $sparkyApi->getResponseData();
// Get the request response messages.
$requestResponseMessages = $sparkyApi->getResponseMessages();
$requestSent = $sparkyApi->getSent(); // Return a PHP object.
$requestSent = $sparkyApi->getSent(true); // Return a PHP object.
$requestSent = $sparkyApi->getSent(false); // Return a raw string.
$requestSentHeaders = $sparkyApi->getSentHeaders(); // Return a parsed array.
$requestSentHeaders = $sparkyApi->getSentHeaders(true); // Return a parsed array.
$requestSentHeaders = $sparkyApi->getSentHeaders(false); // Return a raw headers string.
$requestSentPayload = $sparkyApi->getSentPayload(); // Return a PHP object.
$requestSentPayload = $sparkyApi->getSentPayload(true); // Return a PHP object.
$requestSentPayload = $sparkyApi->getSentPayload(false); // Return a raw string.
// Get the request info.
$requestInfo = $sparkyApi->getCurlInfo();
// Start the debugging mode.
$sparkyApi->debugStart();
// Stop the debugging mode.
$sparkyApi->debugStop();
// Start the debugging mode.
$sparkyApi->debugStart();
// Do a request #1.
$sparkyApi->request('post', '/endpoint');
// Do a request #2.
$sparkyApi->request('post', '/endpoint');
// Dump the debugging data (which contains requests #1 and #2).
var_dump($sparkyApi->debugGet());
// Do a request #3.
$sparkyApi->request('post', '/endpoint');
// Do a request #4.
$sparkyApi->request('post', '/endpoint');
// Stop the debugging mode.
$sparkyApi->debugStop();
// Dump the debugging data (which contains requests #1, #2, #3, #4).
var_dump($sparkyApi->debugGet());
// Do a request and reset the debugging data.
$sparkyApi->request('post', '/endpoint')->debugReset();
// Start the debugging mode.
$sparkyApi->debugStart();
// Do a request #1.
$sparkyApi->request('post', '/endpoint');
// Do a request #2.
$sparkyApi->request('post', '/endpoint');
// Dump the debugging data (which contains requests #1 and #2).
var_dump($sparkyApi->debugGet());
// Reset the debugging data.
$sparkyApi->debugReset();
// Do a request #3.
$sparkyApi->request('post', '/endpoint');
// Do a request #4.
$sparkyApi->request('post', '/endpoint');
// Stop the debugging mode.
$sparkyApi->debugStop();
// Dump the debugging data (which contains requests #3 and #4).
var_dump($sparkyApi->debugGet());
// Accept self-signed certificates when testing against local API instance.
$sparkyApi = new SparkyApiClient('https://local-api.dev', 'SPARKY_API_KEY');
$sparkyApi->acceptUnsafeCertificatesByDisablingCurlSslVerifyPeer();
$sparkyApi->request('post', '/endpoint', ['data' => 'value']);