微信接口获取

protected function get_wx_access_token($appid, $secret) {

    $access_token =  cache('wx_access_token');

    if( !$access_token ) {

        $url = "https://api.weixin.qq.com/cgi-bin/token";

        /**
        "http://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx614d8f2a7695492e&secret=0a024487aa9bf60a79969ffe49b35198";
        **/

        $params = array(
              "grant_type" => "client_credential",
              "appid" => "***",                 
              "secret" => "***",   
        );
        $paramstring = http_build_query($params);
        $content = $this->httpcurl($url,$paramstring);
        $result = json_decode($content,true);
        if($result){

            if(isset($result['access_token'])) {

                $access_token = $result['access_token']; 
                cache('wx_access_token', $access_token, $result['expires_in']);

            } else if( isset($result['errcode']) ) {
                dump($result);
                exit('');
            }
        }

        // dump($result);
    }

    return $access_token;
}


protected function get_wx_jsapi_ticket() {

    $jsapi_ticket =  cache('wx_jsapi_ticket');

    if( !$jsapi_ticket ) {

        $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";

        $params = array(
              "access_token" => $this->get_wx_access_token(0,0),
              "type" => "jsapi",                 
        );
        $paramstring = http_build_query($params);
        $content = $this->httpcurl($url,$paramstring);
        $result = json_decode($content,true);
        if($result){

            if(isset($result['ticket'])) {

                if( $result['errcode'] == 0 ) {
                    $jsapi_ticket = $result['ticket']; 
                    cache('wx_jsapi_ticket', $jsapi_ticket, $result['expires_in']);
                }
            } 
        }

        // dump($result);
    }

    return $jsapi_ticket;
}


protected function wx_jsapi($url) {

    $appId        = 'wx614d8f2a7695492e';
    $jsapi_ticket = $this->get_wx_jsapi_ticket();
    $noncestr     = random(10);
    $timestamp    = time();
    $string       = "jsapi_ticket={$jsapi_ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$url}";
    $signature    = sha1($string);


    $this->assign('appId', $appId);
    $this->assign('noncestr', $noncestr);
    $this->assign('timestamp', $timestamp);
    $this->assign('signature', $signature);

}

    /**
 * 请求接口返回内容
 * @param  string $url [请求的URL地址]
 * @param  string $params [请求的参数]
 * @param  int $ipost [是否采用POST形式]
 * @return  string
 */
protected function httpcurl($url,$params=false,$ispost=0){
    $httpInfo = array();
    $ch = curl_init();

    curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false);
    // curl_setopt($ch, CURLOPT_HEADER, false);
    // curl_setopt($ch, CURLOPT_NOBODY, false);
    // curl_setopt( $ch, CURLOPT_USERAGENT , '' );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
    curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true);
    if( $ispost )
    {
        curl_setopt( $ch , CURLOPT_POST , true );
        curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
        curl_setopt( $ch , CURLOPT_URL , $url );
    }
    else
    {
        if($params){
            curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
        }else{
            curl_setopt( $ch , CURLOPT_URL , $url);
        }
    }
    $response = curl_exec( $ch );
    if ($response === FALSE) {
        echo "cURL Error: " . curl_error($ch);
        return false;
    }
    $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
    $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
    curl_close( $ch );
    return $response;
}
赞(0) 打赏
取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

    暂无评论...