无法访问Zomato api JSON对象

Cannot access Zomato APIs JSON object

本文关键字:JSON 对象 api Zomato 访问      更新时间:2023-09-26

我正在Udacity的web开发纳米学位课程中的一个项目上工作。我决定使用餐厅信息并生成我的API密钥。然而,当我在AJAX对象中使用它来获取数据时,开发工具显示了一个"无效API"错误。不过,API是正确的。请帮助。

下面是我的代码:

$(document).ready(function(){
$("button").click(function(){
    $.get("https://developers.zomato.com/api/v2.1/restaurant?res_id=MY_RES_ID", { "Accept": "application/json", "user-key": 'MY_API_KEY' }, function(data, status){
        alert("Data: " + data + "'nStatus: " + status);
    });
});

});

下面是Zomato的文档:

旋度:

curl -X GET --header "Accept: application/json" --header "user-key: MY_API" "https://developers.zomato.com/api/v2.1/restaurant?res_id=MY_RES_ID"
URL:

https://developers.zomato.com/api/v2.1/restaurant?res_id=MY_RES_ID

请注意,我使用的餐厅ID和API是正确的。我不太懂PHP,所以不知道curl是什么意思

请尝试下面的代码:

$(document).ready(function(){
  $("button").click(function(){
    $.get("https://developers.zomato.com/api/v2.1/restaurant?res_id=MY_RES_ID&apikey=MY_API_KEY", function(data, status){
        alert("Data: " + data + "'nStatus: " + status);
    });
  });
});

但是,由于安全问题,不鼓励使用上述方法。通过在服务器端编写处理程序,您可以在PHP中使用服务器对服务器调用访问Zomato API,该处理程序可以作为ajax调用从按钮单击事件调用。下面是示例PHP处理程序:

$curl_channel = curl_init('https://developers.zomato.com/api/v2.1/restaurant?res_id=MY_RES_ID&apikey=MY_API_KEY');
curl_setopt($curl_channel, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl_channel);
curl_close($curl_channel);
//Convert output to object
$curl_output = json_decode($curl_response);