function handleImage($postObj) { //https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID $access_token = db_get_token_ticket(CFG_APPID, CFG_SECRET)["token"]; $mediaId = $postObj->MediaId; $url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token={$access_token}&media_id={$mediaId}"; $fileinfo = downloadWeixinFile($url); // $uploadPath = dirname(__FILE__) . "/upload/weixin/"; $tmpFile = time() . ".jpg"; saveWeixinFile($uploadPath . $tmpFile, $fileinfo["body"]); response_text($postObj, "图片上传完成"); }
function handleImage($postObj) { //https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID $access_token = db_get_token_ticket(CFG_APPID, CFG_SECRET)["token"]; $mediaId = $postObj->MediaId; $url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token={$access_token}&media_id={$mediaId}"; $fileinfo = downloadWeixinFile($url); // $uploadPath = dirname(__FILE__) . "/upload/weixin/"; $fileName = time() . ".jpg"; saveWeixinFile($uploadPath . $fileName, $fileinfo["body"]); response_text($postObj, "图片上传完成"); //保存图片信息 $params = array(array("" . $postObj->FromUserName, SQLSRV_PARAM_IN), array("" . $fileName, SQLSRV_PARAM_IN)); sp_execute("{call app_upload_image(?,?)}", $params); }
if (!isset($_SESSION["ccl"])) { echo "<script>location.href='mycanteen.php'</script>"; exit; } //接收AJAX请求注册 if ($_REQUEST["action"] == "reg") { $status = ""; unset($_SESSION["ccl"]); if (isset($_REQUEST["canteenName"])) { //download image if (isset($_REQUEST["mediaId"])) { $mediaId = $_REQUEST["mediaId"]; //https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID $access_token = db_get_token_ticket(CFG_APPID, CFG_SECRET)["token"]; $url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token={$access_token}&media_id={$mediaId}"; $fileinfo = downloadWeixinFile($url); // $uploadPath = dirname(dirname(__FILE__)) . "/upload/shop/"; $tmpFile = "aaa.jpg"; $time = time(); $ext = ".jpg"; saveWeixinFile($uploadPath . $tmpFile, $fileinfo["body"]); my_image_resize($uploadPath . $tmpFile, $uploadPath . $time . $ext, 375, 300); } // $canteenName = $_REQUEST["canteenName"]; $canteenTel = $_REQUEST["canteenTel"]; $picurl = $time . $ext; $cid = 0; //创建餐厅,并建立绑定关系,设置成管理员 $longitude = $_REQUEST["longitude"];
/** * 下载二维码接口,已调通。 * @param string $ticket_id 要下载的二维码ticket_id * @param string $savefinal 最终保存下载二维码图片的路径 * @return array $result 返回是否下载成功的信息数组 */ public function downloadQR($ticket_id = '', $savefinalpath = '') { $downloadresult = array('errCode' => 10001, 'errMsg' => "下载图片失败,请稍后再试!"); // 默认没下载成功 // 通过ticket下载二维码 $params['ticket'] = $ticket_id; //获取当前要下载的$ticket_id $fileInfo = downloadWeixinFile(self::DOWNLOAD_QRCODE, $params); // 调用downloadWeixinFile()函数请求微信服务器返回数据 // 微信端文件是否成功返回,如果文件出错 if (empty($fileInfo['body'])) { // 如果有错误信息 $downloadresult['errCode'] = $httpresult['errcode']; // 错误码给他 return $downloadresult; // 返回错误信息 } // 如果不出错,尝试下载多媒体文件,并写入本地磁盘文件 $localFile = fopen($savefinalpath, 'w'); //打开文件流,写文件方式 if ($localFile !== false) { // 如果创建文件成功,则写入文件 if (fwrite($localFile, $fileInfo['body']) !== false) { fclose($localFile); // 关闭文件读写流 // 将成功写入的信息返回 $downloadresult['errCode'] = 0; $downloadresult['errMsg'] = "ok"; $downloadresult['code_path'] = $savefinalpath; } } return $downloadresult; }
<?php header("content-type:text/html;charset=utf-8"); $cfg = array('appid' => "wx6b3d0038bcd9f337", 'appsecret' => "d4624c36b6795d1d99dcf0547af5443d"); $access_token = getAccessToken($cfg['appid'], $cfg['appsecret']); $tempewm = '{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 1234}}}'; $ticket = getTicket($access_token, $tempewm); //创建临时二维码票据 //用ticket换取二维码图片 $url2 = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" . urlencode($ticket); $imageInfo = downloadWeixinFile($url2); $filename = "qrcode.jpg"; $local_file = fopen($filename, 'w'); if (false !== $local_file) { if (false !== fwrite($local_file, $imageInfo["body"])) { echo '<img src="' . $filename . '" />'; fclose($local_file); } } function downloadWeixinFile($url) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_NOBODY, 0); //只取body头 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $package = curl_exec($ch); $httpinfo = curl_getinfo($ch); curl_close($ch);