doukui7574 2011-11-13 19:03
浏览 54
已采纳

cURL操作以奇数格式返回,如何获得正常结果?

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?

  • 写回答

1条回答 默认 最新

  • dongzhuo1733 2011-11-13 19:11
    关注

    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.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services