相同的端点(Bing 视觉搜索 API)但来自 Python 脚本和 PHP 脚本的结果不同
Same endpoint (Bing Visual Search API) but different result from Python script and PHP script
我正在使用的图像
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
来自 Bing 视觉搜索的结果:here
我可以确认这两个脚本使用相同的端点、API 密钥和相同的图像。
我在 PHP 中做错了什么?
我不知道 PHP 但您确定在这两种情况下都将相同的负载传递到 post 端点吗?尝试打印两个有效载荷并进行比较。
Python:
# a dict with key and value(a tuple with 'myfile' and image)
file = {'image' : ('myfile', open(imagePath, 'rb'))}
PHP:
# a list (?)
$post = ["image" => $img];
我正在使用的图像
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
来自 Bing 视觉搜索的结果:here
我可以确认这两个脚本使用相同的端点、API 密钥和相同的图像。 我在 PHP 中做错了什么?
我不知道 PHP 但您确定在这两种情况下都将相同的负载传递到 post 端点吗?尝试打印两个有效载荷并进行比较。
Python:
# a dict with key and value(a tuple with 'myfile' and image)
file = {'image' : ('myfile', open(imagePath, 'rb'))}
PHP:
# a list (?)
$post = ["image" => $img];