PHP version of the micro-credit public number custom sharing content implementation Method _php instance

Source: Internet
Author: User
Tags sprintf ticket

This article describes the PHP version of the micro-public number custom sharing content implementation method. Share to everyone for your reference, specific as follows:

Micro-credit public number in the mobile phone through the API interface can be customized to share content, the following we look at the implementation of the interface steps.

I. Preparation stage

Public number one, micro website one.

Second, bound domain name

First login micro-trust public platform into the "Public number settings" in the "Functional settings" fill in "JS interface security Domain name."

Note: After logging in, you can view the corresponding interface permissions in the developer center.

Third, the Code

<?php//curl Fetch Request text content function get_curl_contents ($url, $method = ' get ', $data = Array ()) {if ($method = = ' POST ') {
    Use Crul to simulate $ch = Curl_init (); Disable Htt<a href= "/fw/photo.html" target= "_blank" >ps</a> <a href= "/tags.php/curl_setopt/" target= "_
    Blank ">curl_setopt</a> ($ch, Curlopt_ssl_verifypeer, FALSE);
    Allows the request to return curl_setopt ($ch, Curlopt_returntransfer, TRUE) as a file stream;
    curl_setopt ($ch, Curlopt_post, 1);
    curl_setopt ($ch, Curlopt_postfields, Http_build_query ($data));
    curl_setopt ($ch, Curlopt_connecttimeout, 30);
    curl_setopt ($ch, Curlopt_dns_cache_timeout, 30);
    curl_setopt ($ch, Curlopt_url, $url); $result = curl_exec ($ch);
  Execute send curl_close ($ch); }else {if Ini_get (' Allow_<a href= '/tags.php/fopen/' target= ' _blank ' >fopen</a>_url ') = = ' 1 ') {$res
    Ult = file_get_contents ($url);
      }else {//using Crul analog $ch = Curl_init (); Allows the request to return curl_setopt in the form of a file stream ($ch, Curlopt_returntransFER, TRUE);
      Disables HTTPS curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE);
      curl_setopt ($ch, Curlopt_url, $url); $result = curl_exec ($ch);
    Execute send curl_close ($ch);
} return $result; //Get the micro-credit Access_token function Wx_get_token () {$AppID = ' 1235464654 ';//appid (Application id) $AppSecret = ' 705641465sdfasdf45 6465a4sdf ';//appsecret (Application key) $url = ' https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential& Appid= '. $AppID. '
  &secret= '. $AppSecret;
  $res = Get_curl_contents ($url);
  $res = Json_decode ($res, true);
Here should be Access_token cache, as to how to cache to see everyone, the validity period is 7200s return $res [' Access_token ']; //Get the micro-credit ticket function Wx_get_jsapi_ticket () {$url = sprintf ("Https://api.weixin.qq.com/cgi-bin/ticket/getticket
  ? Access_token=%s&type=jsapi ", Wx_get_token ());
  $res = Get_curl_contents ($url);
  $res = Json_decode ($res, true);
Here should be Access_token cache, as to how to cache to see everyone, the validity period is 7200s return $res [' ticket '];
} $wx = Array (); The timestamp that generated the signature $wx [' timestamp '] = tIME ();
Generates a random string of signatures $wx [' noncestr '] = ' WM3WZYTPZ0WZCCNW '; Jsapi_ticket is the public number used to invoke the micro-letter JS interface of temporary bills.
Normally, the Jsapi_ticket is valid for 7,200 seconds and is obtained by Access_token.
$wx [' jsapi_ticket '] = Wx_get_jsapi_ticket ();
Share the address, note: Here is the URL of the current page, does not contain # and the back of the section, I was in the pit here, so the small partners to be careful $wx [' url '] = ' http://www.baidu.com '; $string = sprintf ("jsapi_ticket=%s&noncestr=%s&timestamp=%s&url=%s", $wx [' Jsapi_ticket '], $wx ['
Noncestr '], $wx [' timestamp '], $wx [' url ']);
Generate signature $wx [' signature '] = SHA1 ($string);
/* Note signing NONCESTR and timestamp must be the same as NONCESTR and timestamp in Wx.config.
The URL used for the signature must be the full URL of the call to the JS interface page.
For security reasons, the developer must implement the logic of the signature on the server side.

 * *?>

Four, view display

In the need to call the JS interface page to introduce the following JS file, support Https:http://res.wx.qq.com/open/js/jweixin-1.0.0.js

Inject the permission authentication configuration through the Config interface.

<script>//through Config interface inject permission authentication configuration wx.config ({debug:false, appId: ' AppId ', timestamp: ' <?php echo $wx ["Tim Estamp "];? > ', noncestr: ' <?php echo $wx ["noncestr"];? > ', Signature: ' <?php echo $wx ["signature"];?
> ', jsapilist: [' onmenusharetimeline ', ' onmenushareappmessage ', ' onmenushareqq ', ' Onmenushareweibo ']}; Wx.ready (function () {var s_title = ' Share title ',//share title S_link = ' Share link ',//share link S_DESC = ' share description ',//share description s _imgurl = ' share picture '; Share icon//friend Circle Wx.onmenusharetimeline ({title:s_title,//share title Link:s_link,//Share link imgurl:s_imgurl,//
  Share icon Success:function () {}, Cancel:function () {}}); Send to Friend Wx.onmenushareappmessage ({title:s_title,//share title Desc:s_desc,//Share description link:s_link,//share links I Mgurl:s_imgurl,//share icon type: ',//share type, music, video, or link, do not fill default for link dataurl: ',//if type is music or video, provide data link,
  The default is empty Success:function () {}, Cancel:function () {}}); QQ GoodFriend Wx.onmenushareqq ({title:s_title,//share title Desc:s_desc,//Share description link:s_link,//share links Imgurl:s_imgur
  L,//Share icon Success:function () {}, Cancel:function () {}});  Tencent Weibo Wx.onmenushareweibo ({title:s_title,//share title Desc:s_desc,//Share description link:s_link,//share links Imgurl:
S_imgurl,//Share icon Success:function () {}, Cancel:function () {}});
});

 </script>

Five, done

Basically the process is like this, the more troublesome point is to generate the signature piece, pay attention to the line.

For more information on PHP related content readers can view the site topics: "PHP micro-credit Development Skills summary", "PHP coding and transcoding Operation skills Summary", "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", "PHP string (String) Usage Summary", "php+ MySQL Database operations Introduction tutorial and PHP Common database operation Skills Summary

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.