0

我正在编写一个函数来构建一个 URL,这里是:

public static function requestContent($p_lParameters)
{
    $sParameters = "?key=TEST&format=json&jsoncallback=none";

    foreach($p_lParameters as $sParameterName => $sParameterValue)
    {
        $sParameters .= "&$sParameterName=$sParameterValue";
    }

    echo "<span style='font-size: 16px;'>URL : http://api.oodle.com/api/v2/listings" . $sParameters . "</span><br />";

    $aXMLData = file_get_contents("http://api.oodle.com/api/v2/listings" . $sParameters);

    return json_decode($aXMLData,true);
}

我用这个数组列表调用这个函数:

print_r() result : Array ( [region] => canada [category] => housing/sale/home )

但这很奇怪,我得到了一个意想不到的字符(注意特殊字符 none**®**ion):

http://api.oodle.com/api/v2/listings?key=TEST&format=json&jsoncallback=none®ion=canada&category=housing/sale/home

有关信息,我使用此标头:

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<?php header('Content-Type: text/html;charset=UTF-8'); ?>

编辑 :

$sRequest = "http://api.oodle.com/api/v2/listings?key=TEST&format=json&jsoncallback=none&region=canada&category=housing/sale/home";

echo "<span style='font-size: 16px;'>URL : " . $sRequest . "</span><br />";

返回有问题的确切 URL:

http://api.oodle.com/api/v2/listings?key=TEST&format=json&jsoncallback=none®ion=canada&category=housing/sale/home
4

2 回答 2

1

这是解决方案,这次它会起作用

$sParameters .= "&$sParameterName=$sParameterValue";
$sParameters = htmlentities($sParameters);

它将所有字符集转换为 html 代码.. 即使我经常在用户输入中使用它,也完全忘记了这一点......

于 2012-11-19T04:45:01.610 回答
0

好吧,首先你建立一个字符串

$sParameters = "?key=TEST&format=json&jsoncallback=none";

然后你附加到那个。所以它会串联。现在,字符串的最后一部分可能是 & 而您的参数是区域。

不知何故,它被转换为 html ASCII 代码 ® 这会导致注册符号出现。

于 2012-11-19T04:29:33.113 回答