vendredi 27 janvier 2017

PHP curl output is encoded? My IF is wrong?

I'm newbie with curl and php. Acctually i'm work on this sample code:

I'm using this API to receive the response in Newline (\n).

$client_ip = @$_SERVER['HTTP_CLIENT_IP'];
$forward_ip = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote_ip = $_SERVER['REMOTE_ADDR'];

if (filter_var($client_ip, FILTER_VALIDATE_IP)){
 $visitor_ip = $client_ip;        
} elseif (filter_var($forward_ip, FILTER_VALIDATE_IP)){
 $visitor_ip = $forward_ip;
} else {
 $visitor_ip = $remote_ip;
}

$url = "http://ip-api.com/line/".$visitor_ip."?fields=city";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$visitor_city = curl_exec($curl);
curl_close($curl);

echo $visitor_city;

Result:

New York

The result is correct. I'm in New York, the code show my city. But when i add IF, something as wrong:

if ($visitor_city == "New York") {
    echo " TRUE!";
}

My result with this IF:

New York

But i want IF result

New York TRUE!

I'm miss something? Server configuration?

Aucun commentaire:

Enregistrer un commentaire