开发者

CURL issue in PHP while getting location list

I am retrieving the nearest locations available from a given address (Longitude/Latitude) from geolocation website. It works fine, but for some places it gives junk characters in the name. Moreover, in browser I am getting different characters compared to my PHP CURL functionality. Here is the URL

http://www.geoplugin.net/extras/nearby.gp?lat=17.7374669&long=83.3214858&limit=5&radius=50&format=php

One of the location is "Sitammapeta" in original location name, but in browser I am getting "Sītammapeta" where as in CURL function I am getting "SÄ«tammapeta".

Please tell me why this difference. I wrote a function to convert browser output to original which works fine.

function convert ($old)
    {
        $n="";
        for ($i=0; $i<strlen($old); $i++)
        {
            $n .= chr(ord(substr($old,$i,1)));
        }
        return $n;
    }

But I dont understand how I convert the CURL output to original name.

EDIT CURL code

$ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $host);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_USERAGENT, 'geoPlugin PHP Class v1.0');
            $resp开发者_如何学运维onse = curl_exec($ch);
            curl_close ($ch);


The problem is that you're displaying the data in your web-browser, that the data your sending is UTF-8 encoded and that your browser has no idea about that. If you add <meta charset="utf-8" /> or <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> to the head of your HTML document, that should solve your problem.

Without the charset defined as UTF-8:

CURL issue in PHP while getting location list

With <meta charset="utf-8" /> or <meta http-equiv="Content-Type" content="text/html; charset=utf-8">:

CURL issue in PHP while getting location list

Alternatively, you could have your PHP script send a content-type header using header('Content-Type:text/html; charset=utf-8'); (or replace text/html with text/plain if you want to use plain text).

Reference:

According to RFC2616 Section 3.7.1:

When no explicit charset parameter is provided by the sender, media subtypes of the "text" type are defined to have a default charset value of "ISO-8859-1" when received via HTTP.

That means that if you don't specify a charset, ISO-8859-1 will (should) be used, and as such, some characters won't be displayed properly if the data is UTF-8 encoded.


This looks like it's just an encoding issue. Whereas your browser can usually automatically detect and employ the best fit encoding, the internals of cURL cannot. I'd look into the curl_setopt() function to see what encoding options you can pass.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜