Hi im trying to write a currency converting php script, utilizing the Google Calculator API via cURL. (file_get_contents doesnt work in my hosting server).
the URL im trying to get data from is
http://www.google.com/ig/calculator?hl=en&q=1usd=?idr
the result of loading from a browser:
{lhs: "1 U.S. dollar",rhs: "8 928.57143 Indonesian rupiahs",error: "",icc: true}
but my script returns :
{lhs: "1 U.S. dollar",rhs: "8Â 928.57143 Indonesian rupiahs",error: "",icc: true}
as you can see on the rhs part there are white space differences, and a funny A character, hampering my rounding operations.
my script before exploding & rounding etc: ($url has been set to the above value)
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $this->url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);
$rawresult = curl_exec($ch);
curl_close($ch);
how do i get the same formatting?
What you are receiving there is C2 A0
in UTF-8 encoding. That's the non-breaking space (NBSP). You can either use utf8_decode()
to cope or just set your pages to UTF-8 charset.
When decoded, the character becomes chr(0xA0)
in Latin-1. So you might want to use a preg_split('/\s/u'
rather than just exploding on a space.
The other alternative is to add the Accept-Charset: ASCII
header when requesting the resource via cURL. (Then Google returns an ordinary space here. Just a solution for Google though, not many websites honour such HTTP features.)