Request.php
1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
* Created by PhpStorm.
* User: DELL
* Date: 2016/8/26
* Time: 18:21
*/
namespace common\lib\YohonowApi;
use common\lib\Curl;
class Request
{
private static $success = 200000;
private static $failed = 400000;
private static $self;
public $errors;
const SET_LIVE_START = "yohoboyins/v5/qcloud/startLive";//?room_id=1001&living_stat=0
/**
* 实例化自身 单例
*/
public static function getInstance()
{
if (self::$self instanceof self === false) {
self::$self = new self();
}
return self::$self;
}
/**
* 设置开始结束状态
* @param $room_id 房间号
* @param $living_stat 直播状态 1直播,0 关闭
* @return bool
*/
public function setLiveStat($room_id, $living_stat)
{
$params = [
'room_id' => $room_id,
'living_stat' => $living_stat
];
return $this->_send(self::SET_LIVE_START, $params);
}
private function makeUrl($uri, array $data)
{
$domain = \Yii::$app->params['news_domain'];
$packageList = array();
foreach ($data as $key => $val) {
$packageList[] = trim($key . '=' . $val);
}
$url = sprintf("%s%s?%s", $domain, $uri, implode('&', $packageList));
return $url;
}
private function _send($uri, array $data)
{
file_put_contents("/tmp/yoholive.log",date('Y-m-d H:i:s')."|request yohonow,url:".$this->makeUrl($uri, $data),FILE_APPEND);
$response = Curl::get($this->makeUrl($uri, $data));
file_put_contents("/tmp/yoholive.log",date('Y-m-d H:i:s')."|request yohonow,result:".var_export($response,true),FILE_APPEND);
$response = json_decode($response,true);
if ($response['code'] == self::$success) {
return true;
} else {
$this->errors = $response['message'];
return false;
}
}
}