Alexandre Lavoie
Alexandre Lavoie

Reputation: 8771

PHP URL parameters append return special character

I'm programming a function to build an URL, here it is :

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);
}

And I am calling this function with this array list :

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

But this is very strange I get an unexpected character (note the special character none**®**ion) :

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

For information I use this header :

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

EDIT :

$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 />";

return the exact URL with problem :

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

Upvotes: 0

Views: 337

Answers (2)

Dinesh
Dinesh

Reputation: 3105

here is the solution and this time it will work

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

It converts all the charset into html code.. totally forgot about this even when I regualarly use it in user input...

Upvotes: 1

Digitalis
Digitalis

Reputation: 570

Well, first you build a string

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

And then you're appending to that. So it will concatenate. Now, the last part of a string might be & and your parameter is region.

Somehow that gets converted to the html ASCII code &reg; which causes the registered symbol to appear.

Upvotes: 0

Related Questions