本身封裝的微信分享類

<?php

class WeixinShare{

    public function curlGet($url,$data){

        $ch = curl_init();

        // get的變量

        $getData="";

        foreach($data as $key=>$value){

            $getData.="$key=$value&";

        }

        $getData=substr($getData,0,strlen($getData)-1);

        $url.="?".$getData;


        //設置選項,包括URL

        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_HEADER, 0);


        //執行並獲取HTML文檔內容

        $output = curl_exec($ch);

        //釋放curl句柄

        curl_close($ch);

        $output=json_decode($output,true);

        return $output;

    }


    private  function getAccesssToken($appId,$appSecret,$force=false){

        $file=sys_get_temp_dir().'/access_token.json';

        @$data=file_get_contents($file);

        if($data)    $data=json_decode($data,true);

        $now=time();

        if(!$data || $data['create_time']+$data['expire_time']<=$now || $force){

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

            $data=array(

                'grant_type'=>'client_credential',

                'appid'=>$appId,

                'secret'=>$appSecret,

            );

            $result=$this->curlGet($url,$data);


            $data=array(

                'access_token'=>$result['access_token'],

                'expire_time'=>$result['expires_in']-1800,

                'create_time'=>time(),

            );

            $data=json_encode($data);

            file_put_contents($file,$data);

        }

        return $data['access_token'];

    }


    private  function createRandomStr($length=16){

        $str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';//62個字符

        $strlen = 62;

            while($length > $strlen){

            $str .= $str;

            $strlen += 62;

        }

        $str = str_shuffle($str);

        return substr($str,0,$length);

    }


    public function configWeixin(){

         $appId="APP_ID";

        $appSecret="APP_SECERT";

        $data=array();

        $data['appId']=$appId;

        $accessToken=$this->getAccesssToken($appId,$appSecret);

        $params=array(

            'access_token'=>$accessToken,

            'type'=>'jsapi'

        );

        $result=$this->curlGet("https://api.weixin.qq.com/cgi-bin/ticket/getticket",$params);

        $jsapiTicket=$result['ticket'];

        $nonceStr=$this->createRandomStr();

        $timestamp=time();

        $url=getgpc('shareUrl');

        $string1=sprintf("jsapi_ticket=%s&noncestr=%s&timestamp=%s&url=%s",$jsapiTicket,$nonceStr,$timestamp,$url);

        $data['signature']=sha1($string1);

        $data['nonceStr']=$nonceStr;

        $data['timestamp']=$timestamp;

        $share=array(

            'title'=>'分享標題',

            'link'=>'分享連接',

            'desc'=>'分享的文字說明',

            'imgUrl'=>'分享圖標的地址'

        );

        $data['share']=$share;

        echo json_encode($data);

    }

}
相關文章
相關標籤/搜索