[英]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作為回報:
{
"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代碼
我使用以下鏈接指導我:
https://incarnate.github.io/curl-to-php/
https://gist.github.com/germanattanasio/ca22c0d47755d6f023f1
IBM Watson api的視覺識別使用curl在集合中添加圖像問題
請記住,我沒有安裝任何庫或使用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的關注。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.