Php 使用Zomato API搜索食物时出现错误403
假设我想列出雅加达所有使用此Zomato API的海鲜餐厅: 下面是使用curl直接调用它的方式:Php 使用Zomato API搜索食物时出现错误403,php,php-curl,zomato-api,Php,Php Curl,Zomato Api,假设我想列出雅加达所有使用此Zomato API的海鲜餐厅: 下面是使用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" 返回的
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密钥是正确的。您的
实体id
被颠倒了。在CURL查询中是74,在PHP中是47,这可以解释为什么您没有查看它的权限?啊,是的,这是一个输入错误,但没关系。这两个实体id
都可以在curl上正常工作。