目前我通过使用 hitbox.tv api 插件到我的网站来做一个项目。我想在直播期间更新我的视频标题。我使用guzzle 6向api端点发送http请求,以下是我的代码
myGuzzle.php
<?php
namespace myGuzzle;
require __DIR__.'/../vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
class myGuzzle{
public $params;
public $method;
public $endpoint;
public $body;
public $status;
public $errorJson;
function response($params=[],$method,$endpoint) {
$this->params = $params;
$this->method = $method;
$this->endpoint = $endpoint;
$client = new Client();
try{
$response = $client->request($this->method, $this->endpoint,['json' => $this->params]);
$this->body = $response->getBody();
$this->status = $response->getStatusCode();
}
catch(ClientException $e){
$this->body = $e->getMessage();
$this->status = $e->getCode();
$this->errorJson = $e->getResponse()->getBody();
}
}
function getStatus(){
return $this->status;
}
function getBody(){
return $this->body;
}
function getErrorJson(){
return $this->errorJson;
}
}
更新LiveMedia.php
namespace models;
use myGuzzle\myGuzzle;
class updateLiveMedia {
protected $username;
protected $access_token;
public $media_user_name;
public $media_id;
public $media_category_id;
public $media_live_delay;
public $media_hidden;
public $media_recording;
public $media_mature;
public $media_hosted_name;
public $media_countries = [];
public $media_status;
public $media_description;
function __construct($username,$acessToken) {
$this->username = $username;
$this->access_token = $acessToken;
$this->media_countries = ["EN"];
$this->media_category_id = '0';
$this->media_recording = "1";
$this->media_mature ='0';
$this->media_user_name = $this->username;
$this->media_hosted_name = "on";
$this->media_live_delay='2';
$this->media_hidden = '0';
}
/*
* Parameters function(title,descritption) or
* Parameters function(title,description,countries).
*/
function update($opts=[]){
if(is_array($opts)){
foreach($opts as $key=>$value){
$this->$key = $value;
}
}
$myGuzzle = new myGuzzle;
$uri = "https://api.hitbox.tv/media/live/".$this->username."?authToken=".$this->access_token;
$params = ["livestream"=>[
[
"media_user_name"=>$this->username,
"media_id"=>$this->media_id,
"media_category_id"=>$this->media_category_id,
"media_live_delay"=>$this->media_live_delay,
"media_hidden"=>$this->media_hidden,
"media_recording"=>$this->media_recording,
"media_mature"=>$this->media_mature,
"media_hosted_name"=>$this->media_hosted_name,
"media_countries"=> $this->media_countries,
"media_status"=>$this->media_status,
"media_description"=>$this->media_description,
]
]];
$myGuzzle->response($params,"PUT",$uri);
return $myGuzzle->getBody();
if($myGuzzle->getStatus() == 200){
return true;
}
return false;
}
}
运行 update() 返回以下结果
{"success":true,"error":false,"message":"host_mode_enabled"}
根据 hitbox.tv ( http://developers.hitbox.tv/#update-live-media ) 提供的文档应该返回如下内容
{
"livestream": [
{
"media_user_name": "masta",
"media_id": "1",
"media_category_id": "447",
"media_live_delay": "0",
"media_hidden": "0",
"media_recording": "1",
"media_mature": "0",
"media_countries": [
"EN"
],
"media_status": "This is a stream title!",
"media_description_md": null
}
]
}