標題重寫為:"PHP數組中的未定義變數"
<p>我試圖使用php取得國家代碼。我使用以下程式碼,但是得到了這個警告:</p>
<blockquote>
<p>未定義的索引:country_code</p>
</blockquote>
<pre class="brush:php;toolbar:false;">function getIP($ip) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://ipwho.is/".$ip,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "utf8",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
//print_r($response);
curl_close($curl);
if ($err) {
return "cURL Error #:" . $err;
} else {
return $response;
}
}
function isIndo($ip) {
$ip = getIP($ip);
$info = json_decode($ip, true);
//print_r($info);
if( $info['country_code'] == "ID" ) {
return true;
} else {
return false;
}
}</pre>
<p>哪裡出錯了? </p>