0

假设我想使用此 Zomato API 列出雅加达的所有海鲜餐厅: https ://developers.zomato.com/documentation#!/restaurant/search

下面是直接用 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"

它返回像这样的大 JSON:[![在此处输入图像描述][1]][1]

现在我想通过 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;
?>

得到了这个错误:

{ 代码:403,状态:“禁止”,消息:“无效的 API 密钥”}

这里有什么问题?我确定 API 密钥是正确的。

4

0 回答 0