Last active
February 3, 2022 08:04
-
-
Save angorb/749a8a53ce9f0689dd0b48e9fcace28d to your computer and use it in GitHub Desktop.
WeatherAPI.com Guzzle Service Description (Example)
This file contains 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
<?php | |
/*** | |
* An example of a Guzzle Service Description for WeatherAPI.com | |
* @see https://www.weatherapi.com/api-explorer.aspx | |
*/ | |
// composer require guzzlehttp/guzzle-services | |
require_once __DIR__ . '/../vendor/autoload.php'; | |
use GuzzleHttp\Client; | |
use GuzzleHttp\Command\Guzzle\GuzzleClient; | |
use GuzzleHttp\Command\Guzzle\Description; | |
// This is the important part. A usage example is below. | |
$weatherApiServiceDescription = [ | |
'baseUri' => 'http://api.weatherapi.com', | |
'operations' => [ | |
'current' => [ | |
'httpMethod' => 'GET', | |
'uri' => '/v1/current.json{?key}{?q}{?aqi}', | |
'responseModel' => 'getResponse', | |
'parameters' => [ | |
'key' => [ | |
'type' => 'string', | |
'location' => 'uri', | |
'required' => true | |
], | |
'q' => [ | |
'type' => 'string', | |
'location' => 'query', | |
'required' => true | |
], | |
'aqi' => [ | |
'type' => 'string', | |
'location' => 'query', | |
'pattern' => '/\byes|no\b/' | |
], | |
] | |
], | |
'forecast' => [ | |
'httpMethod' => 'GET', | |
'uri' => '/v1/forecast.json{?key}{?q}{?days}{?aqi}{?alerts}', | |
'responseModel' => 'getResponse', | |
'parameters' => [ | |
'key' => [ | |
'type' => 'string', | |
'location' => 'uri', | |
'required' => true | |
], | |
'q' => [ | |
'type' => 'string', | |
'location' => 'query', | |
'required' => true | |
], | |
'days' => [ | |
'type' => 'integer', | |
'location' => 'query' | |
], | |
'aqi' => [ | |
'type' => 'string', | |
'location' => 'query', | |
'pattern' => '/\byes|no\b/' | |
], | |
'alerts' => [ | |
'type' => 'string', | |
'location' => 'query', | |
'pattern' => '/\byes|no\b/' | |
], | |
] | |
], | |
'search' => [ | |
'httpMethod' => 'GET', | |
'uri' => '/v1/search.json{?key}{?q}', | |
'responseModel' => 'getResponse', | |
'parameters' => [ | |
'key' => [ | |
'type' => 'string', | |
'location' => 'uri', | |
'required' => true | |
], | |
'q' => [ | |
'type' => 'string', | |
'location' => 'query', | |
'required' => true | |
], | |
] | |
], | |
'history' => [ | |
'httpMethod' => 'GET', | |
'uri' => '/v1/history.json{?key}{?q}{?dt}', | |
'responseModel' => 'getResponse', | |
'parameters' => [ | |
'key' => [ | |
'type' => 'string', | |
'location' => 'uri', | |
'required' => true | |
], | |
'q' => [ | |
'type' => 'string', | |
'location' => 'query', | |
'required' => true | |
], | |
'dt' => [ // Date in yyyy-MM-dd format | |
'type' => 'string', | |
'location' => 'query', | |
'pattern' => '/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/' | |
], | |
] | |
], | |
'astronomy' => [ | |
'httpMethod' => 'GET', | |
'uri' => '/v1/astronomy.json{?key}{?q}{?dt}', | |
'responseModel' => 'getResponse', | |
'parameters' => [ | |
'key' => [ | |
'type' => 'string', | |
'location' => 'uri', | |
'required' => true | |
], | |
'q' => [ | |
'type' => 'string', | |
'location' => 'query', | |
'required' => true | |
], | |
'dt' => [ // Date in yyyy-MM-dd format | |
'type' => 'string', | |
'location' => 'query', | |
'pattern' => '/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/' | |
], | |
] | |
], | |
'timezone' => [ | |
'httpMethod' => 'GET', | |
'uri' => '/v1/timezone.json{?key}{?q}', | |
'responseModel' => 'getResponse', | |
'parameters' => [ | |
'key' => [ | |
'type' => 'string', | |
'location' => 'uri', | |
'required' => true | |
], | |
'q' => [ | |
'type' => 'string', | |
'location' => 'query', | |
'required' => true | |
], | |
] | |
], | |
'sports' => [ | |
'httpMethod' => 'GET', | |
'uri' => '/v1/sports.json{?key}{?q}', | |
'responseModel' => 'getResponse', | |
'parameters' => [ | |
'key' => [ | |
'type' => 'string', | |
'location' => 'uri', | |
'required' => true | |
], | |
'q' => [ | |
'type' => 'string', | |
'location' => 'query', | |
'required' => true | |
], | |
] | |
], | |
], | |
'models' => [ | |
'getResponse' => [ | |
'type' => 'object', | |
'additionalProperties' => [ | |
'location' => 'json' | |
] | |
] | |
] | |
]; | |
/*** | |
* EXAMPLE | |
* run as "php -f weatherapi-service-description-example.php current" | |
*/ | |
$client = new Client(); | |
$description = new Description($weatherApiServiceDescription); // created above // | |
$weatherApiClient = new GuzzleClient($client, $description); | |
// fill in your values (strings) | |
$key = '<API KEY>'; | |
$zip = '<ZIP CODE>'; | |
switch (strtolower($argv[1])) { | |
case 'current': | |
$result = $weatherApiClient->current(['key' => $key, 'q' => $zip]); | |
break; | |
case 'forecast': | |
$result = $weatherApiClient->forecast(['key' => $key, 'q' => $zip]); | |
break; | |
case 'search': | |
$result = $weatherApiClient->search(['key' => $key, 'q' => $zip]); | |
break; | |
case 'history': | |
$result = $weatherApiClient->history(['key' => $key, 'q' => $zip, 'dt' => date('Y-m-d', time() - (86400 * 3))]); | |
break; | |
case 'astronomy': | |
$result = $weatherApiClient->astronomy(['key' => $key, 'q' => $zip]); | |
break; | |
case 'timezone': | |
$result = $weatherApiClient->timezone(['key' => $key, 'q' => $zip]); | |
break; | |
case 'sports': | |
$result = $guzzleClient->sports(['key' => $key, 'q' => $zip]); | |
break; | |
default: | |
echo "'{$argv[1]}' was not recognized" . PHP_EOL; | |
exit(1); | |
} | |
print_r($result); | |
exit(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment