1 <?php$APPID=‘‘; 2 $APPSECRET=‘‘; 3 $info=json_decode(file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET")); 4 //获取token 5 $access_token=$info->access_token; 6 7 /* 使用curl函数 */ 8 $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=$access_token&type=image";; 9 $post_data = array( 10 ‘media‘ => ‘@bag03.png‘, 11 ); 12 $response = curl_http($url, ‘POST‘, $post_data); 13 $params = array(); 14 $params = json_decode($response,true); 15 if (isset($params[‘errcode‘])) 16 { 17 echo "error:" . $params[‘errcode‘]; 18 echo "msg :" . $params[‘errmsg‘]; 19 exit; 20 } 21 var_dump( $params ); 22 /** 23 * http请求方式: 默认GET 24 */ 25 function curl_http($url, $method="GET", $postfields){ 26 $ch=curl_init(); 27 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 28 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 29 curl_setopt($ch, CURLOPT_URL, $url); 30 switch ($method){ 31 case "POST": 32 curl_setopt($ch, CURLOPT_POST, true); 33 if (!empty($postfields)){ 34 $hadFile=false; 35 if (is_array($postfields) && isset($postfields[‘media‘])) { 36 /* 支持文件上传 */ 37 if (class_exists(‘\CURLFile‘)){ 38 curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); 39 foreach ($postfields as $key => $value) { 40 if (isPostHasFile($value)) { 41 $postfields[$key] = new \CURLFile(realpath(ltrim($value, ‘@‘))); 42 $hadFile = true; 43 } 44 } 45 }elseif (defined(‘CURLOPT_SAFE_UPLOAD‘)) { 46 if (isPostHasFile($value)) { 47 curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); 48 $hadFile = true; 49 } 50 } 51 } 52 $tmpdatastr = (!$hadFile && is_array($postfields)) ? http_build_query($postfields) : $postfields; 53 curl_setopt($ch, CURLOPT_POSTFIELDS, $tmpdatastr); 54 } 55 break; 56 default: 57 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); /* //设置请求方式 */ 58 break; 59 } 60 $ssl=preg_match(‘/^https:\/\//i‘,$url) ? TRUE : FALSE; 61 curl_setopt($ch, CURLOPT_URL, $url); 62 if($ssl){ 63 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts 64 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在 65 } 66 $response=curl_exec($ch); 67 curl_close($ch); 68 if(empty($response)){ 69 exit("错误请求"); 70 } 71 return $response; 72 } 73 74 function isPostHasFile($value) 75 { 76 if (is_string($value) && strpos($value, ‘@‘) === 0 && is_file(realpath(ltrim($value, ‘@‘)))) { 77 return true; 78 } 79 return false; 80 } 81 82 83 //命令不会返回错误 如果返回空 2>&1 输出报错信息 如果乱码就是用 header("content-type:text/html;charset=gbk"); curl下载地址 https://curl.haxx.se/download.html 84 85 $filepath=‘./bag03.png‘; 86 /* 使用exec函数 */ 87 $command = ‘curl -F media=@‘.$filepath.‘ "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=‘.$access_token.‘&type=image"‘; 88 $retval = array(); 89 exec($command, $retval, $status); 90 $params = array(); 91 $params = json_decode($retval[0],true); 92 if ($status != 0) { 93 $params = array( 94 ‘errcode‘ => ‘-100‘, 95 ‘errmsg‘ => ‘公众号服务出错,请联系管理员‘, 96 ); 97 } 98 return $params; 99 100 /* 使用system函数 */ 101 $command = ‘curl -F media=@‘.$filepath.‘ "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=‘.$access_token.‘&type=image" 2>&1‘; 102 $retval = 1; 103 104 $last_line = system($command, $retval); 105 $params = array(); 106 $params = json_decode($last_line,true); 107 if ($retval != 0) { 108 if (isset($params[‘errcode‘])) { 109 $params = array( 110 ‘errcode‘ => ‘-100‘, 111 ‘errmsg‘ => ‘公众号服务出错,请联系管理员‘, 112 ); 113 } 114 } 115 return $command;