zencoder/zencoder-php

PHP 的 Zencoder 集成库

v2.2.4 2019-09-05 18:44 UTC

README

公司: Brightcove/Zencoder

版本: 2.2.4

日期: 2019-09-05

仓库: http://github.com/zencoder/zencoder-php/

HTTP 头以不区分大小写的方式处理,以便与 HTTP2 兼容。

作者: [Nathan Sutton] (nsutton (a) brightcove (.) c�om)

公司: Brightcove/Zencoder

版本: 2.2.3

日期: 2014-07-29

仓库: http://github.com/zencoder/zencoder-php/

为了帮助解决用户无法修改 php.ini 以将 cURL 指向系统 CA 套件路径,或使用 5.3.7 之前的 PHP 版本的问题,我们已扩展此库,允许用户在提交请求使用的 cURL 连接上设置 CURLOPT_CAPATH 和 CURLOPT_CAINFO。

$zencoder = new Services_Zencoder($my_api_key, 'v2', 'https://app.zencoder.com', false, $my_curlopt_capath, $my_curlopt_cainfo);

有关可用参数的更多信息,请参阅 Services_Zencoder 构造函数。

有关获取 CA 套件的信息,请参阅 cURL CA 套件提取页面。我们建议使用 HTTPS 链接下载 CA 套件。

作者: [Zac Shenker] (zshenker (a) brightcove (.) c�om)

公司: Brightcove/Zencoder

版本: 2.2.0

日期: 2014-07-24

仓库: http://github.com/zencoder/zencoder-php/

由于捆绑的证书将于 2014 年 7 月 26 日到期,因此已从库中删除 Zencoder CA 链证书,并且不应需要它,因为 PHP/curl 应该能够使用内置的 CA 链。如有问题,请通过 help@zencoder.com 联系我们。

作者: [Michael Christopher] (mchristopher (a) brightcove (.) c�om)

公司: Zencoder - 在线视频编码器

版本: 2.1.1

日期: 2012-08-02

仓库: http://github.com/zencoder/zencoder-php/

此库的部分基于 http://github.com/twilio/twilio-php

有关 Zencoder API v2 的更多信息,请访问 http://blog.zencoder.com/2012/01/05/announcing-zencoder-api-v2/

有关 Zencoder API 要求的更多信息,请访问 https://support.brightcove.com/zencoder

要开始使用此库,请创建 Services_Zencoder 类的新实例,并将 API 密钥作为第一个参数传递。

$zencoder = new Services_Zencoder('93h630j1dsyshjef620qlkavnmzui3');

创建对象后,您可以使用它与 API 交互。有关完整信息,请参阅文档文件夹,但以下是一些可调用的函数的快速概述

$zencoder->accounts->create($array);
$zencoder->jobs->create($array);
$zencoder->jobs->progress($job_id);
$zencoder->inputs->details($input_id);
$zencoder->outputs->details($output_id);
$zencoder->notifications->parseIncoming();

任何错误都会抛出 Services_Zencoder_Exception。您可以在异常上调用 getErrors(),并将返回从 Zencoder API 收到的任何错误。

编码作业

ZencoderJob 对象使用 cURL 创建编码作业,将 JSON 格式的参数发送到 Zencoder 的编码 API。

步骤 1

访问您账户中的 API 构建器,并执行一个成功的编码作业。

步骤 2

复制成功的 JSON 字符串,从第一个大括号 "{" 开始,并将其作为参数传递给新的 ZencoderJob 对象。在您的服务器上执行脚本以测试其是否正常工作。

示例

<?php

// Make sure this points to a copy of Zencoder.php on the same server as this script.
require_once('Services/Zencoder.php');

try {
  // Initialize the Services_Zencoder class
  $zencoder = new Services_Zencoder('93h630j1dsyshjef620qlkavnmzui3');

  // New Encoding Job
  $encoding_job = $zencoder->jobs->create(
    array(
      "input" => "s3://bucket-name/file-name.avi",
      "outputs" => array(
        array(
          "label" => "web"
        )
      )
    )
  );

  // Success if we got here
  echo "w00t! \n\n";
  echo "Job ID: ".$encoding_job->id."\n";
  echo "Output ID: ".$encoding_job->outputs['web']->id."\n";
  // Store Job/Output IDs to update their status when notified or to check their progress.
} catch (Services_Zencoder_Exception $e) {
  // If were here, an error occurred
  echo "Fail :(\n\n";
  echo "Errors:\n";
  foreach ($e->getErrors() as $error) echo $error."\n";
  echo "Full exception dump:\n\n";
  print_r($e);
}

echo "\nAll Job Attributes:\n";
var_dump($encoding_job);

?>

步骤 3

修改上述脚本以满足您的需求。

您的 API 请求历史记录 可能会有所帮助。

您可以重新访问您的API构建器,以添加/更新JSON的参数。

您可以将JSON字符串转换为嵌套关联数组,以便您可以动态更改“输入”等属性。
前面的JSON示例将变为

$encoding_job = $zencoder->jobs->create(array(
  "input" => "s3://bucket-name/file-name.avi",
  "outputs" => array(
    array(
      "label" => "web"
    )
  )
));

通知处理

当输出文件完成后,ZencoderOutputNotification类用于捕获并解析从Zencoder发送到您的应用程序的JSON数据。

步骤 1

创建一个接收通知的脚本,并将其上传到您服务器上公开可访问的位置。

示例

<?php

// Make sure this points to a copy of Zencoder.php on the same server as this script.
require_once('Services/Zencoder.php');

// Initialize the Services_Zencoder class
$zencoder = new Services_Zencoder('93h630j1dsyshjef620qlkavnmzui3');

// Catch notification
$notification = $zencoder->notifications->parseIncoming();

// Check output/job state
if($notification->job->outputs[0]->state == "finished") {
  echo "w00t!\n";

  // If you're encoding to multiple outputs and only care when all of the outputs are finished
  // you can check if the entire job is finished.
  if($notification->job->state == "finished") {
    echo "Dubble w00t!";
  }
} elseif ($notification->job->outputs[0]->state == "cancelled") {
  echo "Cancelled!\n";
} else {
  echo "Fail!\n";
  echo $notification->job->outputs[0]->error_message."\n";
  echo $notification->job->outputs[0]->error_link;
}

?>

步骤 2

在编码作业的参数中,将您脚本的URL添加到每个要接收通知的输出的通知数组中。然后提交作业以测试其是否正常工作。

您可以在以下位置查看结果:
https://app.zencoder.com/notifications

示例

...
"outputs" => array(
  array(
    "label" => "web",
    "notifications" => array("http://example.com.com/encoding/notification.php")
  ),
  array(
    "label" => "iPhone",
    "notifications" => array("http://example.com.com/encoding/notification.php")
  )
)
...

步骤 3

修改上述脚本以满足您的需求。

您的通知页面将非常有用。

版本

Version 2.2.4 - 2019-09-05    HTTP headers are handled in a case-insensitve manner so that they are compatible with HTTP2.
Version 2.2.3 - 2014-07-29    Fixed the versions listed in the user agent and throughout the code
Version 2.2.2 - 2014-07-29    Fixed a bug where api_key was set as api_version in the http connection options
Version 2.2.1 - 2014-07-29    Support setting CURLOPT_CAPATH and CURLOPT_CAINFO on cURL connections.
Version 2.2.0 - 2014-07-24    Removing the bundled CA chain to address expiring intermediate certificate
Version 2.1.1 - 2012-08-02    Fixing issue where jobs index call didn't return jobs as individual objects
Version 2.1.0 - 2012-06-05    Adding support for job-level notifications & merging output with job in notification object
Version 2.0.2 - 2012-01-11    Fixed job creation response object, added documentation to variables
Version 2.0.1 - 2012-01-10    Added ability to get error info from API
Version 2.0   - 2012-01-02    Complete refactoring of library
Version 1.6   - 2011-10-24    Fixed issue with user agents in cURL
Version 1.4   - 2011-10-06    Fixed error with adding api_key to URL
Version 1.3   - 2011-09-21    Fixed bundled SSL certification chain and made catch_and_parse() static
Version 1.2   - 2011-08-06    Added fixes for PHP Notices and SSL handling
Version 1.1   - 2010-06-04    Added General API Requests
Version 1.0   - 2010-04-02    Jobs and Notifications.