Yii2-xmlsoccer
Yii2 client for Football API API, (*1)
Full API Documentation here: http://football-api.com/documentation/, (*2)
Requirements:
PHP5 with CURL. SimpleXML extensions only needed if XML is chosen as output., (*3)
Installation
The preferred way to install this extension is through composer., (*4)
Either run, (*5)
composer require --prefer-dist drsdre/yii2-footballapi "*"
or add, (*6)
"drsdre/yii2-footballapi": "*"
to the require
section of your composer.json
file., (*7)
Usage
You can either setup the client as an application component:, (*8)
'components' => [
'footballapiApi' => [
'class' => '\FootballAPI\Client',
'api_key' => 'xxx',
]
...
]
or use the client directly in your code:, (*9)
$client = new \FootballAPI\Client([
'api_key' => 'xxx',
]);
Configuration
The output format of the the API calls can be changed by adding a output type parameter:, (*10)
'components' => [
'footballApi' => [
'class' => '\FootballAPI\Client',
'api_key' => 'xxx',
'output_type' => 'PHP',
]
...
]
Default value: JSON. Options include: XML, JSON, PHPARRAY, PHPOBJECT, LINE, CONSOLE, VAR., (*11)
Optionally a cache component can be added for example to keep the client returning data during a time-out:, (*12)
'components' => [
'footballApiCache' => [
'class' => 'yii\caching\FileCache',
],
'footballApi' => [
'class' => '\FootballAPI\Client',
'api_key' => 'xxx',
'cache' => 'footballApiCache',
]
...
]
To facilitate a check to detect data changes, a content hash can be generated by setting the parameter 'generate_hash'
to true (currently only supported in XML, PHPARRAY & PHPOBJECT output type). The output will then include two new attributes:, (*13)
- contentHash: MD5 hash
- sourceURL: URL used to retrieve the data
If you need to have the API be executed via a specific network adapter it's possible the specify the outgoing IP:, (*14)
$client = new \FootballAPI\Client([
'api_key' => 'xxx',
'service_ip' => '192.168.1.1',
]);
How to use API:
Go to Sign up for Free and retrieve the API key for
access to the football-api.com API., (*15)
Methods Available
Go to http://football-api.com/documentation/ for more info about methods and
parameters including online testing., (*16)
Examples:
List competitions
try {
$client = new \FootballAPI\Client([
'api_key' => 'xxx',
]);
$competitions=json_decode($soccer->competitions());
echo "Competitions List:<br>";
foreach($competitions as $competition){
echo "<b>".$competition->name."</b> ".$competition->region."<br>";
}
}
catch(Exception $e) {
echo "FootballAPI Exception: ".$e->getMessage();
}
If your server has multiple IP's available, you can set any IP for request:
try {
$client = new \FootballAPI\Client([
'api_key' => 'xxx',
]);
$soccer->setRequestIp("ip_for_request");
$result=json_decode($soccer->standings(["comp_id"=>1064]));
var_dump($result);
}
catch(Exception $e) {
echo "FootballAPI: ".$e->getMessage();
}
That's all!