视觉识别的IBM Watson API使用curl在Collectio中添加图像问题 [英] IBM watson api of visual recognition add image issue in collectio using curl

查看:84
本文介绍了视觉识别的IBM Watson API使用curl在Collectio中添加图像问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我向集合中添加图像时,我正在使用IBM watson视觉识别api,但是
总是收到以下错误:

I am using IBM watson visual recognition api when I add an image to collection but I receive following error always:

string(59) "{ "error": "Missing multipart/form-data", "code": 400 }" bool(true)

这是我的代码:

 <?php
 if ( isset($_FILES['uploadedfile']) && $_POST!="" ) {
 $targetPath = 'uploads/'.basename($_FILES['uploadedfile']['name']);
 $url = 'https://gateway-a.watsonplatform.net/visual-recognition/api/v3/collections/searchItems_c5c677/images?api_key=655e4118jgfd8e967ce58ee0b67behjfh3ebfad22e38a34e&version=2016-05-20';
 $fileData = $_FILES['uploadedfile']['name'];
 $post_data = array(
        'image_file' => $fileData
 );
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
 $headers = array();
 $headers[] = "Content-Type: multipart/form-data";
 $headers[] = "Accept: application/json";
 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 $result = curl_exec($ch);
 if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);die;
 }
   var_dump($result, true);die;
 }
?>
<form enctype="multipart/form-data" method='post' action="index.php">
  <input name="uploadedfile" type="file" value="choose">
  <input type="submit" value="Upload">
</form>


推荐答案

我已经检查了您的代码并进行了一些更改,现在

I have review your code and make some changes and now its working.

if (isset($_FILES['uploadedfile']) && $_POST != "") {
    $targetPath = 'uploads/' . basename($_FILES['uploadedfile']['name']);
    $url = 'https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key={your_key}&version=2016-05-20';
    $post_data = array(
        'file' =>
        '@' . $_FILES['uploadedfile']['tmp_name']
        . ';filename=' . $_FILES['uploadedfile']['name']
        . ';type=' . $_FILES['uploadedfile']['type']
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    $headers = array();
    $headers[] = "Content-Type: multipart/form-data";
    $headers[] = "Accept: application/json";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
        die;
    }
    var_dump($result, true);
    die;
}

这篇关于视觉识别的IBM Watson API使用curl在Collectio中添加图像问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆