Link.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\extra\weMini;
  3. use yzh52521\EasyHttp\Http;
  4. class Link extends BasicWeChat
  5. {
  6. public function createQrcodeWx(string $path = "",string $query = "", string $filePath = "")
  7. {
  8. $url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token={$this->getAccessToken()}";
  9. $data = [
  10. "path" => $path."?".$query,
  11. 'width' => 430
  12. ];
  13. // 初始化cURL
  14. $ch = curl_init();
  15. curl_setopt($ch, CURLOPT_URL, $url);
  16. curl_setopt($ch, CURLOPT_POST, true);
  17. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  18. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  19. curl_setopt($ch, CURLOPT_HEADER, false);
  20. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  21. // 执行请求
  22. $response = curl_exec($ch);
  23. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  24. curl_close($ch);
  25. // 检查请求是否成功
  26. if ($httpCode != 200) {
  27. return false;
  28. }
  29. // 检查返回的是否是JSON(错误情况)
  30. $decode = json_decode($response, true);
  31. if ($decode && isset($decode['errcode'])) {
  32. error_log("微信接口返回错误: " . $decode['errmsg']);
  33. return false;
  34. }
  35. // 保存为JPG文件
  36. $filePath = public_path().$filePath;
  37. $result = file_put_contents($filePath, $response);
  38. if ($result === false) {
  39. error_log("文件保存失败: " . $filePath);
  40. return false;
  41. }
  42. return $filePath;
  43. // echo $url;
  44. // print_r($param);
  45. // return Http::asJson()->post($url,$param);
  46. }
  47. public function createQrcode(string $path = "",string $query = "", string $filePath = "")
  48. {
  49. $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={$this->getAccessToken()}";
  50. $data = [
  51. "path" => $path."?".$query,
  52. 'width' => 430
  53. ];
  54. // 初始化cURL
  55. $ch = curl_init();
  56. curl_setopt($ch, CURLOPT_URL, $url);
  57. curl_setopt($ch, CURLOPT_POST, true);
  58. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  59. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  60. curl_setopt($ch, CURLOPT_HEADER, false);
  61. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  62. // 执行请求
  63. $response = curl_exec($ch);
  64. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  65. curl_close($ch);
  66. // 检查请求是否成功
  67. if ($httpCode != 200) {
  68. return false;
  69. }
  70. // 检查返回的是否是JSON(错误情况)
  71. $decode = json_decode($response, true);
  72. if ($decode && isset($decode['errcode'])) {
  73. error_log("微信接口返回错误: " . $decode['errmsg']);
  74. return false;
  75. }
  76. // 保存为JPG文件
  77. $filePath = public_path().$filePath;
  78. $result = file_put_contents($filePath, $response);
  79. if ($result === false) {
  80. error_log("文件保存失败: " . $filePath);
  81. return false;
  82. }
  83. return $filePath;
  84. // echo $url;
  85. // print_r($param);
  86. // return Http::asJson()->post($url,$param);
  87. }
  88. public function createLink(string $path = "",string $query = "",string $version = "release")
  89. {
  90. $url = "https://api.weixin.qq.com/wxa/generate_urllink?access_token={$this->getAccessToken()}";
  91. $param = [
  92. "path" => $path,
  93. "query" => $query,
  94. "env_version" => $version
  95. ];
  96. return Http::asJson()->post($url,$param)->array();
  97. }
  98. public function createLinkJump(string $path = "",string $query = "",string $version = "release")
  99. {
  100. $url = "https://api.weixin.qq.com/wxa/generatescheme?access_token={$this->getAccessToken()}";
  101. $param = [
  102. "jump_wxa" => [
  103. "path" => $path,
  104. "query" => $query,
  105. "env_version" => $version
  106. ],
  107. ];
  108. print_r($param);
  109. return Http::asJson()->post($url,$param)->array();
  110. }
  111. public function createShortLink(string $path = "",string $query = "",string $version = "release")
  112. {
  113. $url = "https://api.weixin.qq.com/wxa/genwxashortlink?access_token={$this->getAccessToken()}";
  114. $param = [
  115. "page_url" => $path."?".$query,
  116. ];
  117. return Http::asJson()->post($url,$param)->array();
  118. }
  119. }