首页 文章

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

提问于
浏览
3

我需要在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
}

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;

这是错误:

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的自定义模型 . 我留下了评论我如何使用它,因为我使用的语法我不能使用我需要的图像 .

Using 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 .

1 回答

  • 1

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

    {your_api_key} 更改为您的凭据,将 {your_custom_model_ID} 更改为您的自定义型号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;
    

    特别感谢 Samvel Aleqsanyan 引起您的注意 .

相关问题