drsdre / yii2-footballapi
用于访问football-api.com API的Yii2扩展
0.0.1
2015-09-01 09:15 UTC
Requires
- php: >=5.4.0
- ext-simplexml: *
- lib-curl: *
- yiisoft/yii2: *
This package is auto-updated.
Last update: 2024-08-29 04:42:50 UTC
README
Football API的Yii2客户端
完整的API文档在此:http://football-api.com/documentation/
要求
PHP5 with CURL。如果选择XML作为输出,则只需SimpleXML扩展。
安装
安装此扩展的首选方式是通过composer。
运行
composer require --prefer-dist drsdre/yii2-footballapi "*"
或者将以下内容添加到你的composer.json文件的require部分。
"drsdre/yii2-footballapi": "*"
使用方法
您可以将客户端设置为应用程序组件
'components' => [ 'footballapiApi' => [ 'class' => '\FootballAPI\Client', 'api_key' => 'xxx', ] ... ]
或直接在您的代码中使用客户端
$client = new \FootballAPI\Client([ 'api_key' => 'xxx', ]);
配置
可以通过添加输出类型参数来更改API调用的输出格式
'components' => [ 'footballApi' => [ 'class' => '\FootballAPI\Client', 'api_key' => 'xxx', 'output_type' => 'PHP', ] ... ]
默认值:JSON。选项包括:XML、JSON、PHPARRAY、PHPOBJECT、LINE、CONSOLE、VAR。
可选地,可以添加缓存组件,例如,在超时期间保持客户端返回数据。
'components' => [ 'footballApiCache' => [ 'class' => 'yii\caching\FileCache', ], 'footballApi' => [ 'class' => '\FootballAPI\Client', 'api_key' => 'xxx', 'cache' => 'footballApiCache', ] ... ]
为了方便检测数据变化,可以生成内容哈希,将参数'generate_hash'设置为true(目前仅支持XML、PHPARRAY & PHPOBJECT输出类型)。输出将包括两个新属性
- contentHash: MD5哈希
- sourceURL: 用于检索数据的URL
如果您需要通过特定的网络适配器执行API,可以指定出站IP。
$client = new \FootballAPI\Client([ 'api_key' => 'xxx', 'service_ip' => '192.168.1.1', ]);
如何使用API
前往免费注册并获取访问football-api.com API的API密钥。
可用方法
有关方法和参数的更多信息(包括在线测试),请访问http://football-api.com/documentation/
示例
列出比赛
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();
}
如果您的服务器有多个IP可用,您可以为请求设置任何IP
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();
}