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);
}
}
领取专属 10元无门槛券
私享最新 技术干货