如何将IBM Watson cURL命令转换为PHP
我需要在PHP中转换这个cURL命令,以便在我的WordPress站点上使用它如何将IBM Watson cURL命令转换为PHP,php,wordpress,curl,ibm-watson,visual-recognition,Php,Wordpress,Curl,Ibm Watson,Visual Recognition,我需要在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" 我正在使用的参数对象: { "cl
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;
<?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;
这是我返回的JSON:
{
"error":
{
"code": 400,
"error_id": "input_error",
"description": "No images were specified."
},
"images_processed": 0
}
我想使用我的IBM Watson视觉识别自定义模型。
我留下了关于我如何使用它的评论,因为我使用的语法无法使用我需要的图像
使用WordPress
版本:9.4.4
插件:
我使用以下链接来指导我:
请记住,我没有安装任何库或使用Composer。经过几次尝试,我能够使用IBM Watson Visual Recognition的自定义分类器模型使代码正常工作 将
{your_api_key}
更改为您的凭证,将{your_custom_model_ID}
更改为您的自定义model ID
代码:
<?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;
<?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;
你确定/wp content/uploads/2018/03/raiox_img02.jpg文件存在,还是你用正确的路径调用它?另外,下一个错误用法是curl_file_create()
函数。你的$filename=file_get_contents('@/wp content/uploads/2018/03/raiox_img02.jpg')文件在哪里代码>代码定位?这就是问题所在,我如何使用此图像?首先,回答我的问题,您的文件位于哪里(包含您提供的文档的文件)?乐意帮助。很高兴你为即将到来的人找到了解决方案,并且想让detect_faces方法发挥作用,我在这里留下了这样做的要点(受此答案启发)