| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace app\service;
- use app\middleware\Log;
- use think\facade\Log as FacadeLog;
- use think\facade\Request;
- class WeiboService
- {
- /**
- * 本类为底层微博侧相关接口请求代码示例
- */
- //@todo 切换为正式接口地址
- private static $base_url = 'https://ent.weibo.cn/movie/z/musicawards2021h5/';
- private $appKey = 'h5vote';
- private $appSecret = '8d6967d654bca47a';
- private $uid;
- public $signStr;
- public function __construct($uid = 0)
- {
- $this->uid = $uid;
- $this->signStr = $this->signStr($uid);
- $this->appKey = env('weibo.api_key', 'h5vote');
- $this->appSecret = env('weibo.api_secret', '8d6967d654bca47a');
- static::$base_url = env('weibo.api_host', 'https://ent.weibo.cn/movie/z/musicawards2021h5/');
- }
- private function signStr($uid)
- {
- $timestamp = time();
- $appSign = substr(strtolower(md5($this->appKey . $timestamp . $this->appSecret)), 0, 6);
- if ($uid > 0) {
- $uid_str = '&uid=' . $uid;
- } else {
- $uid_str = '';
- }
- return '?timestamp=' . $timestamp . '&appKey=' . $this->appKey . '&appSign=' . $appSign . $uid_str;
- }
- //获取用户信息
- public function userinfo()
- {
- if (env('weibo.mock') == 1) {
- return [
- 'code' => 10000,
- 'msg' => '操作成功',
- 'data' => [
- 'uid' => mt_rand(1000, 9999),
- 'screen_name' => substr(md5(time()), 0, 6),
- 'avatar' => 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLZyicsRMQS6KouWddW4E0HZYfmwZNTiaE6QbgvTf5JJicvc1RsK0ibAXzDq8mOIVcK9tG9Zz1icXprY0Q/132',
- ]
- ];
- }
- $url = self::$base_url . 'userinfo' . $this->signStr . '&sub=' . ($_COOKIE['SUB'] ?? Request::post('cookie'));
- return self::_httpGet($url);
- }
- //给用户发放指定类型卡券
- public function setcoupons($num)
- {
- if (env('weibo.mock') == 1) {
- return [
- 'code' => 10000,
- 'msg' => '操作成功',
- 'data' => null,
- ];
- }
- $url = self::$base_url . 'setcoupons' . $this->signStr;
- $data['uid'] = $this->uid;
- $data['data'] = $num;
- return self::_httpPost($url, $data);
- }
- //发原创微博
- public function status($content, $pic_id = '')
- {
- if (env('weibo.mock') == 1) {
- return [
- 'code' => 10000,
- 'msg' => '操作成功',
- 'data' => null,
- ];
- }
- $url = self::$base_url . 'status' . $this->signStr;
- $data['uid'] = $this->uid;
- $data['content'] = $content;
- if (!empty($pic_id)) {
- $data['pic_id'] = $pic_id;
- }
- return self::_httpPost($url, $data);
- }
- //转发微博
- public function repost($mid, $content)
- {
- if (env('weibo.mock') == 1) {
- return [
- 'code' => 10000,
- 'msg' => '操作成功',
- 'data' => null,
- ];
- }
- $url = self::$base_url . 'repost' . $this->signStr;
- $data['uid'] = $this->uid;
- $data['id'] = $mid;
- $data['content'] = $content;
- return self::_httpPost($url, $data);
- }
- //查询关注关系
- public function friends($friends)
- {
- if (env('weibo.mock') == 1) {
- return [
- 'code' => 10000,
- 'msg' => '操作成功',
- 'data' => [
- $friends => mt_rand(0, 1),
- ],
- ];
- }
- $url = self::$base_url . 'friends' . $this->signStr;
- $data['uid'] = $this->uid;
- $data['id'] = $friends;
- return self::_httpPost($url, $data);
- }
- //添加关注
- public function add($friends)
- {
- if (env('weibo.mock') == 1) {
- return [
- 'code' => 10000,
- 'msg' => '操作成功',
- 'data' => null,
- ];
- }
- $url = self::$base_url . 'add' . $this->signStr;
- $data['uid'] = $this->uid;
- $data['id'] = $friends;
- return self::_httpPost($url, $data);
- }
- public static function _httpGet($url = "")
- {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_TIMEOUT, 500);
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_REFERER, 'https://ent.weibo.cn'); //模拟来路
- curl_setopt($curl, CURLOPT_HTTPHEADER, [
- 'X-Forwarded-For' => get_client_ip(0),
- 'X-Request-Id' => Log::$logID,
- ]);
- FacadeLog::info(
- 'weibo_request|uri:' . $url
- );
- $start = microtime(true);
- $res = curl_exec($curl);
- $end = microtime(true);
- $diff = bcsub(strval($end), strval($start), 4);
- FacadeLog::info(
- 'weibo_request|spentTime:' . $diff . '|uri|' . $url . '|response:' . $res
- );
- curl_close($curl);
- $res = json_decode($res, true);
- return $res;
- }
- public static function _httpPost($url = "", $requestData = [])
- {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_REFERER, 'https://ent.weibo.cn'); //模拟来路
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_HTTPHEADER, [
- 'X-Forwarded-For' => get_client_ip(0),
- 'X-Request-Id' => Log::$logID,
- ]);
- //POST数据
- curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($requestData));
- FacadeLog::info(
- 'weibo_request|uri:' . $url . '|params:' . json_encode(
- http_build_query($requestData),
- JSON_UNESCAPED_UNICODE
- )
- );
- $start = microtime(true);
- $res = curl_exec($curl);
- $end = microtime(true);
- $diff = bcsub(strval($end), strval($start), 4);
- FacadeLog::info(
- 'weibo_request|spentTime:' . $diff . '|uri|' . $url . '|response:' . $res
- );
- curl_close($curl);
- return json_decode($res, true);
- }
- }
|