PHP quickstart
You need PHP 8.0 or later with the curl and json extensions. The client has no other dependencies.
1. Install
The client is not on Packagist yet, so download this release from our site and unpack it beside your code:
curl -L https://developers.astrologics.in/downloads/sdks/astrologics-php-0.1.0.tar.gz | tar -xz
This creates the folder astrologics-php-0.1.0.
2. Set your key
Create a test key. Test keys run the real engine and are never charged.
export ASTROLOGICS_API_KEY=al_test_YOUR_KEY
Use a secret key only on a server, never in a web page or a mobile app.
3. Make your first call
Save as first-call.php and run php first-call.php.
<?php
require __DIR__ . '/astrologics-php-0.1.0/src/ApiError.php';
require __DIR__ . '/astrologics-php-0.1.0/src/Client.php';
$client = new \AstroLogics\Client(getenv('ASTROLOGICS_API_KEY'));
$result = $client->getKundli(['birth' => ['date' => '1990-08-13', 'time' => '06:30', 'place' => ['latitude' => 28.6139, 'longitude' => 77.209]]]);
echo $result['data']['lagna']['sign'], ' ', $result['meta']['mode'], ' ', $result['meta']['credits_charged'], "\n";
It prints the lagna's sign, then test 0: the call ran in test mode and cost no credits. A place can also be a place_id or free text; see the birth object.
4. Save a chart
Save as save-chart.php. An SVG, PNG or PDF operation returns a string holding the file's content.
<?php
require __DIR__ . '/astrologics-php-0.1.0/src/ApiError.php';
require __DIR__ . '/astrologics-php-0.1.0/src/Client.php';
$client = new \AstroLogics\Client(getenv('ASTROLOGICS_API_KEY'));
$svg = $client->getChartSvg(['varga' => 'D1', 'birth' => ['date' => '1990-08-13', 'time' => '06:30', 'place' => ['latitude' => 28.6139, 'longitude' => 77.209]]]);
file_put_contents('chart.svg', $svg);
5. Handle errors
Save as handle-errors.php. Any failed call throws an ApiError. Branch on errorCode, not on the message, and quote requestId when you write to us. See errors.
<?php
require __DIR__ . '/astrologics-php-0.1.0/src/ApiError.php';
require __DIR__ . '/astrologics-php-0.1.0/src/Client.php';
$client = new \AstroLogics\Client(getenv('ASTROLOGICS_API_KEY'));
try {
$client->getKundli(['birth' => ['date' => '1990-13-45']]);
} catch (\AstroLogics\ApiError $e) {
echo $e->status, ' ', $e->errorCode, ' ', $e->requestId, "\n";
foreach ($e->issues as $issue) {
echo $issue['param'], ' ', $issue['message'], "\n";
}
}
ApiError also carries candidates for an ambiguous place and suggestions for an unknown path.
Retries and timeouts
429 and 5xx answers are retried twice by default, honouring Retry-After. quota_exhausted is not retried. To change this, pass $maxRetries and $timeout (in seconds, default 60) to the constructor, after $baseUrl.
Find a method
Each method is named after the operation's id in the API reference. It takes one array holding the operation's parameters and body, side by side. There is one method per public operation: 133 today.
Go live
Swap in a live key (al_live_) when you are ready. Live keys spend your plan's credits, and only successful calls are charged.
Next: SDKs · TypeScript · Python