Request.php 1.93 KB
<?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;
        }
    }
}