php 微信分享可编辑分享标题、内容、图标

微信分享

1、服务端
//生成签名的随机串
function nonceStr($length){
$str = ‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’;//62个字符
s t r l e n = 62 ; w h i l e ( strlen = 62; while( strlen=62;while(length > $strlen){
$str .= $str;
$strlen += 62;
}
s t r = s t r s h u f f l e ( str = str_shuffle( str=strshuffle(str);
return substr( s t r , 0 , str,0, str,0,length);
}

//获取access_token
public function access_token(){
    $app_id = $this->config['app_id'];
    $app_secret = $this->config['app_secret'];
    $result = $this->get_by_curl("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$app_id."&secret=".$app_secret);
    $json = json_decode($result,true);
    $access_token = $json['access_token'];
    return $access_token;
}

//获取ticket
public function get_ticket(){
    $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=".$this->access_token();
    $res = json_decode($this->get_by_curl( $url ));
    $ticket = $res->ticket;
    return $ticket;
}

/**
 * 请求微信接口获取数据
 * @param $url
 * @param bool $post
 * @return bool|string
 */
function get_by_curl($url,$post = false){
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if($post){
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
    }
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

//分享获取config
function getWxConfig() {
    $appid = $this->config['app_id'];
    $jsapiTicket = $this->get_ticket();
    $url = '';
    $timestamp = time();
    $nonceStr = $this->nonceStr(16);
    $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
    $signature = sha1 ( $string );

    $WxConfig["appId"] = $appid;
    $WxConfig["nonceStr"] = $nonceStr;
    $WxConfig["timestamp"] = $timestamp;
    $WxConfig["url"] = $url;
    $WxConfig["signature"] = $signature;
    $WxConfig["rawString"] = $string;
    return json_encode($WxConfig);
}

2、前端

你可能感兴趣的:(PHP,php,微信,开发语言)