相同的端点Bing Visual Search API但与 Python 脚本和 PHP 脚本不同的结果

问题描述

我正在使用的图像

testing image

Python 脚本:

HEADERS = {'Ocp-Apim-Subscription-Key': SUBSCRIPTION_KEY}
file = {'image' : ('myfile',open(imagePath,'rb'))}
def print_json(obj):
        """Print the object as json"""
        print(json.dumps(obj,sort_keys=True,indent=2,separators=(',',': ')))
    
try:
        response = requests.post(BASE_URI,headers=HEADERS,files=file)
        response.raise_for_status()
        print_json(response.json())
        
except Exception as ex:
        raise ex

Python 脚本结果:https://pastebin.com/3N3BTgPU

PHP 脚本:

$ch = curl_init();
$img = curl_file_create($imagepath);
$post = ["image" => $img];
    
curl_setopt($ch,CURLOPT_URL,self::endpoint);
curl_setopt($ch,CURLOPT_HTTPHEADER,array(
        'Content-Type: multipart/form-data','Ocp-Apim-Subscription-Key: ' . self::API_KEY,));
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
$contents = curl_exec($ch);
echo "$contents";

PHP 脚本结果:https://pastebin.com/c2tKwgBV

必应视觉搜索结果here

我可以确认两个脚本使用相同的端点、API 密钥和相同的图像。 我在 PHP 中做错了什么?

解决方法

我不懂 PHP,但您确定在这两种情况下都将相同的有效负载传递给 post 端点吗?尝试打印两个有效负载并进行比较。

蟒蛇:

# a dict with key and value(a tuple with 'myfile' and image)
file = {'image' : ('myfile',open(imagePath,'rb'))}

PHP:

# a list (?)
$post = ["image" => $img];

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...