phpwordpresscurlibm-watsonvisual-recognition

How to convert IBM Watson cURL commands to PHP


I need to convert this cURL command in PHP to use it on my site in 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"

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

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

Remember that I am not installing any library or using Composer.


Solution

  • After a few attempts, I was able to make the code work using my Custom Classifier Model of IBM Watson Visual Recognition.

    Change {your_api_key} to your credential and {your_custom_model_ID} to your custom model ID.

    The Code:

    <?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;
    

    Especial thanks to Samvel Aleqsanyan for your attention.