WeiboService.php 6.6 KB

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