sq_test.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /*
  3. 方倍工作室 http://www.cnblogs.com/txw1958/
  4. CopyRight 2014 All Rights Reserved
  5. */
  6. define("TOKEN", "eqcxcx");
  7. define("AppID", "wxd6972323f37bfdf0");//Appsecret
  8. define("Appsecret", "e7c30a645edec78aa64d30a92024237b");
  9. define("EncodingAESKey", "53o42sbbzslie4tjbll3ta5zlwe11rrk53o42sbbzsl");
  10. $ComponentVerifyTicket = file_get_contents('json/VerifyTicket.json');
  11. //echo "ComponentVerifyTicket=>". $ComponentVerifyTicket;
  12. //2、获取第三方平台component_access_token
  13. $token= getToken(AppID,Appsecret,$ComponentVerifyTicket,'11111');
  14. //echo "<br>";
  15. //echo "token=>".$token;
  16. //3获取预授权码pre_auth_code
  17. $data='{"component_appid":"'.AppID.'" }';
  18. $url="https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=".$token;
  19. $pre_auth_code=http_post_data($url,$data);
  20. //echo "<br>".$pre_auth_code;
  21. $pre_auth_code=json_decode($pre_auth_code,true)['pre_auth_code'];
  22. $redirect_url="http://web.woshangtong.com/addons/qidong_app/redirect_url.php";
  23. $openurl="https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=".AppID."&pre_auth_code=$pre_auth_code&redirect_uri=$redirect_url&auth_type=";
  24. //echo "<br>";
  25. //echo "pre_auth_code=>".$pre_auth_code;
  26. // echo "<br>";
  27. ?>
  28. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  29. <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-cn">
  30. <head>
  31. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  32. <title>智能客服-微信授权演示</title>
  33. <style type="text/css">
  34. body
  35. {
  36. background-image: url('http://web.woshangtong.com/attachment/images/global/fh0HERD4p8mr8pEsMmM0Gb9hENm9M8.jpg');
  37. background-repeat: repeat-y;
  38. }
  39. .align-center{
  40. position:fixed;left:40%;top:30%;margin-left:width/2;margin-top:height/2;
  41. }
  42. </style>
  43. </head>
  44. <body>
  45. <div class="align-center">
  46. <table >
  47. <tr>
  48. <th>
  49. <img width='200' src='logo.png'>
  50. </th>
  51. </tr>
  52. <tr>
  53. <th>
  54. <br>
  55. </th>
  56. </tr>
  57. <tr>
  58. <th>
  59. <? if ($pre_auth_code<>''){?>
  60. <a href='<?php echo $openurl ;?>'><img src='https://open.weixin.qq.com/zh_CN/htmledition/res/assets/res-design-download/icon_button3_1.png'></a>
  61. <? }else{ echo "请刷新本页.";}?>
  62. </th>
  63. </tr>
  64. </table>
  65. </div>
  66. </body>
  67. </html>
  68. <?php
  69. function http_post_data($url, $data_string) {
  70. $ch = curl_init();
  71. curl_setopt($ch, CURLOPT_POST, 1);
  72. curl_setopt($ch, CURLOPT_URL, $url);
  73. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  74. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  75. 'Content-Type: application/json; charset=utf-8',
  76. 'Content-Length: ' . strlen($data_string))
  77. );
  78. ob_start();
  79. curl_exec($ch);
  80. $return_content = ob_get_contents();
  81. //echo $return_content."<br>";
  82. ob_end_clean();
  83. $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  84. // return array($return_code, $return_content);
  85. return $return_content;
  86. }
  87. function getToken($appid,$appsecret,$ComponentVerifyTicket,$jsonid){
  88. $file = file_get_contents("jsonX/{$appid}_token.json",true);
  89. $result = json_decode($file,true);
  90. if (time() > $result['expires']){
  91. $data = array();
  92. $data['component_access_token'] = getNewToken($appid,$appsecret,$ComponentVerifyTicket);
  93. $data['expires']=time()+7200;
  94. $jsonStr = json_encode($data);
  95. $fp = fopen("jsonX/{$appid}_token.json", "w");
  96. fwrite($fp, $jsonStr);
  97. fclose($fp);
  98. return $data['component_access_token'];
  99. }else{
  100. return $result['component_access_token'];
  101. }
  102. }
  103. function getNewToken($appid,$appsecret,$ComponentVerifyTicket){
  104. $data_string='{
  105. "component_appid":"'.$appid.'" ,
  106. "component_appsecret": "'.$appsecret.'",
  107. "component_verify_ticket": "'.$ComponentVerifyTicket.'"
  108. }';
  109. $access_token_Arr =http_post_data("https://api.weixin.qq.com/cgi-bin/component/api_component_token", $data_string);
  110. //echo "<br>---";
  111. $access_token_Arr=json_decode($access_token_Arr,true);
  112. return $access_token_Arr['component_access_token'];
  113. }
  114. function zhucelist($path){
  115. $lines = file($path);
  116. $list='';
  117. foreach ($lines as $line) {
  118. $line = rtrim($line);
  119. $i++;
  120. $zhuce=json_decode($line, true);
  121. $list=$list.' '.$zhuce['name'];
  122. // more statements...
  123. }//如果没找到.
  124. return $list;
  125. }
  126. ?>