BaseApi.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace DouYinGameOpen;
  3. use think\Env;
  4. class BaseApi
  5. {
  6. const Douyin_Url = "https://open.douyin.com";
  7. public $app_id = null; // 抖音游戏ID
  8. public $app_secret = null; // 抖音游戏对应的密钥
  9. public $client_key = null;
  10. public $client_secret = null;
  11. public $response = null; // 返回内容
  12. public $error = null; // 报错内容
  13. public function __construct($config)
  14. {
  15. $this->app_id = $config['app_id'];
  16. $this->app_secret = $config['app_secret'];
  17. // $this->client_key = $config['client_key'];
  18. // $this->client_secret = $config['client_secret'];
  19. }
  20. public function toArray()
  21. {
  22. return $this->response ? json_decode($this->response, true) : true;
  23. }
  24. public function https_get($url, $params = [], $headers = null)
  25. {
  26. if ($params) {
  27. $url = $url . '?' . http_build_query($params);
  28. }
  29. $this->response = $this->https_request($url, null, $headers);
  30. $result = json_decode($this->response, true);
  31. return $result;
  32. }
  33. public function https_post($url, $data = [], $headers = null)
  34. {
  35. $header = [
  36. 'Accept' => 'application/json',
  37. 'Content-Type' => 'application/json'
  38. ];
  39. if($headers){
  40. $header = array_merge($header, $headers);
  41. }
  42. $this->response = $this->https_request($url, json_encode($data), $header);
  43. $result = json_decode($this->response, true);
  44. return $result;
  45. }
  46. public function https_request($url, $data = null, $headers = null)
  47. {
  48. $curl = curl_init();
  49. curl_setopt($curl, CURLOPT_URL, $url);
  50. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  51. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  52. if (!empty($data)) {
  53. curl_setopt($curl, CURLOPT_POST, 1);
  54. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  55. }
  56. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  57. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  58. if (!empty($headers)) {
  59. $headerArray = [];
  60. foreach ($headers as $key => $value) {
  61. $headerArray[] = $key . ': ' . $value;
  62. }
  63. curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
  64. }
  65. //设置curl默认访问为IPv4
  66. if (defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')) {
  67. curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  68. }
  69. $output = curl_exec($curl);
  70. curl_close($curl);
  71. return ($output);
  72. }
  73. public function https_byte($url, $file)
  74. {
  75. $payload = '';
  76. $params = "--__X_PAW_BOUNDARY__\r\n"
  77. . "Content-Type: application/x-www-form-urlencoded\r\n"
  78. . "\r\n"
  79. . $payload . "\r\n"
  80. . "--__X_PAW_BOUNDARY__\r\n"
  81. . "Content-Type: video/mp4\r\n"
  82. . "Content-Disposition: form-data; name=\"video\"; filename=\"123456.mp4\"\r\n"
  83. . "\r\n"
  84. . file_get_contents($file) . "\r\n"
  85. . "--__X_PAW_BOUNDARY__--";
  86. $first_newline = strpos($params, "\r\n");
  87. $multipart_boundary = substr($params, 2, $first_newline - 2);
  88. $request_headers = array();
  89. $request_headers[] = 'Content-Length: ' . strlen($params);
  90. $request_headers[] = 'Content-Type: multipart/form-data; boundary=' . $multipart_boundary;
  91. $curl = curl_init($url);
  92. curl_setopt($curl, CURLOPT_POST, 1);
  93. curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
  94. curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  95. curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);
  96. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  97. $output = curl_exec($curl);
  98. curl_close($curl);
  99. $result = json_decode($output, true);
  100. return $result;
  101. }
  102. }