duanao6704 2016-03-11 16:21
浏览 57

在codeigniter中打印geolocation api中的每个数据

I want to print all info from geolocation API in codeigniter then I want to insert into database. I try to print using foreach but find an error,

    $ip='202.6x.xx.xx';
    $this->load->library('Geolocation');
    $this->load->config('geolocation', true);
    $config = $this->config->config['geolocation'];
    $this->geolocation->initialize($config);
    $this->geolocation->set_ip_address($ip);
    $country = $this->geolocation->get_country();
    //var_dump($country);

    $city = $this->geolocation->get_city();
    if($city === FALSE){
        //var_dump($this->geolocation->get_error());
    }
    else{
        //var_dump($city);
        foreach ($city as $c) {
            echo $c->regionName; <-- EROR
        }
        echo "Country code : ".$city['countryName']."
"; <-- EROR
        echo "Cityname : ".$city['cityName']."
";  <-- EROR
    }

Here is value of var_dump($city)

string(286) "{ "statusCode" : "OK", "statusMessage" : "", "ipAddress" : "202.67.xx.xx", "countryCode" : "ID", "countryName" : "Indonesia", "regionName" : "regionblablabla", "cityName" : "cityblablabla", "zipCode" : "60132", "latitude" : "-7.258", "longitude" : "112.758", "timeZone" : "+07:00" }" 

Eror if using foreach :

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: controllers/Front.php

Line Number: 105

Can you explain how to write right code to print each data. Thanks

  • 写回答

2条回答 默认 最新

  • dongtan1845 2016-03-11 16:33
    关注

    You are getting response in array, replace this:

    foreach ($city as $c) { 
        echo $c->regionName;
    }
    

    Into:

    foreach ($city as $c) { 
        echo "Region :".$c['regionName'];
        echo "Country code : ".$c['countryName']."
    "; 
        echo "Cityname : ".$c['cityName']."
    ";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题