I need to convert this cURL command in PHP to use it on my site in WordPress.
curl -X POST -F "[email protected]" -F "[email protected]" "https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key={api-key}&version=2016-05-20"
Parameters object that I'm using:
{
"classifier_ids": [
"My_Model_ID",
"default"
],
"owners": ["me"],
"threshold": 0.6
}
This is my attempt:
<?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;
This is the error:
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
This is the JSON I get as a return:
{
"error":
{
"code": 400,
"error_id": "input_error",
"description": "No images were specified."
},
"images_processed": 0
}
I want use my custom model of IBM Watson Visual Recognition. I left commenting exactly how I use it, because with the syntax I'm using I can not use the image I need.
Using WordPress
Version: 9.4.4
Plugin: XYZ PHP Code
I am using the following links to guide me:
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
Remember that I am not installing any library or using Composer.
/wp-content/uploads/2018/03/raiox_img02.jpg
file exists, or you calling it with right path? – Samvel Aleqsanyancurl_file_create()
function. – Samvel Aleqsanyan$filename = file_get_contents('@/wp-content/uploads/2018/03/raiox_img02.jpg');
code located? – Samvel Aleqsanyan