简单查询爱发电的订单和赞助者信息
爱发电官方提供了API和PUSH功能,所以简单写了个读取爱发电的API的方法。
爱发电的官方API文档,返回数据的具体含义可以看这个。
/**
* 爱发电接口查询
* Version 1.0
*
* By Yeraph.
* https://www.yeraph.com/
*
* 官方文档
* https://afdian.net/dashboard/dev
*
*/
//返回结果处理
class Afdian_Response{
//初始化
public function __construct($status, $headers, $data){
$this->status = $status;
$this->headers = $headers;
$this->data = $data;
}
}
//生成查询
class Afdian_Query{
private $api_url = 'https://afdian.net/api/open/%s';
private $userid, $token;
//初始化
public function __construct($userid, $token){
$this->userId = $userid;
$this->token = $token;
}
//Curl函数
public function curl($url, $postData = [], $cookie = '', $headers = false){
$curl = curl_init();
$rHeaders = [];
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
curl_setopt($curl, CURLOPT_REFERER, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADERFUNCTION,
function ($curl, $header) use (&$rHeaders) {
$len = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) {
return $len;
}
$rHeaders[strtolower(trim($header[0]))][] = trim($header[1]);
return $len;
}
);
if (!empty($postData)) {
curl_setopt($curl, CURLOPT_POST, 1);
if (is_array($postData)) {
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postData));
} else {
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
}
}
if ($cookie) {
curl_setopt($curl, CURLOPT_COOKIE, $cookie);
}
if ($headers) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
$data = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if (curl_errno($curl)) {
$httpCode = curl_error($curl);
}
curl_close($curl);
return new Afdian_Response($httpCode, $rHeaders, $data);
}
//计算API请求需要的签名
public function signature($params, $time){
return md5("{$this->token}params{$params}ts{$time}user_id{$this->userid}");
}
//Api查询
public function query($api_name, $params){
if(!isset($api_name) || empty($api_name)){
return new Afdian_Response('Error: Empty api name endpoint', [], '');
}
$params = json_encode($params);
$queryData = json_encode([
'user_id' => $this->userid,
'params' => $params,
'ts' => time(),
'sign' => $this->signature($params, time())
]);
return $this->curl(sprintf($this->api_url, $api_name), $queryData, false, ['Content-Type: application/json']);
}
//Api查询 返回Ping状态
public function ping(){
$result = $this->query('ping', ['ping' => 'hello world']);
if($result->status == 200) {
$json = json_decode($result->data, true);
if($json && is_array($json)) {
return (isset($json['ec']) && $json['ec'] == 200);
}
return new Afdian_Response('Error: Ping api failed', [], '');
}
return false;
}
//Api查询 返回订单列表
public function get_orders($page = 1){
$result = $this->query('query-order', ['page' => $page]);
if($result->status == 200) {
$json = json_decode($result->data, true);
if($json && is_array($json)) {
return (isset($json['ec']) && $json['ec'] == 200) ? $json : $json['em'];
}
return new Afdian_Response('Error: Cannot parse json string', [], '');
}
return $result->status;
}
//Api查询 返回赞助者列表
public function get_ponsors($page = 1){
$result = $this->query('query-sponsor', ['page' => $page]);
if($result->status == 200) {
$json = json_decode($result->data, true);
if($json && is_array($json)) {
return (isset($json['ec']) && $json['ec'] == 200) ? $json : $json['em'];
}
return new Afdian_Response('Error: Cannot parse json string', [], '');
}
return $result->status;
}
}
如何使用:
//初始化类
$afdian = new Afdian_Query(‘你的USERID’, ‘你的TOKEN’);
//自定义查询(需要两个参数,请求的API类型和请求传递的参数)
$afdian = $afdian->query($api_name, $params);
//ping(返回1表示连接成功,否则报错)
$afdian = $afdian->ping();
//获取订单列表(需要一个参数,请求第几页订单列表)
$orders = $afdian->get_orders($page);
//获取赞助者(需要一个参数,请求第几页赞助者)
$sponsors = $afdian->get_sponsors($page);
一些其他说明:
Afdian_Query
会在Afdian_Response
中处理输出,用来添加额外的HTML或者重写格式化数据。
Afdian_Query
中简单定义了三个方法,分别是ping、获取订单列表、获取赞助者列表。
因为爱发电接口返回的数据本身就是分页的,所以,如果需要获取全部列表的话,则需要循环查询。
THE END
站点声明:本站部分内容转载自网络,作品版权归原作者及来源网站所有,任何内容转载、商业用途等均须联系原作者并注明来源。
暂无评论