WeiboService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace app\service;
  3. use app\middleware\Log;
  4. use think\facade\Log as FacadeLog;
  5. class WeiboService
  6. {
  7. /**
  8. * 本类为底层微博侧相关接口请求代码示例
  9. */
  10. //@todo 切换为正式接口地址
  11. private static $base_url = 'https://ent.weibo.cn/movie/z/musicawards2021h5/';
  12. private $appKey = 'h5vote';
  13. private $appSecret = '8d6967d654bca47a';
  14. private $uid;
  15. public $signStr;
  16. public function __construct($uid = 0)
  17. {
  18. $this->uid = $uid;
  19. $this->signStr = $this->signStr($uid);
  20. $this->appKey = env('weibo.api_key', 'h5vote');
  21. $this->appSecret = env('weibo.api_secret', '8d6967d654bca47a');
  22. static::$base_url = env('weibo.api_host', 'https://ent.weibo.cn/movie/z/musicawards2021h5/');
  23. }
  24. private function signStr($uid)
  25. {
  26. $timestamp = time();
  27. $appSign = substr(strtolower(md5($this->appKey . $timestamp . $this->appSecret)), 0, 6);
  28. if ($uid > 0) {
  29. $uid_str = '&uid=' . $uid;
  30. } else {
  31. $uid_str = '';
  32. }
  33. return '?timestamp=' . $timestamp . '&appKey=' . $this->appKey . '&appSign=' . $appSign . $uid_str;
  34. }
  35. //获取用户信息
  36. public function userinfo()
  37. {
  38. if (env('weibo.mock') == 1) {
  39. return [
  40. 'code' => 10000,
  41. 'msg' => '操作成功',
  42. 'data' => [
  43. 'uid' => mt_rand(1000, 9999),
  44. 'screen_name' => substr(md5(time()), 0, 6),
  45. 'avatar' => 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLZyicsRMQS6KouWddW4E0HZYfmwZNTiaE6QbgvTf5JJicvc1RsK0ibAXzDq8mOIVcK9tG9Zz1icXprY0Q/132',
  46. ]
  47. ];
  48. }
  49. $url = self::$base_url . 'userinfo' . $this->signStr . '&sub=' . ($_COOKIE['SUB'] ?? $_GET['cookie']);
  50. return self::_httpGet($url);
  51. }
  52. //给用户发放指定类型卡券
  53. public function setcoupons($num)
  54. {
  55. if (env('weibo.mock') == 1) {
  56. return [
  57. 'code' => 10000,
  58. 'msg' => '操作成功',
  59. 'data' => null,
  60. ];
  61. }
  62. $url = self::$base_url . 'setcoupons' . $this->signStr;
  63. $data['uid'] = $this->uid;
  64. $data['data'] = $num;
  65. return self::_httpPost($url, $data);
  66. }
  67. //发原创微博
  68. public function status($content, $pic_id = '')
  69. {
  70. if (env('weibo.mock') == 1) {
  71. return [
  72. 'code' => 10000,
  73. 'msg' => '操作成功',
  74. 'data' => null,
  75. ];
  76. }
  77. $url = self::$base_url . 'status' . $this->signStr;
  78. $data['uid'] = $this->uid;
  79. $data['content'] = $content;
  80. if (!empty($pic_id)) {
  81. $data['pic_id'] = $pic_id;
  82. }
  83. return self::_httpPost($url, $data);
  84. }
  85. //转发微博
  86. public function repost($mid, $content)
  87. {
  88. if (env('weibo.mock') == 1) {
  89. return [
  90. 'code' => 10000,
  91. 'msg' => '操作成功',
  92. 'data' => null,
  93. ];
  94. }
  95. $url = self::$base_url . 'repost' . $this->signStr;
  96. $data['uid'] = $this->uid;
  97. $data['id'] = $mid;
  98. $data['content'] = $content;
  99. return self::_httpPost($url, $data);
  100. }
  101. //查询关注关系
  102. public function friends($friends)
  103. {
  104. if (env('weibo.mock') == 1) {
  105. return [
  106. 'code' => 10000,
  107. 'msg' => '操作成功',
  108. 'data' => [
  109. $friends => mt_rand(0, 1),
  110. ],
  111. ];
  112. }
  113. $url = self::$base_url . 'friends' . $this->signStr;
  114. $data['uid'] = $this->uid;
  115. $data['id'] = $friends;
  116. return self::_httpPost($url, $data);
  117. }
  118. //添加关注
  119. public function add($friends)
  120. {
  121. if (env('weibo.mock') == 1) {
  122. return [
  123. 'code' => 10000,
  124. 'msg' => '操作成功',
  125. 'data' => null,
  126. ];
  127. }
  128. $url = self::$base_url . 'add' . $this->signStr;
  129. $data['uid'] = $this->uid;
  130. $data['id'] = $friends;
  131. return self::_httpPost($url, $data);
  132. }
  133. public static function _httpGet($url = "")
  134. {
  135. $curl = curl_init();
  136. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  137. curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  138. curl_setopt($curl, CURLOPT_URL, $url);
  139. curl_setopt($curl, CURLOPT_REFERER, 'https://ent.weibo.cn'); //模拟来路
  140. curl_setopt($curl, CURLOPT_HTTPHEADER, [
  141. 'X-Forwarded-For' => get_client_ip(0),
  142. 'X-Request-Id' => Log::$logID,
  143. ]);
  144. FacadeLog::info(
  145. 'weibo_request|uri:' . $url
  146. );
  147. $start = microtime(true);
  148. $res = curl_exec($curl);
  149. $end = microtime(true);
  150. $diff = bcsub(strval($end), strval($start), 4);
  151. FacadeLog::info(
  152. 'weibo_request|spentTime:' . $diff . '|uri|' . $url . '|response:' . $res
  153. );
  154. curl_close($curl);
  155. $res = json_decode($res, true);
  156. return $res;
  157. }
  158. public static function _httpPost($url = "", $requestData = [])
  159. {
  160. $curl = curl_init();
  161. curl_setopt($curl, CURLOPT_URL, $url);
  162. curl_setopt($curl, CURLOPT_REFERER, 'https://ent.weibo.cn'); //模拟来路
  163. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  164. curl_setopt($curl, CURLOPT_HTTPHEADER, [
  165. 'X-Forwarded-For' => get_client_ip(0),
  166. 'X-Request-Id' => Log::$logID,
  167. ]);
  168. //POST数据
  169. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($requestData));
  170. FacadeLog::info(
  171. 'weibo_request|uri:' . $url . '|params:' . json_encode(
  172. http_build_query($requestData),
  173. JSON_UNESCAPED_UNICODE
  174. )
  175. );
  176. $start = microtime(true);
  177. $res = curl_exec($curl);
  178. $end = microtime(true);
  179. $diff = bcsub(strval($end), strval($start), 4);
  180. FacadeLog::info(
  181. 'weibo_request|spentTime:' . $diff . '|uri|' . $url . '|response:' . $res
  182. );
  183. curl_close($curl);
  184. return json_decode($res, true);
  185. }
  186. }