Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.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 从json结果中提取一位数据_Php_Json_Curl - Fatal编程技术网

Php 从json结果中提取一位数据

Php 从json结果中提取一位数据,php,json,curl,Php,Json,Curl,我试图在加载CURL时从JSON数组中提取一位数据。如果我回显结果的话,我可以告诉你这一切都很好,但是当我要去邮递镇的时候,我总是遇到一片空白????我的PHP代码: $url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=52.406822,-1.519693&sensor=true"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url

我试图在加载CURL时从JSON数组中提取一位数据。如果我回显结果的话,我可以告诉你这一切都很好,但是当我要去邮递镇的时候,我总是遇到一片空白????我的PHP代码:

  $url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=52.406822,-1.519693&sensor=true";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $response = curl_exec($ch);
    curl_close($ch);
    $response_a = json_decode($response, true);
    $town = $response_a['results'][0]['address_components'][0]['types']['postal_town'];

    echo $town;
JSON结果的一个示例是:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Ringway Rudge",
               "short_name" : "A4053",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Coventry",
               "short_name" : "Coventry",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Coventry",
               "short_name" : "Coventry",
               "types" : [ "postal_town" ]
            },
            {
               "long_name" : "West Midlands",
               "short_name" : "West Midlands",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "England",
               "short_name" : "England",
               "types" : [ "administrative_area_level_1", "political" ]
            },

这就成功了,照德文说的做了,谢谢

   $result = @file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?latlng=53.406822,-1.519693&sensor=true" );
    if ($result === FALSE) {
        //manage exception from file_get_contents call
    } else {
        $geocodedinfo = json_decode($result);
        if ($geocodedinfo->status == "OK") {
            $town = "";
            foreach ($geocodedinfo->results[0]->address_components as $addrcomps) {
                if ( $addrcomps->types[0] == 'postal_town')
                    $town = $addrcomps->long_name;           
            }
        }
    }


    echo $town;

我在你的JSON中没有看到postal_town键…“types”:[“postal_town”]这是第三个,但这不是键,这是一个值。哦?我只想将的值设置为$city。对不起,也许我用错了,我不认为这是你怎么用的,这是你试图访问它的方式。你试图用一把钥匙进入postal_town,不知怎的,你希望它能给你带来回报?但postal_town不是键,而是类型数组中的值。