企微短剧业务系统

BingtianService.php 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\RedisModel;
  5. class BingtianService
  6. {
  7. const BASE_URI = 'https://admin-video.shruisong.net/';
  8. const SESSION_RDS_KEY = 'Playlet:BingTianSession';
  9. /*
  10. * 调用接口取得凭证
  11. * */
  12. public static function getSession()
  13. {
  14. $session = RedisModel::get(self::SESSION_RDS_KEY);
  15. if(empty($session)) {
  16. $session = self::setSession();
  17. }
  18. return $session;
  19. }
  20. public static function setSession($retry=0)
  21. {
  22. $url = self::BASE_URI . 'User/login?time=' . time() .' 257';
  23. $body = [
  24. 'username' => 'liedou',
  25. 'password' => '28IL7erA'
  26. ];
  27. $headers = [
  28. 'Origin:https://h5-video.shruisong.net',
  29. 'Accept-Encoding:gzip, deflate, br',
  30. 'Accept-Language:zh-CN,zh;q=0.9',
  31. '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',
  32. 'Content-Type:application/x-www-form-urlencoded;charset=UTF-8',
  33. 'Accept:application/json, text/plain, */*',
  34. 'Connection:keep-alive'
  35. ];
  36. $res = HttpService::curl_post($url, $body, $headers);
  37. $res = json_decode($res, true);
  38. $session = null;
  39. if( (isset($res['code']) && $res['code']=='0000' && isset($res['data']['session']) )){
  40. $session = $res['data']['session'];
  41. RedisModel::set(self::SESSION_RDS_KEY, $session);
  42. } elseif( $retry < 5) {
  43. $retry++;
  44. $session = self::setSession($retry);
  45. }
  46. if( !$session ){
  47. Log::logError('冰甜接口调用凭证获取失败', [
  48. 'res' => $res,
  49. 'retry' => $retry
  50. ], 'BingtSetSession');
  51. }
  52. return $session;
  53. }
  54. public static function crawlBingtApi($url, $body=[], $retry=0)
  55. {
  56. $session = BingtianService::getSession();
  57. if( !$session ){
  58. return false;
  59. }
  60. $headers = [
  61. 'Origin:https://h5-video.shruisong.net',
  62. 'Accept-Encoding:gzip, deflate, br',
  63. 'Accept-Language:zh-CN,zh;q=0.9',
  64. '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',
  65. 'Content-Type:application/x-www-form-urlencoded;charset=UTF-8',
  66. 'Accept:application/json, text/plain, */*',
  67. 'Connection:keep-alive',
  68. 'session:'. $session
  69. ];
  70. $res = HttpService::curl_post($url, $body, $headers);
  71. $res = json_decode($res, true);
  72. if(isset($res['code']) && $res['code']=='10004' && $retry<5 ){
  73. $retry++;
  74. //session过期,重置
  75. RedisModel::del(BingtianService::SESSION_RDS_KEY);
  76. $res = self::crawlBingtApi($url, $body, $retry);
  77. }
  78. return $res;
  79. }
  80. }