Max MInd Omni Web服务数组为变量


Max MInd Omni Web Service Array into Variables

嗨,我正在使用Max Mind Omni web服务,并使用他们的PHP示例代码。

我是新的使用curl,我正试图找出最好的方法来分离数组到单独的变量,我可以插入到数据库表。

下面的

是我使用的代码示例。当我运行这段代码时,我似乎无法获得全能键的单个值。

如有任何建议,不胜感激。

 if (!isset($params['i'])) $params['i'] = '82.150.248.29';
 $query = 'https://geoip.maxmind.com/e?' . http_build_query($params);
 $omni_keys = 
   array(
    'country_code',
    'country_name',
    'region_code',
    'region_name',
    'city_name',
    'latitude',
    'longitude',
    'metro_code',
    'area_code',
    'time_zone',
    'continent_code',
    'postal_code',
    'isp_name',
    'organization_name',
    'domain',
    'as_number',
    'netspeed',
    'user_type',
    'accuracy_radius',
    'country_confidence',
    'city_confidence',
    'region_confidence',
    'postal_confidence',
    'error'
    );
   $curl = curl_init();
   curl_setopt_array( $curl, 
               array(
                     CURLOPT_URL => $query,
                     CURLOPT_USERAGENT => 'MaxMind PHP Sample',
                     CURLOPT_RETURNTRANSFER => true
                     )
               );
            $resp = curl_exec($curl);
          if (curl_errno($curl)) {
            throw new Exception('GeoIP Request Failed');
              }
                  $omni_values = str_getcsv($resp);
                $omni = array_combine( $omni_keys, $omni_values);
                //print_r($omni_values);
                 $country_code=$omni_value[0];
                 $country_name=$omni_value[1];
                 echo "$country_code";
                 echo "$country_name";

您的代码中有一个错别字....

$country_code= $omni_values[0]; //instead of $omni_value[0];

其中的这一行使它成为一个关联数组。

$omni = array_combine( $omni_keys, $omni_values);

所以你会做一些改变,像这样:

$country_code= $omni['country_code']; //instead of $omni_value[0];
$country_name= $omni['country_name']; //instead of $omni_value[1];   

希望能有所帮助