简体   繁体   English

调用在线转换Rest API

[英]Call online-convert rest api

I'm using this service online-convert trying to make a simple call like in the online-convert sample. 我正在使用此服务进行在线转换,试图像在线转换示例中那样进行简单的调用。 However they do not have examples of actual code so I'm kinda in the dark. 但是,他们没有实际代码的示例,因此我有点茫然。 online-convert docs 在线转换文档

I have got that far, here is my best guess at what a call should look like: 我已经知道了,这是我对电话呼叫的最佳猜测:

<?php
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://api2.online-convert.com/jobs");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  "Host: https://api2.online-convert.com",
  "X-Oc-Api-Key: <my api key>",
  "Content-Type: application/json"
));

$response = curl_exec($ch);
curl_close($ch);

var_dump($response);

Any help will be appreciated. 任何帮助将不胜感激。

They are using a REST API to convert files. 他们正在使用REST API来转换文件。 Here is an example. 这是一个例子。

First create a json file with a link where the file you want to convert can be downloaded (uploads are handled differently). 首先创建一个带有链接的json文件,您可以在其中下载要转换的文件(上传的处理方式有所不同)。 Also add the format you want to convert to. 还添加您要转换为的格式。 Save it as test.json. 将其另存为test.json。

{
    "input": [{
        "type": "remote",
        "source": "https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png"
    }],
        "conversion": [{
        "category": "image",
        "target": "png"
    }]
}

Then send this file using curl to the API of online-convert.com . 然后使用curl将这个文件发送到online-convert.com的API。 Add your API key to the script below and save it as start.php in the same directory where you saved the test.json: 将您的API密钥添加到下面的脚本中,并将其另存为start.php,保存在test.json所在的目录中:

<?php
    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => "https://api2.online-convert.com/jobs",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS => file_get_contents('test.json'),
        CURLOPT_HTTPHEADER => array(
            "content-type: application/json",
            "x-oc-api-key: <your API key here>"
            ),
        )
    );

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo $response;
    }
?>

Execute the PHP file on the command line with 使用以下命令在命令行上执行PHP文件

php save.php

You can also call the script using a webbrowser. 您也可以使用网络浏览器调用脚本。

After you have successfully sent the job and got a valid response, you can obtain the status of the conversion. 成功发送作业并获得有效响应后,您可以获取转换状态。 For this you need the id (job id) you got in the answer when executing start.php. 为此,您需要在执行start.php时获得答案的ID(作业ID)。 Create a file called status.php and execute it. 创建一个名为status.php的文件并执行它。

<?php
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => "https://api2.online-convert.com/jobs/<your job id here>",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => array(
            "content-type: application/json",
            "x-oc-api-key: <your API key here>
            ),
        )
    );

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo $response;
    }
?>

There you will find the URL to download your file. 您会在此处找到下载文件的URL。

The API is much more powerful than that. 该API的功能远不止于此。 You can upload files you want to convert, create multiple conversions of one single file (eg videos in different resolution with one API call) and set various conversion options. 您可以上传要转换的文件,可以对一个文件进行多次转换(例如,使用一个API调用的不同分辨率的视频),还可以设置各种转换选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM