PHP获取腾讯接口;

在工作过程中接触的PHP,之前专业是Java方向的; 由于是首次接触,不好的点望指点!

需求:接通腾旭开发手册的接口获取数据并且展现;

<?php

namespace app\marketing\controller;

use library\Controller;
use think\Db;

class Brand extends Controller

{
    //连接页面前端展示数据库内容
    public $table = 'yx_brand';//前端展现的数据表


    public function brand()
    {
        $this->title = 'brand展示';//标题
        $this->_query($this->table)->page();
    }

    //后端获取品牌形象接口返回数据前端展示
    public function index($access_token)
    {
        $list = Db::query("select * from yx_user_account ");//为有token的表
        foreach ($list as $vo) {
            dump($list);
                $interface = 'brand/get';
                $url = 'https://api.e.qq.com/v1.1/' . $interface;
                $common_parameters = array(
                    'access_token' => $vo['access_token'],
                    'timestamp' => time(),
                    'nonce' => md5(uniqid('', true))
                );
                $parameters = array(
                    'account_id' => $vo['account_id'],

                );
                $parameters = array_merge($common_parameters, $parameters);
                foreach ($parameters as $key => $value) {
                    if (!is_string($value)) {
                        $parameters[$key] = json_encode($value);
                    }
                }
                $request_url = $url . '?' . http_build_query($parameters);
                $curl = curl_init();
                curl_setopt($curl, CURLOPT_URL, $request_url);
                curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                $response = curl_exec($curl);
                if (curl_error($curl)) {
                    $error_msg = curl_error($curl);
                    $error_no = curl_errno($curl);
                    curl_close($curl);
                    throw new \Exception($error_msg, $error_no);
                }
                curl_close($curl);
                $result = json_decode($response, true);
                if ($result['code'] !== 0) {
                    $this->error('同步失败');
                }
                foreach ($result['data'] ['list'] as $ttt) {
                    dump($ttt);
                    $num = Db::name("yx_brand")->where('account_id', $ttt['account_id'])->count();
                    if ($num == 0) {
                        $fund['account_id'] = $ttt['account_id'];
                        $fund['name'] = $ttt['name'];
                        $fund['image_id'] = $ttt['image_id'];
                        $fund['width'] = $ttt['width'];
                        $fund['height'] = $ttt['height'];
                        $fund['image_url'] = $ttt['image_url'];
                       // $fund['created_time'] = $ttt['created_time'];
                        $fund['created_time']=date('Y-m-d H:i:s', $ttt['created_time']);
                        Db::name("yx_brand")->insert($fund);
                    } else {
                        $fund['account_id'] = $ttt['account_id'];
                        $fund['name'] = $ttt['name'];
                        $fund['image_id'] = $ttt['image_id'];
                        $fund['width'] = $ttt['width'];
                        $fund['height'] = $ttt['height'];
                        $fund['image_url'] = $ttt['image_url'];
                        //$fund['created_time'] = $ttt['created_time'];
                        $fund['created_time']=date('Y-m-d H:i:s', $ttt['created_time']);
                        Db::name("yx_brand")->where('account_id', $ttt['account_id'])->update($fund);
                    }
            }
            $this->success('同步成功!', '');
        }
    }

}

前端的话就是把你们自己的数据库内容展现出来就好了;

对应调取的腾讯接口为


https://developers.e.qq.com/docs/api/business_assets/brand/brand_get


如有不好的点望大神指教!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值