| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- <?php
- namespace app\service;
- use app\middleware\Log;
- use think\facade\Request;
- use think\facade\Log as FacadeLog;
- class WeiboService
- {
- /**
- * @var mixed
- */
- public $signStr;
- /**
- * @var time
- */
- public $time;
- /**
- * @var string
- */
- private $appKey = 'h5vote';
- /**
- * @var string
- */
- private $appSecret = '8d6967d654bca47a';
- /**
- * 本类为底层微博侧相关接口请求代码示例
- */
- //@todo 切换为正式接口地址
- private static $base_url = 'https://ent.weibo.cn/open/wmac/';
- /**
- * @var mixed
- */
- private $uid;
- /**
- * @param $uid
- */
- public function __construct($uid = 0)
- {
- $this->uid = $uid;
- $this->appKey = env('weibo.api_key', 'h5vote');
- $this->appSecret = env('weibo.api_secret', '8d6967d654bca47a');
- $this->time = time();
- static::$base_url = env('weibo.api_host', 'https://ent.weibo.cn/open/wmac/');
- }
- /**
- * @param $url
- * @return mixed
- */
- 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 = round($end, $start, 4);
- FacadeLog::info(
- 'weibo_request|spentTime:' . $diff . '|uri|' . $url . '|response:' . $res
- );
- curl_close($curl);
- $res = json_decode($res, true);
- return $res;
- }
- /**
- * @param $url
- * @param array $requestData
- */
- 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 = round($end - $start, 4);
- FacadeLog::info(
- 'weibo_request|spentTime:' . $diff . '|uri|' . $url . '|response:' . $res
- );
- curl_close($curl);
- return json_decode($res, true);
- }
- //添加关注
- /**
- * @param $friends
- */
- 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);
- }
- //查询关注关系
- /**
- * @param $friends
- */
- 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);
- }
- //转发微博
- /**
- * @param $mid
- * @param $content
- */
- 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);
- }
- //给用户发放指定类型卡券
- /**
- * @param $num
- */
- 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);
- }
- //发原创微博
- /**
- * @param $content
- * @param $pic_id
- */
- 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);
- }
- //获取用户信息
- /**
- * @param $sub
- */
- public function userinfo($sub)
- {
- if (env('weibo.mock') == 1) {
- return [
- 'ok' => 1,
- 'msg' => '操作成功',
- 'data' => [
- 'uid' => mt_rand(1000, 9999),
- 'name' => substr(md5(time()), 0, 6),
- 'profile_image_url' => 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLZyicsRMQS6KouWddW4E0HZYfmwZNTiaE6QbgvTf5JJicvc1RsK0ibAXzDq8mOIVcK9tG9Zz1icXprY0Q/132',
- ],
- ];
- }
- $this->signStr = $this->signStr($this->time, $sub);
- $url = self::$base_url . 'token'. '?sign=' . $this->signStr . '&sub=' . $sub . '&time='.$this->time.'&secret='.$this->appSecret;
- return self::_httpGet($url);
- }
- /**
- * @param $uid
- */
- private function signStr($time, $sub)
- {
- $str = ($time>>8) . "8d6967d654bca47a" . $sub;
- FacadeLog::info('signStr: '.$str);
- return md5($str);
- }
- }
|