123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- /**
- * Author: luzheng.liu
- * Time: 2020/12/6 17:25
- */
- namespace app\common\service;
- use app\index\exception\ApiException;
- class SettingService {
- const TOKEN = 'Bearer a1aw1av25bk1usqjv7r84mw9knpv3sma';
- const BASE_URL = 'http://api.mudu.tv/';
- public function verifyChannelName(int $channelId, string $channelName) {
- // $client = new Client();
- // $res = $client->request('GET', 'http://api.mudu.tv/v1/activities/' . $channelId, [
- // 'headers' => [
- // 'Authorization' => self::TOKEN
- // ]
- // ]);
- $curl = curl_init();
- curl_setopt_array($curl, [
- CURLOPT_URL => 'http://api.mudu.tv/v1/activities/'.$channelId,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_ENCODING => '',
- CURLOPT_MAXREDIRS => 10,
- CURLOPT_TIMEOUT => 0,
- CURLOPT_FOLLOWLOCATION => true,
- CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
- CURLOPT_CUSTOMREQUEST => 'GET',
- CURLOPT_HTTPHEADER => [
- 'Authorization: '.self::TOKEN,
- 'Content-Type: application/json'
- ],
- ]);
- $response = curl_exec($curl);
- curl_close($curl);
- $info = json_decode($response, true);
- if ($channelName !== $info['name']) {
- throw new ApiException("频道名称不一致,频道{$channelId}名称为 " . $info['name']);
- }
- }
- public function setAuthUrl(int $channelId, string $redirectUrl) {
- // $client = new Client();
- // $res =$client->request('Post', self::BASE_URL.'v1/activities/'.$channelId.'/setAuth', [
- // 'headers' => [
- // 'Authorization' => self::TOKEN
- // ],
- // 'json' => [
- // 'method' => 'CUSTOM_VERIFY',
- // 'redirect_url' => $redirectUrl
- // ]
- // ]);
- $curl = curl_init();
- $data = [
- 'method' => 'CUSTOM_VERIFY',
- 'redirect_url' => $redirectUrl
- ];
- $data = json_encode($data);
- curl_setopt_array($curl, [
- CURLOPT_URL => 'http://api.mudu.tv/v1/activities/' . $channelId . '/setAuth',
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_ENCODING => '',
- CURLOPT_MAXREDIRS => 10,
- CURLOPT_TIMEOUT => 0,
- CURLOPT_FOLLOWLOCATION => true,
- CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
- CURLOPT_CUSTOMREQUEST => 'POST',
- CURLOPT_POSTFIELDS => $data,
- CURLOPT_HTTPHEADER => [
- "Authorization: ".self::TOKEN,
- 'Content-Type: application/json',
- ],
- ]);
- $response = curl_exec($curl);
- curl_close($curl);
- $info = json_decode($response, true);
- if ($info['status'] !== 'y') {
- throw new ApiException("设置自定义授权url失败--" . (string)$response);
- }
- }
- public function getAuthCode(int $channelId) {
- // $client = new Client();
- // $res = $client->request('Post', self::BASE_URL . 'v1/activities/' . $channelId . '/getAuthKey', [
- // 'headers' => [
- // 'Authorization' => self::TOKEN
- // ]
- // ]);
- $curl = curl_init();
- curl_setopt_array($curl, [
- CURLOPT_URL => 'http://api.mudu.tv/v1/activities/' . $channelId . '/getAuthKey',
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_ENCODING => '',
- CURLOPT_MAXREDIRS => 10,
- CURLOPT_TIMEOUT => 0,
- CURLOPT_FOLLOWLOCATION => true,
- CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
- CURLOPT_CUSTOMREQUEST => 'POST',
- CURLOPT_HTTPHEADER => [
- 'Authorization: '.self::TOKEN
- ],
- ]);
- $response = curl_exec($curl);
- curl_close($curl);
- $info = json_decode((string)$response, true);
- if (empty($info)) {
- throw new ApiException($info['message']);
- }
- return $info['authkey'];
- }
- }
|