歡迎您光臨本站 註冊首頁

php獲取小程序碼的實現代碼(B類接口)

←手機掃碼閱讀     retouched @ 2020-06-14 , reply:0

效果圖

生成小程序碼的php代碼

  public function qrcode(){      $member_id = session('id');      if(empty($member_id)) $this->error('請先登錄');         //推廣二維碼      $member = model('Member')->where('id',$member_id)->find();      if($member['is_share'] && $member['share_qrcode']){        $litpic  = $member['share_qrcode'];      }else{        header('content-type:image/jpg');//加載速度快        // 生成小程序碼        $wechatObj =  new Wechat();//這是個類 這裡有小程序appid和密碼        $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$wechatObj->getAccessToken();           $page = 'pages/index/index';        $scene = 'share_id='.$member_id;        $path = './upload/qrcode/'.$member_id.'.jpg';               $postData = array();      $postData['page'] = $page;        $postData['scene'] = $scene;        $data = json_encode($postData);                 $result = $this->api_notice_increment($url,$data);        $image = 'data:image/jpg;base64,' . base64_encode($result);                 $other_result = $result;                 $file = fopen($path,"w");//打開文件準備寫入      fwrite($file,$other_result);//寫入      fclose($file);//關閉                 //return $result;        $litpic  = $path;        $litpic = ltrim($litpic,'.');        //寫入數據庫        $member->save(['share_qrcode'=>$litpic,'is_share'=>1]);      }         //推廣人數      $path_member = model('Member')->where('path',$member_id)->field('id,name,litpic,add_time')->select();      $path  = [];      foreach($path_member as $v){        $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']);        $path[]   = $v;      }      $data  = [        'litpic'  => $litpic,        'path'   => $path,      ];      return json($data);          }              public function api_notice_increment($url,$data){      //return $data;      $curl = curl_init(); // 啟動一個CURL會話      //$header = "Accept-Charset: utf-8";      curl_setopt($curl, CURLOPT_URL, $url); // 要訪問的地址      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 對認證證書來源的檢測      curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 從證書中檢查SSL加密算法是否存在      curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:')); //解決數據包大不能提交      curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自動跳轉      curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自動設置Referer      curl_setopt($curl, CURLOPT_POST, 1); // 發送一個常規的Post請求      curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的數據包      curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 設置超時限制防止死循      curl_setopt($curl, CURLOPT_HEADER, 0); // 顯示返回的Header區域內容      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 獲取的信息以文件流的形式返回         $tmpInfo = curl_exec($curl); // 執行操作      if (curl_errno($curl)) {        echo 'Errno'.curl_error($curl);      }      curl_close($curl); // 關鍵CURL會話      return $tmpInfo; // 返回數據            }          function api_notice_increment($url,$data)  {    $curl = curl_init();    $a = strlen($data);    $header = array("Content-Type: application/json; charset=utf-8","Content-Length: $a");    curl_setopt($curl, CURLOPT_URL, $url);    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);    curl_setopt($curl,CURLOPT_POST,1);    curl_setopt($curl,CURLOPT_POSTFIELDS,$data);    curl_setopt($curl, CURLOPT_HEADER, 0);    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);    $res = curl_exec($curl);    curl_close($curl);    return $res;     }

 

小程序端獲取二維碼中帶的參數

  /**     * 生命週期函數--監聽頁面加載     */    onLoad: function(option) {      console.log(option)      if(option.id){       this.setData({ id: option.id });       this.data.business_id = option.id;       this.loadData(option.id);      }         //接受二維碼掃碼並獲取二維碼中的參數      if (option.scene){       const ids = decodeURIComponent(option.scene).split('=')[1];       console.log("ids", ids);       this.setData({ id: ids });       this.data.business_id = ids;       this.loadData(ids);      }                 },

 

補充上wechat類

  wechatId            = 1;      $this->wechatAppid          = 'wx1161dbcdd18c52c2';      $this->wechatAppsecret        = 'f373410716a198feb462182c69facb8a';      $this->wechatMchid          = 1493574822;      $this->wechatPrivatekey        = md5(123);      */      //客戶appid      $this->wechatId            = 1;      $this->wechatAppid          = 'your appid';      $this->wechatAppsecret        = 'your appsecret';      $this->wechatMchid          = 商戶號;      $this->wechatPrivatekey        = '私鑰';                      /*        $this->wechatToken          = $WechatInfo['wechat_token'];        $this->wechatAccessToken       = $WechatInfo['wechat_access_token'];        $this->wechatAccessTokenTime     = $WechatInfo['wechat_access_token_time'];        $this->wechatJsapiTicket       = $WechatInfo['wechat_jsapi_ticket'];        $this->wechatJsapiTicketTime     = $WechatInfo['wechat_jsapi_ticket_time'];      */    }        // +----------------------------------------------------------------------  // |    獲取access_token  // +----------------------------------------------------------------------    public function getAccessToken(){         $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->wechatAppid.'&secret='.$this->wechatAppsecret;      $data = $this -> curlGet($url);      $access_token = $data['access_token'];      $expires_in  = $data['expires_in'];      $save['wechat_access_token']    = $access_token;      $save['wechat_access_token_time']  = ($expires_in+time())-360;      $this  ->  wechatAccessToken    = $save['wechat_access_token'];      $this  ->  wechatAccessTokenTime  = $save['wechat_access_token_time'];      return $access_token;       }    // +----------------------------------------------------------------------    // |    獲取access_token    // +----------------------------------------------------------------------    public function getJsapiTicket(){         $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$this -> getAccessToken().'&type=jsapi';      $data = $this -> curlGet($url);         $jsapi_ticket = $data['ticket'];      $expires_in  = $data['expires_in'];         $save['wechat_jsapi_ticket']    = $jsapi_ticket;      $save['wechat_jsapi_ticket_time']  = ($expires_in+time())-360;         $this->wechatJsapiTicket    = $save['wechat_jsapi_ticket'];      $this->wechatJsapiTicketTime  = $save['wechat_jsapi_ticket_time'];         return $jsapi_ticket;       }    // +----------------------------------------------------------------------    // |    獲取signature    // +----------------------------------------------------------------------    public function getSignature($appId,$timestamp,$nonceStr,$url)    {         $jsapi_ticket = $this -> getJsapiTicket();      $string1 = "jsapi_ticket={$jsapi_ticket}&noncestr={$nonceStr}&timestamp={$timestamp}&url={$url}";      $signature = sha1($string1);      return $signature;    }    // +----------------------------------------------------------------------    // |    獲取createNonceStr    // +----------------------------------------------------------------------    public function getCreateNonceStr($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 curlDownload($url,$name)    {      $ch = curl_init ();      curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );      curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );      curl_setopt ( $ch, CURLOPT_URL, $url );      ob_start ();      curl_exec ( $ch );      $return_content = ob_get_contents ();      ob_end_clean ();      $return_code = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );         $filename = "Uploads/Card/{$name}";      $fp= @fopen($filename,"a");      fwrite($fp,$return_content);      // 關閉URL請求      curl_close($ch);         $url = "/Uploads/Card/{$name}";         return "{$url}";    }    // +----------------------------------------------------------------------    // |    GET請求    // +----------------------------------------------------------------------    public function curlGet($url)    {        $ch = curl_init();        curl_setopt($ch, CURLOPT_URL, $url);        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        $output = curl_exec($ch);        curl_close($ch);        $jsoninfo = json_decode($output, true);        return $jsoninfo;    }    // +----------------------------------------------------------------------    // |    POST SSL請求    // +----------------------------------------------------------------------    public function curlPostSSL($url, $vars, $second=30,$aHeader=array()){      $ch = curl_init();      //超時時間      curl_setopt($ch,CURLOPT_TIMEOUT,$second);      curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);      //這裡設置代理,如果有的話      curl_setopt($ch,CURLOPT_URL,$url);      curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);      curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);      //curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');      curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/cert/apiclient_cert.pem');      //curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');      curl_setopt($ch,CURLOPT_SSLKEY,getcwd().'/cert/apiclient_key.pem');         if( count($aHeader) >= 1 ){        curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);      }         curl_setopt($ch,CURLOPT_POST, 1);      curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);      $data = curl_exec($ch);      if($data){        curl_close($ch);        return $data;      } else {        $error = curl_errno($ch);        echo "call faild, errorCode:$error ";        curl_close($ch);        return false;      }    }       // +----------------------------------------------------------------------    // | 發送退款    //  退款單號 out_trade_no    //  交易金額 total_fee    //  退款金額 refund_fee    // +----------------------------------------------------------------------    public function refund($out_trade_no,$total_fee,$refund_fee){         $arr['appid']      =  $this->wechatAppid;      $arr['mch_id']     =  $this->wechatMchid;      $arr['nonce_str']    =  $this->getNonceStr();      $arr['out_trade_no']  =  $out_trade_no;      $arr['out_refund_no']  =  $this->getNonceStr();      $arr['total_fee']    =  $total_fee;      $arr['refund_fee']   =  $refund_fee;      $arr['sign']      =  $this->MakeSign($arr);         //將統一下單數組 轉換xml      $xml            =  $this->ToXml($arr);            //post xml 到微信退款接口      $url  =  "https://api.mch.weixin.qq.com/secapi/pay/refund";;//微信退款地址,post請求      $ch=curl_init();      //需要獲取的URL地址,也可以在 curl_init() 函數中設置。      curl_setopt($ch,CURLOPT_URL,$url);      //啟用時會將頭文件的信息作為數據流輸出。      //curl_setopt($ch,CURLOPT_HEADER,1);      //將 curl_exec() 獲取的信息以文件流的形式返回,而不是直接輸出。      curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);      //證書檢查      curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);      //證書的類型。支持的格式有"PEM" (默認值), "DER"和"ENG"。      curl_setopt($ch,CURLOPT_SSLCERTTYPE,'pem');      //一個包含PEM格式證書的文件名。      curl_setopt($ch,CURLOPT_SSLCERT,GEN.'/cert/apiclient_cert.pem');      curl_setopt($ch,CURLOPT_SSLCERTTYPE,'pem');      curl_setopt($ch,CURLOPT_TIMEOUT,30);      //包含SSL私鑰的文件名。      curl_setopt($ch,CURLOPT_SSLKEY,GEN.'/cert/apiclient_key.pem');      curl_setopt($ch,CURLOPT_SSLCERTTYPE,'pem');      //一個保存著1個或多個用來讓服務端驗證的證書的文件名。這個參數僅僅在和 CURLOPT_SSL_VERIFYPEER 一起使用時才有意義。 .      // curl_setopt($ch,CURLOPT_CAINFO,getcwd().'/cert/rootca.pem');      curl_setopt($ch,CURLOPT_POST,1);      curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);      $data=curl_exec($ch);      if($data){        curl_close($ch);        $data_arr = json_decode(json_encode(simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA)), true);        return $data_arr;      }else{        $error =  curl_errno($ch);        return "curl 錯誤:".$error;      }    }                         // +----------------------------------------------------------------------    // | 企業付款    //  退款單號 out_trade_no    //  交易金額 total_fee    //  退款金額 refund_fee    // +----------------------------------------------------------------------    public function payment($partner_trade_no,$openid,$amount,$desc){         // 獲取      $arr['mch_appid']      =  $this->wechatAppid;      $arr['mchid']        =  $this->wechatMchid;      $arr['nonce_str']      =  $this->getNonceStr();      $arr['partner_trade_no']  =  $partner_trade_no;      $arr['openid']       =  $openid;      $arr['check_name']     =  "NO_CHECK";      $arr['amount']       =  $amount*100;      $arr['desc']        =  $desc;      $arr['spbill_create_ip']  =  request()->ip();      $arr['sign']        =  $this->MakeSign($arr);         //將統一下單數組 轉換xml      $xml            =  $this->ToXml($arr);         //post xml 到微信退款接口      $url  =  "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";//微信退款地址,post請求      $ch=curl_init();      //需要獲取的URL地址,也可以在 curl_init() 函數中設置。      curl_setopt($ch,CURLOPT_URL,$url);      //啟用時會將頭文件的信息作為數據流輸出。      //curl_setopt($ch,CURLOPT_HEADER,1);      //將 curl_exec() 獲取的信息以文件流的形式返回,而不是直接輸出。      curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);      //證書檢查      curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);      //證書的類型。支持的格式有"PEM" (默認值), "DER"和"ENG"。      curl_setopt($ch,CURLOPT_SSLCERTTYPE,'pem');      //一個包含PEM格式證書的文件名。      curl_setopt($ch,CURLOPT_SSLCERT,GEN.'/cert/apiclient_cert.pem');      curl_setopt($ch,CURLOPT_SSLCERTTYPE,'pem');      curl_setopt($ch,CURLOPT_TIMEOUT,30);      //包含SSL私鑰的文件名。      curl_setopt($ch,CURLOPT_SSLKEY,GEN.'/cert/apiclient_key.pem');      curl_setopt($ch,CURLOPT_SSLCERTTYPE,'pem');      //一個保存著1個或多個用來讓服務端驗證的證書的文件名。這個參數僅僅在和 CURLOPT_SSL_VERIFYPEER 一起使用時才有意義。 .      // curl_setopt($ch,CURLOPT_CAINFO,getcwd().'/cert/rootca.pem');      curl_setopt($ch,CURLOPT_POST,1);      curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);      $data=curl_exec($ch);      if($data){        curl_close($ch);        $data_arr = json_decode(json_encode(simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA)), true);        return $data_arr;      }else{        $error =  curl_errno($ch);        return "curl 錯誤:".$error;      }             }             // +----------------------------------------------------------------------    // |    POST請求    // +----------------------------------------------------------------------    public function curlPost($url,$post_data)    {      $ch = curl_init();      curl_setopt($ch, CURLOPT_URL, $url);      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);      if (!empty($post_data)){        curl_setopt($ch, CURLOPT_POST, 1);        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);      }      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);      $output = curl_exec($ch);         return $output;    }    // +----------------------------------------------------------------------    // |    齊力短信    // +----------------------------------------------------------------------    public function message($mobile){         $info  =  M('web')          ->  find();         $post_data = array();      $post_data['userid']  =  $info['message_id'];      $post_data['account']  =  $info['message_account'];      $post_data['password'] =  $info['message_password'];      $code = rand(1111,9999);      session('code',$code);      $post_data['content'] =   $info['message_autograph'].'您的驗證碼是:'.$code.' 請務必保管好,以免洩露';      $post_data['mobile'] = $mobile;      $post_data['sendtime'] = date('Y-m-d');      $url='http://pt.sdqlweb.com/sms.aspx?action=send';      $o='';      foreach ($post_data as $k=>$v)      {      $o.="$k=".urlencode($v).'&';      }      $post_data=substr($o,0,-1);      $ch = curl_init();      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);      curl_setopt($ch, CURLOPT_POST, 1);      curl_setopt($ch, CURLOPT_URL,$url);      curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);      $data = curl_exec($ch);      $data = json_decode(json_encode(simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA)), true);      curl_close($ch);      return $data;    }    // +----------------------------------------------------------------------    // |    以post方式提交xml到對應的接口url    // +----------------------------------------------------------------------    public function postXmlCurl($xml, $url, $useCert = false, $second = 30)    {      $ch = curl_init();      //設置超時      curl_setopt($ch, CURLOPT_TIMEOUT, $second);      curl_setopt($ch,CURLOPT_URL, $url);      curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);      curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//嚴格校驗      //設置header      curl_setopt($ch, CURLOPT_HEADER, FALSE);      //要求結果為字符串且輸出到屏幕上      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);      //post提交方式      curl_setopt($ch, CURLOPT_POST, TRUE);      curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);      //運行curl      $data = curl_exec($ch);      //返回結果      if($data){        curl_close($ch);        return $data;      } else {        $error = curl_errno($ch);        curl_close($ch);      }    }    // +----------------------------------------------------------------------    // |    輸出xml字符    // +----------------------------------------------------------------------    public function ToXml($array)    {      if(!is_array($array)        || count($array) $val)      {        if (is_numeric($val)){          $xml.="".$val."";        }else{          $xml.="";        }      }      $xml.="";      return $xml;    }    // +----------------------------------------------------------------------    // |    獲取微信RAS公鑰    // +----------------------------------------------------------------------    public function get_pub_key(){         $url        =  "https://fraud.mch.weixin.qq.com/risk/getpublickey";      $arr['mch_id']   =  $this->wechatMchid;      $arr['nonce_str']  =  $this->getNonceStr();      $arr['sign_type']  =  'MD5';      $arr['sign']    =  $this->MakeSign($arr);      $xml        =  $this->ToXml($arr);      $ch =  curl_init();      curl_setopt($ch,CURLOPT_URL,$url);      curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);      curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,1);      curl_setopt($ch,CURLOPT_SSLCERTTYPE,'pem');      curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/cert/apiclient_cert.pem');      curl_setopt($ch,CURLOPT_SSLCERTTYPE,'pem');      curl_setopt($ch,CURLOPT_SSLKEY,getcwd().'/cert/apiclient_key.pem');      curl_setopt($ch,CURLOPT_SSLCERTTYPE,'pem');      curl_setopt($ch,CURLOPT_CAINFO,getcwd().'/cert/rootca.pem');      curl_setopt($ch,CURLOPT_POST,1);      curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);      $data  =  $this->FromXml(curl_exec($ch));      //要創建的兩個文件      $TxtFileName = "./cert/public.pem";      //以讀寫方式打寫指定文件,如果文件不存則創建      if( ($TxtRes=fopen ($TxtFileName,"w+")) === false){        echo("創建可寫文件:".$TxtFileName."失敗");        exit();      }        echo ("創建可寫文件".$TxtFileName."成功!");        $StrConents =  $data['pub_key'];//要 寫進文件的內容      if(!fwrite ($TxtRes,$StrConents)){ //將信息寫入文件        echo ("嘗試向文件".$TxtFileName."寫入".$StrConents."失敗!");        fclose($TxtRes);        exit();      }        echo ("嘗試向文件".$TxtFileName."寫入".$StrConents."成功!");        fclose ($TxtRes); //關閉指針    }    // +----------------------------------------------------------------------    // |    將xml轉為array    // +----------------------------------------------------------------------    public function FromXml($xml)    {      //禁止引用外部xml實體      libxml_disable_entity_loader(true);      $this->values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);      return $this->values;    }    // +----------------------------------------------------------------------    // |    微信銀行卡編碼    // +----------------------------------------------------------------------    public function Cardcode($card_name)    {      $arr  =  array(        '工商銀行'=>1002,        '農業銀行'=>1005,        '中國銀行'=>1026,        '建設銀行'=>1003,        '招商銀行'=>1001,        '郵儲銀行'=>1066,        '交通銀行'=>1020,        '浦發銀行'=>1004,        '民生銀行'=>1006,        '興業銀行'=>1009,        '平安銀行'=>1010,        '中信銀行'=>1021,        '華夏銀行'=>1025,        '廣發銀行'=>1027,        '光大銀行'=>1022,        '北京銀行'=>1032,        '寧波銀行'=>1056      );         foreach($arr as $k=>$v){        if($k == $card_name){          return $v;        }      }    }    // +----------------------------------------------------------------------    // |    格式化參數格式化成url參數    // +----------------------------------------------------------------------    public function ToUrlParams($array)    {      $buff = "";      foreach ($array as $k => $v)      {        if($k != "sign" && $v != "" && !is_array($v)){          $buff .= $k . "=" . $v . "&";        }      }      $buff = trim($buff, "&");      return $buff;    }    // +----------------------------------------------------------------------    // |    生成簽名 本函數不覆蓋sign成員變量,如要設置簽名需要調用SetSign方法賦值    // +----------------------------------------------------------------------    public function MakeSign($array)    {      //簽名步驟一:按字典序排序參數      ksort($array);      $string =  $this->ToUrlParams($array);      //簽名步驟二:在string後加入KEY      $string =  $string."&key=".$this->wechatPrivatekey;      //簽名步驟三:MD5加密      $string =  md5($string);      //簽名步驟四:所有字符轉為大寫      $string =  strtoupper($string);      return $string;    }    // +----------------------------------------------------------------------    // |    產生的隨機字符串    // +----------------------------------------------------------------------    public function getNonceStr($length = 32)    {      $chars = "abcdefghijklmnopqrstuvwxyz0123456789";      $str ="";      for ( $i = 0; $i < $length; $i++ ) {        $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);      }      return $str;    }    // +----------------------------------------------------------------------    // |    打印log日誌    // +----------------------------------------------------------------------    public function save_log($msg){      error_log(date("Y-m-d H:i:s")."  ".print_r($msg,1)."         ",3,'./error.log');    }    // +----------------------------------------------------------------------    // |    將圖片上傳至微信服務器    // +----------------------------------------------------------------------    public function curlImg($images){         $url    =  "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=".$this->getAccessToken()."&type=image";      $ch1    =  curl_init ();      $timeout  =  5;      $real_path =  "{$_SERVER['DOCUMENT_ROOT']}{$images}";         $data= array("media"=>"@{$real_path}",'form-data'=>$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);        return $result;      }else {        return false;      }    }    // +----------------------------------------------------------------------    // |    將文章轉換為微信文章    // +----------------------------------------------------------------------    public function wechatText($content){      $parrent = "/appId&secret=$this->appSecret";        $res = json_decode($this->http_request($url));        $access_token = $res->access_token;        if ($access_token) {          $data->expire_time = time() + 7000;          $data->access_token = $access_token;          file_put_contents($token_file, json_encode($data));        }      } else {        $access_token = $data->access_token;      }      return $access_token;    }

 

感覺一個完整的PHP實現的代碼目前我還沒找到,這個自己用的還行。如有不恰當的地方,歡迎指出~ _


[retouched ] php獲取小程序碼的實現代碼(B類接口)已經有1132次圍觀

http://coctec.com/docs/php/show-post-238530.html