Error 403 when searching for food using Zomato API

358 views Asked by At

Let's say I want to list all seafood restaurants in Jakarta using this Zomato API: https://developers.zomato.com/documentation#!/restaurant/search

Here's how you call it directly with curl:

curl -X GET --header "Accept: application/json" --header "user-key: xxxxxxxxxxxxxxxxxxxx" "https://developers.zomato.com/api/v2.1/search?entity_id=74&q=seafood"

Which returns big JSON like this: [![enter image description here][1]][1]

Now I want to do it via PHP:

<?php 

    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

    $ZOMATO_API_KEY = "xxxxxxxxxxxxxxxx";

    $BASE_URL = "https://developers.zomato.com/api/v2.1/search";

    // Find all 'seafood' restaurants in Jakarta
    $data = array ('entity_id' => '47', 'q' => 'seafood');
    $PARAMS = '';
    foreach ($data as $key=>$value){
        $PARAMS .= $key.'='.$value.'&';
    }
         
    $PARAMS = trim($PARAMS, '&');

    $HEADERS = [
        'Accept' => 'application/json',
        'user-key' => $ZOMATO_API_KEY
    ];

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $BASE_URL.'?'.$PARAMS); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $HEADERS);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $curl_output = curl_exec($ch); 
    curl_close($ch);      

    echo $curl_output;
?>

Got this error instead:

{ code: 403, status: "Forbidden", message: "Invalid API Key" }

What's wrong here? I'm sure the API key is correct.

0

There are 0 answers