Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在面向对象编程中检查键是否存在_Php_Json_Google Maps_Object_Geocoding - Fatal编程技术网

Php 在面向对象编程中检查键是否存在

Php 在面向对象编程中检查键是否存在,php,json,google-maps,object,geocoding,Php,Json,Google Maps,Object,Geocoding,来自Google API查询的数据有时会丢失,例如当输入无效地址时,就会出现未知密钥的错误。为了避免这个丑陋的错误,我将调用包装到一个条件中,但似乎无法让它以这种方式工作,因为我的面向对象编程技能根本不存在。下面是我所做的和一些被评论出来的尝试,那么我做错了什么?我真正关心的是$dataset->results[0]是否有效,因为之后的任何内容都是有效的 $url = "https://maps.googleapis.com/maps/api/geocode/json?address=$Addr

来自Google API查询的数据有时会丢失,例如当输入无效地址时,就会出现未知密钥的错误。为了避免这个丑陋的错误,我将调用包装到一个条件中,但似乎无法让它以这种方式工作,因为我的面向对象编程技能根本不存在。下面是我所做的和一些被评论出来的尝试,那么我做错了什么?我真正关心的是$dataset->results[0]是否有效,因为之后的任何内容都是有效的

$url = "https://maps.googleapis.com/maps/api/geocode/json?address=$Address&key=$googlekey";

// Retrieve the URL contents
$c = curl_init();
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_FRESH_CONNECT, true);
curl_setopt($c, CURLOPT_URL, $url);
$jsonResponse = curl_exec($c);
curl_close($c);    

$dataset = json_decode($jsonResponse);

if (isset($dataset->results[0])) :
//if (isset($dataset->results[0]->geometry->location)) :
//if (!empty($dataset)) :
//if (!empty($dataset) && json_last_error() === 0) :
    $insertedVal = 1;
    $latitude = $dataset->results[0]->geometry->location->lat;
    $longitude = $dataset->results[0]->geometry->location->lng;
    return "$latitude,$longitude";
endif;

您应该知道,地理编码API web服务也会在响应中返回一个状态。状态指示响应中是否存在有效项或出现了错误,并且您没有任何结果

查看文档,您将看到以下可能的状态

好啊 零结果 超过每日限额 超限 请求被拒绝 无效的请求 未知错误 因此,在尝试访问$dataset->results[0]之前,首先检查$dataset->status的值。如果没有问题,您可以安全地获得结果,否则请正确处理错误代码

代码片段可能是

 $dataset = json_decode($jsonResponse);

 if ($dataset->status == "OK") {
     if (isset($dataset->results[0])) {
         $latitude = $dataset->results[0]->geometry->location->lat;
         $longitude = $dataset->results[0]->geometry->location->lng;
     }
 } elseif ($dataset->status == "ZERO_RESULTS") {
     //TODO: process zero results response 
 } elseif ($dataset->status == "OVER_DAILY_LIMIT" {
     //TODO: process over daily quota 
 } elseif ($dataset->status == "OVER_QUERY_LIMIT" {
     //TODO: process over QPS quota  
 } elseif ($dataset->status == "REQUEST_DENIED" {
     //TODO: process request denied  
 } elseif ($dataset->status == "INVALID_REQUEST" {
     //TODO: process invalid request response  
 } elseif ($dataset->status == "UNKNOWN_ERROR" {
     //TODO: process unknown error response 
 }

我希望这有帮助

谢谢,这正是我需要知道的,虽然您没有回答确切的问题,但您的示例显示,在我正在执行的过程中检查isset$dataset->results[0],这证实了我的操作是正确的。添加$dataset->status==OK是一个额外的奖励,具体的错误部分也是如此。虽然我已经编写了一个函数来输出错误,但我不确定它的有效性,所以您也帮助确认了这一点。