php 微信上传图片 客服发送图片
public function _getAccessToken()
{
$where = array(‘token’ => $this->token);
$this->thisWxUser = M(‘Wxuser’)->where($where)->find();
// import(‘@.ORG.apiOauth’);
$apiOauth = new apiOauth();
$access_token = $apiOauth->update_authorizer_access_token($this->thisWxUser[‘appid’]);
return $access_token;
}
function upload_meterial($file_info,$access_token){
$url=”https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$access_token}&type=image”;
$ch1 = curl_init ();
$timeout = 5;
$data= array(“media”=>”@{$file_info}”);
curl_setopt ( $ch1, CURLOPT_URL, $url );
curl_setopt ( $ch1, CURLOPT_POST, 1 );
curl_setopt ( $ch1, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch1, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt ( $ch1, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt ( $ch1, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt ( $ch1, CURLOPT_POSTFIELDS, $data );
$result = curl_exec ( $ch1 );
curl_close ( $ch1 );
if(curl_errno()==0){
$result=json_decode($result,true);
//var_dump($result);
return $result[‘media_id’];
}else {
return false;
}
}
public function curl_post($url, $data = null)
{
//创建一个新cURL资源
$curl = curl_init();
//设置URL和相应的选项
curl_setopt($curl, CURLOPT_URL, $url);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//执行curl,抓取URL并把它传递给浏览器
$output = curl_exec($curl);
//关闭cURL资源,并且释放系统资源
curl_close($curl);
return $output;
}
public function sendimg($filename){
$access_token = $this->_getAccessToken();
$resmedia_id = $this->upload_meterial($filename,$access_token);
if($resmedia_id){
//发送
$FromUserNamet = “o7pw-waUVRGw72OhxZ8v9qpVEUPM”;
$data='{“touser”: “‘.$FromUserNamet.'”,”msgtype”: “image”,”image”: {“media_id”: “‘.$resmedia_id.'”}}’;
$url2 = “https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=”.$access_token;
$res = $this->curl_post($url2,$data);
}
}