0

目前我通过使用 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
    }
  ]
}
4

1 回答 1

0

update-live-media 的 API 文档描述了仅在未修改托管标志时预期的响应。

其他情况将返回不同类型的答案:

  1. 启用主机模式时

    {"成功":true,"错误":false,"消息":"host_mode_enabled"}

  2. 当主机模式被禁用时

    {"成功":true,"错误":false,"消息":"host_mode_disabled"}

并且错误还会返回与文档不同的内容,例如:

  1. 如果authToken / media_id无效或权限不足

    {"success":true,"error":false,"message":"auth_required_token"} 或 {"success":true,"error":false,"message":"auth_required"}

当您更新托管模式时,您不会更新任何其他信息(标题)。只需尝试从您的查询中省略 media_hosted_name,然后观察描述和标题如何更新。用户(和聊天机器人)然后可以看到更新的信息。

如果您仍然对 API 有任何问题,请告诉我。我需要一个具体的例子,可以进一步帮助你。

于 2016-07-25T11:47:33.673 回答