首页
学习
活动
专区
工具
TVP
发布

thinkphp5 php命令行模式运行,运行定时任务上传微信小程序素材

index模块新建文件command/Test.php

namespace app\index\command;

use think\Db;

use think\console\Command;

use think\console\Input;

use think\console\Output;

class Test extends Command{

protected function configure(){

$this->setName('test')->setDescription('Here is the remark ');

}

protected function execute(Input $input, Output $output){

$token=action('index/kefu/get_access_token');

$url='https://api.weixin.qq.com/cgi-bin/media/upload?access_token='.$token.'&type=image';

$body=array(

'media'=>new \CURLfile("/root/erweima.jpg",'image/jpg')

);

$result =$this->https_request($url,$body);

db::name('wxset')->where('id','eq','6')->update(['appid'=>$result['media_id'],'secret'=>$result['created_at']]);

} //存入数据库

// 模拟get请求和post请求

public function https_request($url,$data=""){

// 开启curl

$ch=curl_init();

// 设置传输选项

curl_setopt($ch, CURLOPT_URL , $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1);//以文件流返回

//执行并获取结果

if($data){

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

}

$request=curl_exec($ch);

// $arrs=json_decode($arr,ture);

$tmpArr=json_decode($request,true);

if(is_array($tmpArr)){

return $tmpArr;

}else{

return $request;

}

// $access_token=$arrs['access_token'];

//释放curl

curl_close($ch);

}

}

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180626G1G1GY00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券