关于php:途狐GPS接口调用

34次阅读

共计 2604 个字符,预计需要花费 7 分钟才能阅读完成。


最近有一个我的项目须要对接途狐 GPS,这里我写了一个 demo,不便咱们来调用处狐 GPS 的相干接口

途狐 GPS 接口文档:http://opendoc.tuhugc.com/thg…

途狐 GPSdemo:

<?php

namespace common\helpers;

use common\models\Config;
use Yii;
use yii\helpers\ArrayHelper;
use common\helpers\Universal;
use yii\httpclient\Client;

/**
 * 途狐管车开放平台接口
 *
 * @author wangjian
 * @since 0.1
 */
class Tuhu
{

    /**
     * @var string app_key
     */
    public $app_key = '';

    /**
     * @var string app_secret
     */
    public $app_secret = '';

    /**
     * @var string url
     */
    public $url = '';

    /**
     * @var string user_id
     */
    public $user_id = '';

    /**
     * @var string method
     */
    public $method = 'POST';

    /**
     * @var string access_token
     */
    public $access_token;

    /**
     * @var array header
     */
    public $header = ['Content-Type' => 'application/x-www-form-urlencoded'];


    /**
     * @var string expires access_token 生效事件
     */
    public $expires = '7200';


    public function __construct($api_url, $data = [], $method = 'POST')
    {$this->access_token = Yii::$app->cache->get('tuhu_access_token');

        $this->data = [
            'appKey' => $this->app_key,
            'v' => '1.0',
            'timestamp' => date('Y-m-d H:i:s'),
            'signMethod' => 'md5',
            'format' => 'json',
        ];

        if (empty($this->access_token)) {$this->access_token = $this->getAccessToken();
        }
        if ($data) {$this->data = ArrayHelper::merge($this->data, $data);
        }
        $this->url = $this->url . $api_url;
    }

    /**
     * 调用处狐接口
     */
    public function curl()
    {$sign = $this->signMd5($this->data);

        $this->data['sign'] = $sign;

        $this->header['X-Access-Token'] = $this->access_token;
        $client = new Client();
        $response = $client->createRequest()
            ->setMethod($this->method) // 申请形式
            ->setUrl($this->url)      // 申请地址
            ->setData($this->data)
            ->setHeaders($this->header)
            ->send();
        if ($response->isOk) {$result = isset($response->data['result']) ? $response->data['result'] : [];
            return $result;
        }

        Yii::$app->cache->delete('tuhu_access_token');
        return false;
    }


    public function getAccessToken()
    {
        $data = $this->data;

        $url = $this->url . '/v1/token/get';

        // 公有参数
        $data['userId'] = $this->user_id;
        $data['expiresIn'] = $this->expires;


        $sign = $this->signMd5($data);

        $data['sign'] = $sign;

        $client = new Client();
        $response = $client->createRequest()
            ->setMethod('POST') // 申请形式
            ->setUrl($url)      // 申请地址
            ->setData($data)
            ->setHeaders($this->header)
            ->send();
        $accessToken = isset($response->data['result']['accessToken']) ? $response->data['result']['accessToken'] : '';
        if ($accessToken) {Yii::$app->cache->set('tuhu_access_token', $accessToken, $this->expires);
            return $accessToken;
        }

        return '';
    }

    /**
     * md5 签名
     */
    public function signMd5($data)
    {
        // 获取待签名字符串
        ksort($data);
        reset($data);
        $query = '';
        foreach ($data as $key => $value) {$query .= $key . $value;}

        $sign = $this->app_secret . $query . $this->app_secret;
        // 签名
        return strtoupper(md5($sign));
    }


}

须要将 demo 中 url,app_key,app_secret,user_id 改成您本人的即可

使用处狐 GPSdemo 简略示例
回去 GPS 车辆实时定位

$tuhu = new Tuhu('/v1/device/location/list', [
    'userId' => '用户 ID',
    'mapType' => 'GOOGLE',//GOOGLE/BAIDU
]);
$data = $tuhu->curl();

如上既能够获取到 GPS 车辆实时定位,其余相干接口调用形式参照如上即可

正文完
 0