cURL with API URL and result URL in PHP?

78
May 19, 2022, at 08:50 AM

I want to use an API with cURL in PHP, but I don't know how to call one URL for sending, and one URL for receiving data. I want to convert images to webp, I send jpeg, jpg, png pictures to the API, and I want to download the webp version of it.

This is the API URL: https://api.webp.to/v1/convert/ And this is the result URL: https://api.webp.to/v1/results/

I wrote this code:

    $picture="picture.jpg";
    $fp = fopen("picture.webp", "w+");
    $data = array(
        'lang' => 'en',
        'convert_to' => 'jpg-webp',
        'ocr' => false,
        'file_contents' => curl_file_create($picture)
    );
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "https://api.webp.to/v1/convert/");
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        "Authorization: <API_KEY_INSERTED_HERE>" 
    ));
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl, CURLOPT_FILE, $fp);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 0);
    curl_exec($curl);

I need to insert somewehere a result URL, because it is different from API URL. I tried to get redirect info rom the curl_exec, but there is no redirect in the returning data.

Here is the API which I use: https://webp.to/api

They provide only Python code:

import requests
import time
import shutil
import json
headers = {'Authorization': 'f134382194a844c8bb589af58ef283e9'}
file_list = ['testfiles/blah.webp', 'testfiles/blah.webp', 'testfiles/blah.webp']
params = {
    'lang': 'en',
    'convert_to': 'webp-docx',
    'ocr': False
}
api_url = 'https://api.webp.to/v1/convert/'
results_url = 'https://api.webp.to/v1/results/'

def download_file(url, local_filename):
    with requests.get("https://api.webp.to/%s" % url, stream=True) as r:
        with open(local_filename, 'wb') as f:
            shutil.copyfileobj(r.raw, f)
    return local_filename

def convert_files(api_url, params, headers):
    files = [eval(f'("files", open("{file}", "rb"))') for file in file_list]
    print(files)
    r = requests.post(
        url=api_url,
        files=files,
        data=params,
        headers=headers
    )
    return r.json()

def get_results(params):
    if params.get('error'):
        return params.get('error')
    r = requests.post(
        url=results_url,
        data=params
    )
    data = r.json()
    finished = data.get('finished')
    while not finished:
        if int(data.get('queue_count')) > 0:
            print('queue: %s' % data.get('queue_count'))
        time.sleep(5)
        results = get_results(params)
        print(results)
        results = json.dumps(results)
        if results:
            break
    if finished:
        print(data.get('files'))
        for f in data.get('files'):
            print(f.get('url'))
            download_file("%s" % f.get('url'), "%s" % f.get('filename'))
        return {"finished": "files downloaded"}
    return r.json()

get_results(convert_files(api_url, params, headers))

Thank you for help!

Rent Charter Buses Company
READ ALSO
Bold particular text in string on runtime jQuery

Bold particular text in string on runtime jQuery

My requirement is : Thers's an input fieldWhen user is going to filled it, on runtime i searched for #tag and make text bold

57
Set textbox in given coordinates over image with autofit text for width and height of the textbox

Set textbox in given coordinates over image with autofit text for width and height of the textbox

Set textbox in given coordinates over image with autofit text for width and height of the textbox, I tried the next code and it works fine but without autofit or to set static dimensions "width ,height" to the textbox

55
Doctrine query (not QueryBuilder) with multiple andWhere and multiple OR

Doctrine query (not QueryBuilder) with multiple andWhere and multiple OR

I have the following code to select items with either of the group and many other parameters:

60