1. 程式人生 > 其它 >小程式傳送請求獲取openid php後臺

小程式傳送請求獲取openid php後臺

 // 登入
    wx.login({
      success:function(res){
        if(res.code){
          console.log(res);
          wx.request({
            url:"http://xcx.com/api/Base/login",
            data:{code:res.code},
            header:{
              'content-type':'application/ison'
            },
            success:function(re){
              console.log(re)
            }
          })
        }
      }
    })

小程式傳送請求

 

TP5後臺介面程式碼

    /**
     * [login 獲取小程式openid]
     * @return [type] [description]
     */
    public function login(){
        $appid = "";
        $AppSecret = "";
        $code = $_GET["code"];
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$AppSecret
}&js_code={$code}&grant_type=authorization_code"; $data = $this->http_get($url); echo $data; } /** * [http_get 傳送請求] * @param [type] $url [description] * @return [type] [description] */ function http_get($url){ $curl = curl_init();//
初始化 curl_setopt($curl,CURLOPT_URL,$url); //設定得到的資料可以儲存 curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); //跳過https安全協議 curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE); curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,FALSE); $data = curl_exec($curl);//執行命令 curl_close($curl);//關閉url請求 return $data;//返回獲得的資料 }
appid ,AppSecret 需要你去微信開發平臺自己獲取,在開發管理,開發設定裡面,需要注意的是AppSecret 只會生成一次,記得儲存,忘了就需要重置了。