首页 文章

是什么导致未定义的偏移:0 php [复制]

提问于
浏览
1

这个问题在这里已有答案:

我正在尝试从地址获取坐标,但有一个称为未定义偏移的错误 . 实际上数组不是null,当我尝试逐个获得坐标时,我可以毫无问题地 grab 它 . 但是,当我尝试立即出现错误时,会弹出错误 .

$addr = [];
    foreach($newHtml as $link){

        $htm = pageContent($link);
        $paths = new \DOMXPath($htm);
        $routes = $paths->query("//body/div[@class='p-main']//table/tr[4]/td");
        foreach ($routes as $route) {
            $addr = trim($route->nodeValue);
        }

        $address = urlencode($addr);
        $response = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address={$address}&key=MY_API_KEY");
        $result = json_decode($response);

        $lat[] = trim($result->results[0]->geometry->location->lat);
        $lng[] = trim($result->results[0]->geometry->location->lng);
    }

    var_dump($lat, $lng);

响应给出了这个;

[“results”] => array(1){[0] => object(stdClass)#696(6){[“address_components”] => array(8){[0] => object(stdClass)# 684(3){[“long_name”] => string(9)“D21”[“short_name”] => string(9)“D21”[“types”] => array(1){[0] => string(7)“premise”}} [1] => object(stdClass)#709(3){[“long_name”] => string(1)“1”[“short_name”] => string(1)“ 1“[”types“] => array(3){[0] => string(9)”political“[1] => string(11)”sublocality“[2] => string(19)”sublocality_level_4“ }} [2] => object(stdClass)#714(3){[“long_name”] => string(7)“5 Chome”[“short_name”] => string(7)“5 Chome”[“types “] => array(3){[0] => string(9)”political“[1] => string(11)”sublocality“[2] => string(19)”sublocality_level_3“}} [3] => object(stdClass)#698(3){[“long_name”] => string(8)“Furuedai”[“short_name”] => string(8)“Furuedai”[“types”] => array(3 ){[0] => string(9)“political”[1] => string(11)“sublocality”[2] => string(19)“sublocality_level_2”}} [4] => object(stdClass)# 680(3){[“long_name”] => string(5) “Suita”[“short_name”] => string(5)“Suita”[“types”] => array(2){[0] => string(8)“locality”[1] => string(9) “political”}} [5] => object(stdClass)#701(3){[“long_name”] => string(16)“Osaka Prefecture”[“short_name”] => string(16)“Osaka Prefecture” [“types”] => array(2){[0] => string(27)“administrative_area_level_1”[1] => string(9)“political”}} [6] => object(stdClass)#695( 3){[“long_name”] => string(5)“Japan”[“short_name”] => string(2)“JP”[“types”] => array(2){[0] => string( 7)“country”[1] => string(9)“political”}} [7] => object(stdClass)#699(3){[“long_name”] => string(8)“565-0874” [“short_name”] => string(8)“565-0874”[“types”] => array(1){[0] => string(11)“postal_code”}}} [“formatted_address”] => string(69)“5 Chome-1-D21 Furuedai,Suita,Osaka Prefecture 565-0874,Japan”[“geometry”] => object(stdClass)#690(3){[“location”] => object(stdClass )#694(2){[“lat”] => float(34.8097093)[“lng”] => float(135.5119112)} [“location_type”] => string(7)“ROOFTOP”[“viewpo” rt“] => object(stdClass)#689(2){[”northeast“] => object(stdClass)#688(2){[”lat“] => float(34.811058280291)[”lng“] => float(135.51326018029)} [“southwest”] => object(stdClass)#692(2){[“lat”] => float(34.808360319709)[“lng”] => float(135.51056221971)}}} [“place_id “] => string(27)”ChIJUY4O7kP7AGARu_UB4lJPFWU“[”plus_code“] => object(stdClass)#687(2){[”compound_code“] => string(30)”RG56 VQ Suita,大阪府日本“[” global_code“] => string(11)”8Q6QRG56 VQ“} [”types“] => array(2){[0] => string(13)”establishment“[1] => string(17)”point_of_interest“ }“”状态“] =>字符串(2)”确定“

2 回答

  • 0

    试着改变

    $locations = json_decode($response, true);
    
        if(is_array($locations)) {
       $lat =   $locations['results'][0]['geometry']['location']['lat'];
       $lng =   $locations['results'][0]['geometry']['location']['lng'];
    
      }
    

    json_decode - >将返回对象数组

    json_decode,第二个参数为true,返回数组

  • 0

    此行导致错误:

    $result->results[0]
    

    你需要检查 $result->results 是否有0键 .

    UPDATE:

    要解决您的问题,您需要捕获您不期望的响应:

    $response = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address={$address}&key=MY_API_KEY");
    $result = json_decode($response);
    
    try {
        $lat[] = trim($result->results[0]->geometry->location->lat);
        $lng[] = trim($result->results[0]->geometry->location->lng);
    } catch (\Exeption $e) {
        var_dump($result);
    }
    

    发生异常时,您将看到导致问题的响应,然后您需要根据收到的内容添加响应格式检查 .

    例如,如果您收到这样的内容:

    {"results":[], "status": "ZERO_RESULTS"}
    

    您的代码应如下所示:

    $response = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address={$address}&key=MY_API_KEY");
    
    $result = json_decode($response);
    
    if(!empty($result->results)){
        $lat[] = trim($result->results[0]->geometry->location->lat);
        $lng[] = trim($result->results[0]->geometry->location->lng);
    }
    

    另见https://developers.google.com/maps/documentation/geocoding/intro

相关问题