最近有一个项目需要对接途狐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车辆实时定位,其他相关接口调用方式参照如上即可


huaweichenai
635 声望114 粉丝

下一篇 »
FFMPEG安装