zoukankan      html  css  js  c++  java
  • 微信上传客服消息图片

    public function uploadWXImg($imgpath, $id){
    	$mp_infoModel = M('Game');
    	$mp_info = $mp_infoModel->find($id);
    	$TOKEN=$this->get_access_token($mp_info['app_id'], $mp_info['app_secret']);
    	$URL ='https://api.weixin.qq.com/cgi-bin/media/upload?access_token='.$TOKEN.'&type=image';
    	$data = ['media'=>'@'.$imgpath];
    	$result = $this->curl_post($URL,$data);
    	$data = @json_decode($result,true);
    	return $data['media_id'];
    }
    /**
     * 获取token
     * @param string $appId
     * @param string $appSecret
     * @return mixed
     */
    public function get_access_token($appId = '', $appSecret = ''){
    	$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$appSecret";
    	$result = file_get_contents($url);
    	$result = json_decode($result,true);
    	$accesstoken = $result['access_token'];
    	return $accesstoken;
    }
    
    public function curl_post($url, $data = null)
    {
    	$curl=curl_init();
    	curl_setopt ( $curl, CURLOPT_SAFE_UPLOAD, FALSE); 
    	curl_setopt($curl, CURLOPT_URL, $url);
    	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    	if(!empty($data)){
    	    curl_setopt($curl, CURLOPT_POST, 1);
    		$hadFile=false;
    		if (is_array($data) && isset($data['media'])) {
    			/* 支持文件上传 */
    			if (class_exists('CURLFile')){
    				curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
    				foreach ($data as $key => $value) {
    					if ($this->isPostHasFile($value)) {
    						$postfields[$key] = new CURLFile(realpath(ltrim($value, '@')));
    						$hadFile = true;
    					}
    				}
    			}elseif (defined('CURLOPT_SAFE_UPLOAD')) {
    				if ($this->isPostHasFile($value)) {
    					curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
    					$hadFile = true;
    				}
    			}
    		}
    		$tmpdatastr = (!$hadFile && is_array($postfields)) ? http_build_query($postfields) : $postfields;
    		curl_setopt($curl, CURLOPT_POSTFIELDS, $tmpdatastr);
    	}
    	curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
    	$output=curl_exec($curl);
    	curl_close($curl);
    	return $output;
    }
    public function isPostHasFile($value)
    {
    	if (is_string($value) && strpos($value, '@') === 0 && is_file(realpath(ltrim($value, '@')))) {
    		return true;
    	}
    	return false;
    }
    

      

  • 相关阅读:
    <!DOCTYPE html>的重要性!
    ibatis 常用标签
    string.match(RegExp) 与 RegExp.exec(string) 深入详解
    JavaScript RegExp.$1
    JavaScript RegExp.exec() 方法
    正则表达式常用符号说明
    正则表达式中/i,/g,/ig,/gi,/m的区别和含义
    JavaScript Math.floor() 方法
    JavaScript RegExp.test() 方法
    js日期格式化 扩展Date()
  • 原文地址:https://www.cnblogs.com/setevn/p/11810361.html
Copyright © 2011-2022 走看看