Search code examples
pythonphpphp-curlbingbing-api

Same endpoint (Bing Visual Search API) but different result from Python script and PHP script


The image I am using testing image

Python script:

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 script results: https://pastebin.com/3N3BTgPU

PHP Script:

$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 Script results: https://pastebin.com/c2tKwgBV

Results from Bing Visual Search: here

I can confirm that both scripts use the same endpoint, API Key and same image. What am I doing wrong in PHP?


Solution

  • I don't know PHP but are you sure you are passing the same payload to the post endpoint in both case? Try printing the both payloads and compare.

    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];