如何将 IBM Watson cURL 命令转换为 PHP

How to convert IBM Watson cURL commands to PHP

我需要在 PHP 中转换此 cURL 命令,以便在我的 WordPress 网站上使用它。

curl -X POST -F "images_file=@fruitbowl.jpg" -F "parameters=@fruit.json" "https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key={api-key}&version=2016-05-20"

我正在使用的参数对象:

{
    "classifier_ids": [
        "My_Model_ID",
        "default"
    ],
    "owners": ["me"],
    "threshold": 0.6
}

这是我的尝试:

<?php
//Here is the JSON Parameters Object
$arr = array('classifier_ids' => array('My_Model_ID', 'default'), 'owners' => array('me'), 'threshold' => 0.6);

//Here is the endpoint URL
$url = 'https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key=MY_KEY&version=2016-05-20';

// IMPORTANT - Image that is uploaded on my site 
$filename = file_get_contents('@/wp-content/uploads/2018/03/raiox_img02.jpg');
$cfile = curl_file_create($filename,'image/jpeg');

//cURL
$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url); //URL
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    curl_setopt($ch, CURLOPT_POST, 1); //POST
    curl_setopt($ch, CURLOPT_FILE, $cFile); //Try pass image
    curl_setopt($ch, CURLOPT_POSTFIELDS,  json_encode($arr)); //Try pass JSON

//Result
$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Erro: ' . curl_error($ch); //Error
}
curl_close ($ch); //Finish

echo $result;

这是错误:

Warning: file_get_contents(@/wp-content/uploads/2018/03/raiox_img02.jpg): failed to open stream: No such file or directory in /srv/bindings/.. ../code/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(65) : eval()'d code on line 9 Warning: curl_setopt(): supplied argument is not a valid File-Handle resource in /srv/bindings/.. ../code/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(65) : eval()'d code on line 18

这是 JSON 我得到的 return:

{
 "error":
     {
       "code": 400,
       "error_id": "input_error",
       "description": "No images were specified."
     },
 "images_processed": 0
}

我想使用我的 IBM Watson Visual Recognition 自定义模型。 我留下了评论我是如何使用它的,因为我使用的语法无法使用我需要的图像。

使用 WordPress

版本:9.4.4

插件:XYZ PHP Code

我正在使用以下链接来指导我:

Convert command line cURL to PHP cURL

https://incarnate.github.io/curl-to-php/

https://gist.github.com/germanattanasio/ca22c0d47755d6f023f1

IBM watson api of visual recognition add image issue in collectio using curl

https://console.bluemix.net/docs/services/visual-recognition/tutorial-custom-classifier.html#classify

请记住,我没有安装任何库或使用 Composer。

经过几次尝试,我能够使用我的 IBM Watson Visual Recognition 自定义分类器模型使代码正常工作。

{your_api_key} 更改为您的凭据,将 {your_custom_model_ID} 更改为您的自定义模型 ID。

代码:

<?php

// Code Date: March, 2018
// === Remember to see all documentation in IBM Watson ===

// Set the endpoint URL
$url = 'https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key={your_api_key}&version=2016-05-20';

// Set the image url
$image_url = '&url=http://completeURL.com/img.jpg';

// Set my custom classifier
$classifier = '&classifier_ids={your_custom_model_ID}';

// Set the Threshold, by default is 0.5 - To show all scores use Zero
$threshold = '&threshold=0';

//cURL
$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url); //Endpoint URL
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1); //POST
    curl_setopt($ch, CURLOPT_POSTFIELDS, $image_url . $classifier . $threshold); //Parameters

// Execute the cURL command
$result = curl_exec($ch);

// Erro
if (curl_errno($ch)) {
    echo 'Error: ' . curl_error($ch);
}

// Close the command
curl_close ($ch);

// Show the JSON result
echo $result;

特别感谢Samvel Aleqsanyan的关注