php - 微信用户经过网页基本授权后jsdk 就报errmsg :"config:invalid signatrue"
PHP中文网
PHP中文网 2017-04-11 09:07:15
0
1
782

微信用户经过网页基本授权后jsdk 就报errmsg :"config:invalid signatrue"(前提我不经过网页基本授权进来是不会报错接口也可以用)

这是不经过基本授权jsdk是ok的

这是经过授权

代码:
//JS-SDK使用权限签名算法

  public function getJsapiTicket(){
      $jsapi=D('jsapi');
      $jsapi_data=$jsapi->selt_jsapi();
  foreach ($jsapi_data as  $v) {
      }
  if($v['time']>time()){
      return $v['jsapi'];
      }else{
      $access_token=$this->access_token();
      $urls="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi";
      $res=$this->http_curl($urls);
      $jsApi_Ticket=$res['ticket'];
      $con['jsapi']=$jsApi_Ticket;
      $con['time']=time()+7000;         
      $jsapi->creat_api($con);
  return $jsApi_Ticket;;   
  }

}

 //curl抓取
  public function http_curl($url,$type='get', $res='json',$postJson=''){//CURL
      $ch=curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
    if($type=='post'){
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $postJson);
    }
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
      $result=curl_exec($ch);
  if($res=='json'){
    if(curl_errno($ch)){
      var_dump(curl_error($ch)) ;
    }
      return json_decode($result,true);
  }   
         curl_close($ch); 
  }

//授权第一步

  public function getinfo(){
       $appid=C('APPID');   
       $redirect_uri=urlencode("http://love.cc.com/index.php/admin/index/getOpenid");
       $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=state#wechat_redirect";
       header('location:'.$url);
  }

//获取openid
public function getOpenid(){

      $urlt="http://love.cc.com/index.php/Admin/index/getOpenid";
      $jsapi_ticket=$this->getJsapiTicket();
      $noncestr=$this->getRandcode();
      $time=time();
      $signature="jsapi_ticket=".$jsapi_ticket."&noncestr=".$noncestr."&timestamp=".$time."&url=".$urlt;
      $signature=sha1($signature);
      var_dump($signature);
      $appid=C('APPID');
      $appsecret=C('APPSECRET');
      $code=$_GET['code'];
      $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code"; 
      $res=$this->http_curl($url);
      $con['openid']= $res['openid'];

      $img=D('purl');
      $data['data']=$img->show_data($con);
     // var_dump($data);
      $this->assign('openid',$con['openid']);
      $this->assign('time',$time);
      $this->assign('noncestr',$noncestr);
      $this->assign('signature',$signature);
      $this->assign($data);
      $this->display('index');  
  }
  
PHP中文网
PHP中文网

认证0级讲师

reply all(1)
巴扎黑
  // 注意 URL 一定要动态获取,不能 hardcode.
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template