在工作过程中接触的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
如有不好的点望大神指教!
|