您好,欢迎访问一九零五行业门户网

PHP处理微信SDK截取照片上传

这篇文章主要介绍了php处理微信sdk截取照片上传,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
php端处理tp3.2框架
一:类名:
namespace home\controller; use think\controller; use app\common\curl; class parentcontroller extends controller { public function __construct(){ parent::__construct(); $this->appid = '你的appid'; $this->appsecret = '你的appsecret'; $openid = session('openid'); // session("openid",'ogc7u1xrm3zoqjlz99o2cojjysru'); if(!$openid){ $this->get_openid(); } }
二:方法:
给客户端返回必要微信参数:
/** * 给客户端返回必要微信参数 * @author tghan * @datetime 2018-04-26 * @return [type] [description] */ public function modifyinfor() { $jsapiticket = $this->getjsapiticket(); // 注意 url 一定要动态获取,不能 hardcode. $protocol = (!empty($_server['https']) && $_server['https'] !== 'off' || $_server['server_port'] == 443) ? "https://" : "http://"; $url = "$protocol$_server[http_host]$_server[request_uri]"; $timestamp = time(); $noncestr = $this->createnoncestr(); // 这里参数的顺序要按照 key 值 ascii 码升序排序 $string = "jsapi_ticket=$jsapiticket&noncestr=$noncestr&timestamp=$timestamp&url=$url"; $signature = sha1($string); $signpackage = array( "appid" => $this->appid, "noncestr" => $noncestr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawstring" => $string ); $this->assign('signpackage',$signpackage); $this->display(); } public function createnoncestr($length = 16) { $chars ="abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789"; $str = ""; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } public function getjsapiticket() { // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例 $data =json_decode(file_get_contents("jsapi_ticket.json")); if ($data->expire_time < time()) { $accesstoken = $this->getaccesstoken(); // 如果是企业号用以下 url 获取 ticket // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accesstoken"; $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accesstoken"; $res = json_decode($this->httpget($url)); $ticket = $res->ticket; if ($ticket) { $data->expire_time = time() + 7000; $data->jsapi_ticket = $ticket; $fp = fopen("jsapi_ticket.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $ticket = $data->jsapi_ticket; } return $ticket; } public function getaccesstoken() { // access_token 应该全局存储与更新,以下代码以写入到文件中做示例 $data =json_decode(file_get_contents("access_token.json")); if ($data->expire_time < time()) { // 如果是企业号用以下url获取access_token // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appid&corpsecret=$this->appsecret"; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appid&secret=$this->appsecret"; $res = json_decode($this->httpget($url)); $access_token = $res->access_token; if ($access_token) { $data->expire_time = time() + 7000; $data->access_token = $access_token; $fp = fopen("access_token.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $access_token = $data->access_token; } return $access_token; } public function httpget($url) { $curl = curl_init(); curl_setopt($curl, curlopt_returntransfer, true); curl_setopt($curl, curlopt_timeout, 500); // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。 // 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。 curl_setopt($curl, curlopt_ssl_verifypeer, true); curl_setopt($curl, curlopt_ssl_verifyhost, true); curl_setopt($curl, curlopt_url, $url); $res = curl_exec($curl); curl_close($curl); return $res; }
三:方法:
图片上传
1,定义文件路径,写入图片流
2,从微信服务器获取图片流
3,图片上传,给客户端返回图片路径
/*图片 * 获取media_id */ public function upload_head(){ if(is_post){ $serverid = i('post.media_id'); if(!empty($serverid)){ $news_file = $this->dowechatpic( $serverid ); $this->ajaxreturn(array("is_success"=>"success","msg"=>"上传成功","url"=>$news_file)); }else{ $this->ajaxreturn(array("is_success"=>"error","msg"=>"上传失败1")); } } } /* * 从微信服务器获取图片流 */ public function dowechatpic($serverid){//media_id=jljs_iqioa-tkluhk4ncdpedxnj6paietoo8vr-wugvz05-6i5n498ezi232xsxn $media_id = $serverid;//提交过来的serverid即$media_id $access_token = $this->getaccesstoken(); $pic_url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$access_token}&media_id={$media_id}"; $filebody = file_get_contents($pic_url);//通过接口获取图片流 $filename = date("ymd").'_'.uniqid().'.jpg';//定义图片名字及格式 return $this->savefile($filename, $filebody); } /* * 定义文件路径,写入图片流 */ public function savefile($filename, $filecontent){ $upload_dir = "./public/static/images/headers";//保存路径,以时间作目录分层 $mkpath = $upload_dir; if(!is_dir($mkpath)){ if(!mkdir($mkpath)){ die('no mkdir power'); } if(!chmod($mkpath,0755)){//若服务器在阿里云上不建议使用0644 die('no chmod power'); } } $savepath = $upload_dir.'/'.$filename; if(file_put_contents($savepath, $filecontent)){//写入图片流生成图片 $news_file = substr($savepath,1); return $news_file;//返回图片路径 }else{ die('save failed'); } }
四:图片上传后,客户端将图片路径返回,插入数据库
public function save_child(){ $openid = session("openid"); $class_tid = session("class_tid"); if(is_post){ $data = array(); $data['cd_head'] = i('post.cd_head') ? i('post.cd_head') : ''; $data['cd_name'] = i('post.cd_name') ? i('post.cd_name') : ''; $data['cd_birthday'] = i('post.cd_birthday') ? i('post.cd_birthday') : ''; $data['cd_sex'] = i('post.cd_sex') ? i('post.cd_sex') : ''; $editdata = m("parent_child")->where("class_tid='{$class_tid}' and wx_openid='{$openid}'")->save($data); if($editdata !== false){ $this->ajaxreturn(array("is_success"=>"success","msg"=>"修改成功")); }else{ $this->ajaxreturn(array("is_success"=>"error","msg"=>"修改失败")); } } }
相关推荐:
php处理表单上传文件的方法
php处理bmp格式图片的步骤
以上就是php处理微信sdk截取照片上传的详细内容。
其它类似信息

推荐信息