WeiboService.php 6.2 KB

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