1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace App\Service;
- use App\Log;
- use App\RedisModel;
- class BingtianService
- {
- const BASE_URI = 'https://admin-video.shruisong.net/';
- const SESSION_RDS_KEY = 'Playlet:BingTianSession';
- /*
- * 调用接口取得凭证
- * */
- public static function getSession()
- {
- $session = RedisModel::get(self::SESSION_RDS_KEY);
- if(empty($session)) {
- $session = self::setSession();
- }
- return $session;
- }
- public static function setSession($retry=0)
- {
- $url = self::BASE_URI . 'User/login?time=' . time() .' 257';
- $body = [
- 'username' => 'liedou',
- 'password' => '28IL7erA'
- ];
- $headers = [
- 'Origin:https://h5-video.shruisong.net',
- 'Accept-Encoding:gzip, deflate, br',
- 'Accept-Language:zh-CN,zh;q=0.9',
- 'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
- 'Content-Type:application/x-www-form-urlencoded;charset=UTF-8',
- 'Accept:application/json, text/plain, */*',
- 'Connection:keep-alive'
- ];
- $res = HttpService::curl_post($url, $body, $headers);
- $res = json_decode($res, true);
- $session = null;
- if( (isset($res['code']) && $res['code']=='0000' && isset($res['data']['session']) )){
- $session = $res['data']['session'];
- RedisModel::set(self::SESSION_RDS_KEY, $session);
- } elseif( $retry < 5) {
- $retry++;
- $session = self::setSession($retry);
- }
- if( !$session ){
- Log::logError('冰甜接口调用凭证获取失败', [
- 'res' => $res,
- 'retry' => $retry
- ], 'BingtSetSession');
- }
-
- return $session;
- }
- public static function crawlBingtApi($url, $body=[], $retry=0)
- {
- $session = BingtianService::getSession();
- if( !$session ){
- return false;
- }
- $headers = [
- 'Origin:https://h5-video.shruisong.net',
- 'Accept-Encoding:gzip, deflate, br',
- 'Accept-Language:zh-CN,zh;q=0.9',
- 'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
- 'Content-Type:application/x-www-form-urlencoded;charset=UTF-8',
- 'Accept:application/json, text/plain, */*',
- 'Connection:keep-alive',
- 'session:'. $session
- ];
- $res = HttpService::curl_post($url, $body, $headers);
- $res = json_decode($res, true);
- if(isset($res['code']) && $res['code']=='10004' && $retry<5 ){
- $retry++;
- //session过期,重置
- RedisModel::del(BingtianService::SESSION_RDS_KEY);
- $res = self::crawlBingtApi($url, $body, $retry);
- }
-
- return $res;
- }
- }
|