【问题标题】:Same endpoint (Bing Visual Search API) but different result from Python script and PHP script相同的端点(Bing Visual Search API),但 Python 脚本和 PHP 脚本的结果不同
【发布时间】:2021-01-05 05:23:31
【问题描述】:

我正在使用的图像

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 中做错了什么?

【问题讨论】:

    标签: python php php-curl bing bing-api


    【解决方案1】:

    我不了解 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];
    

    【讨论】:

    • 谢谢!转至$img = curl_file_create($imagepath, "image/jpg", "myfile"); 解决了问题。
    猜你喜欢
    • 2021-09-23
    • 2023-03-24
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多