convertio/convertio-php

该包最新版本(v0.4)没有提供许可证信息。

Convertio API的官方PHP包装器

v0.4 2017-06-11 01:38 UTC

This package is not auto-updated.

Last update: 2024-09-15 02:48:17 UTC


README

这是一个轻量级的Convertio API包装器。

您可以自由使用、改进或修改此包装器!如果您有问题,请联系我们或在GitHub上创建一个问题。

要求

开发者文档

您可以在以下位置找到完整的API参考:[https://convertio.co/api/docs/](https://convertio.co/api/docs/)

快速入门

以下示例将远程网页渲染为PNG图像

<?php
  require_once 'autoload.php';                      // Comment this line if you use Composer to install the package
  use \Convertio\Convertio;

  $API = new Convertio("_YOUR_API_KEY_");           // You can obtain API Key here: https://convertio.co/api/

  $API->startFromURL('http://google.com/', 'png')   // Convert (Render) HTML Page to PNG
  ->wait()                                          // Wait for conversion finish
  ->download('./google.png')                        // Download Result To Local File
  ->delete();                                       // Delete Files from Convertio hosts

以下示例将本地DOCX文件转换为PDF

<?php
  require_once 'autoload.php';                      // Comment this line if you use Composer to install the package
  use \Convertio\Convertio;

  $API = new Convertio("_YOUR_API_KEY_");           // You can obtain API Key here: https://convertio.co/api/
  $API->start('./input.docx', 'pdf')->wait()->download('./output.pdf')->delete();

以下示例将DOCX文件中的纯文本提取出来

<?php
  require_once 'autoload.php';                      // Comment this line if you use Composer to install the package
  use \Convertio\Convertio;

  $API = new Convertio("_YOUR_API_KEY_");           // You can obtain API Key here: https://convertio.co/api/
  $Text = $API->start('./test.docx', 'txt')->wait()->fetchResultContent()->result_content;
  $API->delete();
  echo $Text;

以下示例将覆盖默认API参数,如果您在PHP安装中没有启用SSL或想限制执行时间

<?php
  require_once 'autoload.php';                      // Comment this line if you use Composer to install the package
  use \Convertio\Convertio;

  $API = new Convertio("_YOUR_API_KEY_");           // You can obtain API Key here: https://convertio.co/api/
  $API->settings(array('api_protocol' => 'http', 'http_timeout' => 10));
  $API->startFromURL('http://google.com/', 'png')->wait()->download('./google.png')->delete();

OCR快速入门

以下示例将PDF的第1-3页、第5页、第7页转换为可编辑的DOCX,使用OCR(光学字符识别)对英语和西班牙语语言进行识别([https://convertio.co/api/docs/#ocr_langs](https://convertio.co/api/docs/#ocr_langs) 可用语言完整列表)

<?php
  require_once 'autoload.php';                      // Comment this line if you use Composer to install the package

  use \Convertio\Convertio;

  $API = new Convertio("_YOUR_API_KEY_");
  $API->start('./test.pdf', 'docx',                 // Convert PDF (which contain scanned pages) into editable DOCX
    [                                               // Setting Conversion Options (Docs: https://convertio.co/api/docs/#options)
      'ocr_enabled' => true,                        // Enabling OCR 
      'ocr_settings' => [                           // Defining OCR Settings
        'langs' => ['eng','spa'],                   // OCR language list (Full list: https://convertio.co/api/docs/#ocr_langs)
        'page_nums' => '1-3,5,7'                    // Page numbers to process (optional)
      ]
    ]
  )
  ->wait()                                          // Wait for conversion finish
  ->download('./test.docx')                         // Download Result To Local File
  ->delete();                                       // Delete Files from Convertio hosts

安装

您可以使用 Composer 或简单地 下载发布版本

Composer

首选方法是使用 composer。如果您还没有安装composer,请遵循 [https://getcomposer.org.cn/doc/00-intro.md](https://getcomposer.org.cn/doc/00-intro.md) 中的安装说明。

一旦安装了composer,请在您的项目根目录中执行以下命令以安装此库

  composer require convertio/convertio-php

最后,请确保包含自动加载器

<?php
  require_once '/path/to/your-project/vendor/autoload.php';

下载发布版本

您可以下载整个包。在 [https://github.com/convertio/convertio-php/releases](https://github.com/convertio/convertio-php/releases) 页面上列出了所有稳定版本。下载任何名称为 convertio-php-[RELEASE_NAME].zip 的文件,即可获得包含此库及其依赖项的包。解压缩您下载的zip文件,并在您的项目中包含自动加载器

<?php
  require_once '/path/to/convertio-php/autoload.php';
  use \Convertio\Convertio;
  $API = new Convertio("_YOUR_API_KEY_");
  //...

带有异常捕获的示例

以下示例显示了在转换过程中可能发生的不同异常类型

<?php
  require_once 'autoload.php';                       // Comment this line if you use Composer to install the package

  use \Convertio\Convertio;
  use \Convertio\Exceptions\APIException;
  use \Convertio\Exceptions\CURLException;

  try {
      $API = new Convertio("_YOUR_API_KEY_");
      $API->start('./test.pdf', 'docx')->wait()->download('test.docx')->delete();
  } catch (APIException $e) {
      echo "API Exception: " . $e->getMessage() . " [Code: ".$e->getCode()."]" . "\n";
  } catch (CURLException $e) {
      echo "HTTP Connection Exception: " . $e->getMessage() . " [CURL Code: ".$e->getCode()."]" . "\n";
  } catch (Exception $e) {
      echo "Miscellaneous Exception occurred: " . $e->getMessage() . "\n";
  }

带有回调URL的转换过程示例

以下示例适用于非即时转换,可能需要一些时间才能完成。在这种情况下,您可以定义回调URL([https://convertio.co/api/docs/#options_callback](https://convertio.co/api/docs/#options_callback) 更多信息),当转换完成时(无论是成功还是失败)都会收到通知

开始转换
<?php
  require_once 'autoload.php';                              // Comment this line if you use Composer to install the package

  use \Convertio\Convertio;
  use \Convertio\Exceptions\APIException;
  use \Convertio\Exceptions\CURLException;

  try {
      $API = new Convertio("_YOUR_API_KEY_");               // You can obtain API Key here: https://convertio.co/api/
      $API->start('./test.avi', 'hevc', [                   // Start AVI => HEVC conversion
          "callback_url" => "https://path/to/callback.php"  // Defined publicly available callback URL
      ]); 
  } catch (APIException $e) {
      echo "API Exception: " . $e->getMessage() . " [Code: ".$e->getCode()."]" . "\n";
  } catch (CURLException $e) {
      echo "HTTP Connection Exception: " . $e->getMessage() . " [CURL Code: ".$e->getCode()."]" . "\n";
  } catch (Exception $e) {
      echo "Miscellaneous Exception occurred: " . $e->getMessage() . "\n";
  }
回调处理程序示例

在此代码片段中的异常处理至关重要。转换错误会抛出APIException,必须妥善处理。请参阅 有关步骤参数的更多信息

<?php
  require_once 'autoload.php';                       // Comment this line if you use Composer to install the package

  use \Convertio\Convertio;
  use \Convertio\Exceptions\APIException;
  use \Convertio\Exceptions\CURLException;

  try {
      $API = new Convertio("_YOUR_API_KEY_");        // You can obtain API Key here: https://convertio.co/api/
      $API->__set('convert_id', $_GET['id']);        // Set Conversion ID
      if ($_GET['step'] == 'finished') {             // If conversion finished
          $API->download('test.hevc.mp4')            // Download result into local file
              ->delete();                            // Delete it from conversion server
      } else {                                       // Otherwise handle error in appropriate way
          echo "Conversion failed." . "\n";
      }       
  } catch (APIException $e) {
      echo "API Exception: " . $e->getMessage() . " [Code: ".$e->getCode()."]" . "\n";
  } catch (CURLException $e) {
      echo "HTTP Connection Exception: " . $e->getMessage() . " [CURL Code: ".$e->getCode()."]" . "\n";
  } catch (Exception $e) {
      echo "Miscellaneous Exception occurred: " . $e->getMessage() . "\n";
  }

按步骤拆分的转换过程示例

以下示例适用于非即时转换,可能需要一些时间才能完成。在这种情况下,您可以获取转换ID,稍后检查转换状态,省略 "->wait()" 调用,并使转换启动过程立即完成

开始转换
<?php
  require_once 'autoload.php';                          // Comment this line if you use Composer to install the package

  use \Convertio\Convertio;
  use \Convertio\Exceptions\APIException;
  use \Convertio\Exceptions\CURLException;

  try {
      $API = new Convertio("_YOUR_API_KEY_");           // You can obtain API Key here: https://convertio.co/api/
      $ConvertID = $API->start('./test.avi', 'hevc')    // Start AVI => HEVC conversion
                       ->getConvertID();                // Get the Conversion ID

  } catch (APIException $e) {
      echo "API Exception: " . $e->getMessage() . " [Code: ".$e->getCode()."]" . "\n";
  } catch (CURLException $e) {
      echo "HTTP Connection Exception: " . $e->getMessage() . " [CURL Code: ".$e->getCode()."]" . "\n";
  } catch (Exception $e) {
      echo "Miscellaneous Exception occurred: " . $e->getMessage() . "\n";
  }
检查转换状态并下载结果

在此代码片段中的异常处理至关重要。转换错误会抛出APIException,必须妥善处理。

<?php
  require_once 'autoload.php';                           // Comment this line if you use Composer to install the package

  use \Convertio\Convertio;
  use \Convertio\Exceptions\APIException;
  use \Convertio\Exceptions\CURLException;

  try {
      $API = new Convertio("_YOUR_API_KEY_");            // You can obtain API Key here: https://convertio.co/api/
      $API->__set('convert_id', $ConvertID);             // Set Conversion ID. $ConvertID is a string, obtained in previous snippet
      $API->status();                                    // Check status of the conversion

      if ($API->step == 'finish') {                      // If conversion finished
          $API->download('test.hevc.mp4')->delete();     // Save result into local file and download it from conversion server
      } else {                                           // Otherwise print some message
         echo "Conversion didn't finish yet." . "\n";
         echo "Check back later." . "\n";
      }

  } catch (APIException $e) {
      echo "API Exception: " . $e->getMessage() . " [Code: ".$e->getCode()."]" . "\n";
  } catch (CURLException $e) {
      echo "HTTP Connection Exception: " . $e->getMessage() . " [CURL Code: ".$e->getCode()."]" . "\n";
  } catch (Exception $e) {
      echo "Miscellaneous Exception occurred: " . $e->getMessage() . "\n";
  }

资源