{"id":86,"date":"2021-07-30T15:43:57","date_gmt":"2021-07-30T07:43:57","guid":{"rendered":"http:\/\/blog.nonot.cn\/?p=86"},"modified":"2024-07-08T12:54:20","modified_gmt":"2024-07-08T04:54:20","slug":"%e3%80%90php%e3%80%91%e5%ae%9e%e7%8e%b0%e5%be%ae%e4%bf%a1%e5%88%86%e4%ba%ab%e6%9c%8b%e5%8f%8b%e5%9c%88demo%e6%ba%90%e7%a0%81","status":"publish","type":"post","link":"https:\/\/blog.nonot.cn\/index.php\/2021\/07\/30\/%e3%80%90php%e3%80%91%e5%ae%9e%e7%8e%b0%e5%be%ae%e4%bf%a1%e5%88%86%e4%ba%ab%e6%9c%8b%e5%8f%8b%e5%9c%88demo%e6%ba%90%e7%a0%81\/","title":{"rendered":"\u3010php\u3011\u5b9e\u73b0\u5fae\u4fe1\u5206\u4eab\u670b\u53cb\u5708demo\u6e90\u7801"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"> PHP\u5b9e\u73b0\u5fae\u4fe1\u5206\u4eab\u670b\u53cb\u5708<br>1\u3001\u5148\u52a0\u8f7d\u5fae\u4fe1\u81ea\u5e26\u7684\u63a5\u53e3\u4ee3\u7801 <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php \nclass JSSDK { \n  private $appId; \n  private $appSecret; \n \n  public function __construct($appId, $appSecret) { \n    $this->appId = $appId; \n    $this->appSecret = $appSecret; \n  } \n \n  public function getSignPackage() { \n    $jsapiTicket = $this->getJsApiTicket(); \n \n    \/\/ \u6ce8\u610f URL \u4e00\u5b9a\u8981\u52a8\u6001\u83b7\u53d6\uff0c\u4e0d\u80fd hardcode. \n    $protocol = (!empty($_SERVER['HTTPS']) &amp;&amp; $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? \"https:\/\/\" : \"http:\/\/\"; \n    $url = \"$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]\"; \n \n    $timestamp = time(); \n    $nonceStr = $this->createNonceStr(); \n \n    \/\/ \u8fd9\u91cc\u53c2\u6570\u7684\u987a\u5e8f\u8981\u6309\u7167 key \u503c ASCII \u7801\u5347\u5e8f\u6392\u5e8f \n    $string = \"jsapi_ticket=$jsapiTicket&amp;noncestr=$nonceStr\u00d7tamp=$timestamp&amp;url=$url\"; \n \n    $signature = sha1($string); \n \n    $signPackage = array( \n      \"appId\"     => $this->appId, \n      \"nonceStr\"  => $nonceStr, \n      \"timestamp\" => $timestamp, \n      \"url\"       => $url, \n      \"signature\" => $signature, \n      \"rawString\" => $string \n    ); \n    return $signPackage;  \n  } \n \n  private function createNonceStr($length = 16) { \n    $chars = \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\"; \n    $str = \"\"; \n    for ($i = 0; $i &lt; $length; $i++) { \n      $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); \n    } \n    return $str; \n  } \n \n  private function getJsApiTicket() { \n    \/\/ jsapi_ticket \u5e94\u8be5\u5168\u5c40\u5b58\u50a8\u4e0e\u66f4\u65b0\uff0c\u4ee5\u4e0b\u4ee3\u7801\u4ee5\u5199\u5165\u5230\u6587\u4ef6\u4e2d\u505a\u793a\u4f8b \n    $data = json_decode($this->get_php_file(\"jsapi_ticket.php\")); \n    if ($data->expire_time &lt; time()) { \n      $accessToken = $this->getAccessToken(); \n      \/\/ \u5982\u679c\u662f\u4f01\u4e1a\u53f7\u7528\u4ee5\u4e0b URL \u83b7\u53d6 ticket \n      \/\/ $url = \"https:\/\/qyapi.weixin.qq.com\/cgi-bin\/get_jsapi_ticket?access_token=$accessToken\"; \n      $url = \"https:\/\/api.weixin.qq.com\/cgi-bin\/ticket\/getticket?type=jsapi&amp;access_token=$accessToken\"; \n      $res = json_decode($this->httpGet($url)); \n      $ticket = $res->ticket; \n      if ($ticket) { \n        $data->expire_time = time() + 7000; \n        $data->jsapi_ticket = $ticket; \n        $this->set_php_file(\"jsapi_ticket.php\", json_encode($data)); \n      } \n    } else { \n      $ticket = $data->jsapi_ticket; \n    } \n \n    return $ticket; \n  } \n \n  private function getAccessToken() { \n    \/\/ access_token \u5e94\u8be5\u5168\u5c40\u5b58\u50a8\u4e0e\u66f4\u65b0\uff0c\u4ee5\u4e0b\u4ee3\u7801\u4ee5\u5199\u5165\u5230\u6587\u4ef6\u4e2d\u505a\u793a\u4f8b \n    $data = json_decode($this->get_php_file(\"access_token.php\")); \n    if ($data->expire_time &lt; time()) { \n      \/\/ \u5982\u679c\u662f\u4f01\u4e1a\u53f7\u7528\u4ee5\u4e0bURL\u83b7\u53d6access_token \n      \/\/ $url = \"https:\/\/qyapi.weixin.qq.com\/cgi-bin\/gettoken?corpid=$this->appId&amp;corpsecret=$this->appSecret\"; \n      $url = \"https:\/\/api.weixin.qq.com\/cgi-bin\/token?grant_type=client_credential&amp;appid=$this->appId&amp;secret=$this->appSecret\"; \n      $res = json_decode($this->httpGet($url)); \n      $access_token = $res->access_token; \n      if ($access_token) { \n        $data->expire_time = time() + 7000; \n        $data->access_token = $access_token; \n        $this->set_php_file(\"access_token.php\", json_encode($data)); \n      } \n    } else { \n      $access_token = $data->access_token; \n    } \n    return $access_token; \n  } \n \n  private function httpGet($url) { \n    $curl = curl_init(); \n    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); \n    curl_setopt($curl, CURLOPT_TIMEOUT, 500); \n    \/\/ \u4e3a\u4fdd\u8bc1\u7b2c\u4e09\u65b9\u670d\u52a1\u5668\u4e0e\u5fae\u4fe1\u670d\u52a1\u5668\u4e4b\u95f4\u6570\u636e\u4f20\u8f93\u7684\u5b89\u5168\u6027\uff0c\u6240\u6709\u5fae\u4fe1\u63a5\u53e3\u91c7\u7528https\u65b9\u5f0f\u8c03\u7528\uff0c\u5fc5\u987b\u4f7f\u7528\u4e0b\u97622\u884c\u4ee3\u7801\u6253\u5f00ssl\u5b89\u5168\u6821\u9a8c\u3002 \n    \/\/ \u5982\u679c\u5728\u90e8\u7f72\u8fc7\u7a0b\u4e2d\u4ee3\u7801\u5728\u6b64\u5904\u9a8c\u8bc1\u5931\u8d25\uff0c\u8bf7\u5230 http:\/\/curl.haxx.se\/ca\/cacert.pem \u4e0b\u8f7d\u65b0\u7684\u8bc1\u4e66\u5224\u522b\u6587\u4ef6\u3002 \n    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); \n    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); \n    curl_setopt($curl, CURLOPT_URL, $url); \n \n    $res = curl_exec($curl); \n    curl_close($curl); \n \n    return $res; \n  } \n \n  private function get_php_file($filename) { \n    return trim(substr(file_get_contents($filename), 15)); \n  } \n  private function set_php_file($filename, $content) { \n    $fp = fopen($filename, \"w\"); \n    fwrite($fp, \"&lt;?php exit();?>\" . $content); \n    fclose($fp); \n  } \n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"> 2\u3001\u4fee\u6539\u81ea\u5df1\u7684\u914d\u5236\u4fe1\u606f\u7528\u5fae\u4fe1\u6d4f\u89c8\u5668\u6253\u5f00\u5c31\u53ef\u4ee5\u4e86 <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php \nrequire_once \"jssdk.php\"; \n$appid = 'wx110'; \n$APPSECRET = '110'; \n$jssdk = new JSSDK($appid, $APPSECRET); \n$signPackage = $jssdk->GetSignPackage(); \n?> \n \n&lt;!DOCTYPE html> \n&lt;html lang=\"en\"> \n    &lt;head> \n        &lt;meta charset=\"UTF-8\"> \n        &lt;title>js\u5fae\u4fe1\u81ea\u5b9a\u4e49\u5206\u4eab\u6807\u9898\u3001\u94fe\u63a5\u548c\u56fe\u6807&lt;\/title> \n         &lt;meta name=\"keywords\" content=\"js\u5fae\u4fe1\u5206\u4eab,php\u5fae\u4fe1\u5206\u4eab\" \/> \n        &lt;meta name=\"description\" content=\"PHP\u81ea\u5b9a\u4e49\u5fae\u4fe1\u5206\u4eab\u5185\u5bb9\uff0c\u5305\u62ec\u6807\u9898\u3001\u56fe\u6807\u3001\u94fe\u63a5\u7b49\uff0c\u5206\u4eab\u6210\u529f\u548c\u53d6\u6d88\u6709js\u56de\u8c03\u51fd\u6570\u3002\" \/> \n    &lt;\/head> \n    &lt;body> \n \n    &lt;\/body> \n    &lt;script src=\"http:\/\/res.wx.qq.com\/open\/js\/jweixin-1.0.0.js\">&lt;\/script> \n    &lt;script> \n        \/* \n         * \u6ce8\u610f\uff1a \n         * 1. \u6240\u6709\u7684JS\u63a5\u53e3\u53ea\u80fd\u5728\u516c\u4f17\u53f7\u7ed1\u5b9a\u7684\u57df\u540d\u4e0b\u8c03\u7528\uff0c\u516c\u4f17\u53f7\u5f00\u53d1\u8005\u9700\u8981\u5148\u767b\u5f55\u5fae\u4fe1\u516c\u4f17\u5e73\u53f0\u8fdb\u5165\u201c\u516c\u4f17\u53f7\u8bbe\u7f6e\u201d\u7684\u201c\u529f\u80fd\u8bbe\u7f6e\u201d\u91cc\u586b\u5199\u201cJS\u63a5\u53e3\u5b89\u5168\u57df\u540d\u201d\u3002 \n         * 2. \u5982\u679c\u53d1\u73b0\u5728 Android \u4e0d\u80fd\u5206\u4eab\u81ea\u5b9a\u4e49\u5185\u5bb9\uff0c\u8bf7\u5230\u5b98\u7f51\u4e0b\u8f7d\u6700\u65b0\u7684\u5305\u8986\u76d6\u5b89\u88c5\uff0cAndroid \u81ea\u5b9a\u4e49\u5206\u4eab\u63a5\u53e3\u9700\u5347\u7ea7\u81f3 6.0.2.58 \u7248\u672c\u53ca\u4ee5\u4e0a\u3002 \n         * 3. \u5e38\u89c1\u95ee\u9898\u53ca\u5b8c\u6574 JS-SDK \u6587\u6863\u5730\u5740\uff1ahttp:\/\/mp.weixin.qq.com\/wiki\/7\/aaa137b55fb2e0456bf8dd9148dd613f.html \n         * \n         * \u5f00\u53d1\u4e2d\u9047\u5230\u95ee\u9898\u8be6\u89c1\u6587\u6863\u201c\u9644\u5f555-\u5e38\u89c1\u9519\u8bef\u53ca\u89e3\u51b3\u529e\u6cd5\u201d\u89e3\u51b3\uff0c\u5982\u4ecd\u672a\u80fd\u89e3\u51b3\u53ef\u901a\u8fc7\u4ee5\u4e0b\u6e20\u9053\u53cd\u9988\uff1a \n         * \u90ae\u7bb1\u5730\u5740\uff1aweixin-open@qq.com \n         * \u90ae\u4ef6\u4e3b\u9898\uff1a\u3010\u5fae\u4fe1JS-SDK\u53cd\u9988\u3011\u5177\u4f53\u95ee\u9898 \n         * \u90ae\u4ef6\u5185\u5bb9\u8bf4\u660e\uff1a\u7528\u7b80\u660e\u7684\u8bed\u8a00\u63cf\u8ff0\u95ee\u9898\u6240\u5728\uff0c\u5e76\u4ea4\u4ee3\u6e05\u695a\u9047\u5230\u8be5\u95ee\u9898\u7684\u573a\u666f\uff0c\u53ef\u9644\u4e0a\u622a\u5c4f\u56fe\u7247\uff0c\u5fae\u4fe1\u56e2\u961f\u4f1a\u5c3d\u5feb\u5904\u7406\u4f60\u7684\u53cd\u9988\u3002 \n         *\/ \n        wx.config({ \n            debug: false, \n            appId: '&lt;?php echo $appid; ?>', \n            timestamp: &lt;?php echo $signPackage[\"timestamp\"]; ?>, \n            nonceStr: '&lt;?php echo $signPackage[\"nonceStr\"]; ?>', \n            signature: '&lt;?php echo $signPackage[\"signature\"]; ?>', \n            jsApiList: [ \n               'onMenuShareTimeline' \n            ] \n        }); \n        wx.ready(function() { \n            wx.onMenuShareTimeline({ \n                title: '\u4e8c\u5f53\u5bb6\u7684', \/\/ \u5206\u4eab\u6807\u9898 \n                link: 'http:\/\/www.erdangjiade.com\/', \/\/ \u5206\u4eab\u94fe\u63a5 \n                imgUrl: '', \/\/ \u5206\u4eab\u56fe\u6807 \n                success: function() { \n                    \/\/ \u7528\u6237\u786e\u8ba4\u5206\u4eab\u540e\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570 \n                }, \n                cancel: function() { \n                    \/\/ \u7528\u6237\u53d6\u6d88\u5206\u4eab\u540e\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570 \n                } \n            }); \n        }); \n    &lt;\/script> \n    &lt;p style=\"text-align: center;color:red;font-size:20px;margin-top: 120px\">\u8bf7\u7528\u5fae\u4fe1\u6d4f\u89c8\u5668\u6253\u5f00\uff0c\u5e76\u6253\u5f00\u53f3\u4e0a\u65b9\u6309\u94ae\u3002\u5206\u4eab\u5230\u670b\u53cb\u5708\u8bd5\u8bd5\u3002&lt;\/p> \n&lt;\/html><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u5b9e\u73b0\u5fae\u4fe1\u5206\u4eab\u670b\u53cb\u57081\u3001\u5148\u52a0\u8f7d\u5fae\u4fe1\u81ea\u5e26\u7684\u63a5\u53e3\u4ee3\u7801 2\u3001\u4fee&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-86","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/86","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/comments?post=86"}],"version-history":[{"count":1,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"predecessor-version":[{"id":1220,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/86\/revisions\/1220"}],"wp:attachment":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}