1

Ok so I'm sure there's a super simple solution for this, but I have to generate a string to use to grab a json file. I know I should be using & amp; instead of &, but for some reason it's not working for me. My problem is I'm getting ¤ when trying to create my string:

$apistr     = 'https://remitradar.com/JsonRequests.aspx?action=getOnlineQuotes&companyKey='.$companyKey.'&countryFrom='.$countryFrom.'&countryTo='.$countryTo.'&currencyFrom='.$currencyFrom.'&currencyTo='.$currencyTo.'&amount='.$amount;

It's spitting out:

https://remitradar.com/JsonRequests.aspx?action=getOnlineQuotes&companyKey=23e9b66aspp6z&countryFrom=AU&countryTo=FJ¤cyFrom=AUD¤cyTo=FJD&amount=200

Instead of:

https://remitradar.com/JsonRequests.aspx?action=getOnlineQuotes&companyKey=23e9b66aspp6z&countryFrom=AU&countryTo=FJ&CurrencyFrom=AUD&CurrencyTo=FJD&amount=200

Any help would be great.

Edit:

This is the var_dump of the variables and the url:

string(13) "23e9b66aspp6z"
string(2) "AU"
string(2) "FJ"
string(3) "AUD"
string(3) "FJD"
string(3) "200"
string(159) "https://remitradar.com/JsonRequests.aspx?action=getOnlineQuotes&companyKey=23e9b66aspp6z&countryFrom=AU&countryTo=FJ¤cyFrom=AUD¤cyTo=FJD&amount=200" 
SoulieBaby
  • 5,405
  • 25
  • 95
  • 145

2 Answers2

2

You should urlencode the content of the variables:

$apistr     = 'https://remitradar.com/JsonRequests.aspx?action=getOnlineQuotes&companyKey='.urlencode($companyKey).'&countryFrom='.urlencode($countryFrom).'&countryTo='.urlencode($countryTo).'&currencyFrom='.urlencode($currencyFrom).'&currencyTo='.urlencode($currencyTo).'&amount='.urlencode($amount);

Than it may also get more clear if there is content in them you did not expect.

And no, you should not use & to code an & in the url.

To check the contents of the variables you may do:

var_dump($companyKey);
var_dump($countryFrom);
var_dump($countryTo);
var_dump($currencyFrom);
var_dump($currencyTo);
var_dump($amount);
var_dump($apistr);

If you echo the content of $apistr to your webbrowser &curren will be displayed as the currency glyph ¤ as the html entity ¤ is reserved.

Try to echo it this way to your browser instead (but dont use this as url! The variable $apistr contains what you expect - only the debug echo output was wrong in case of the rendering of your browser):

echo htmlspecialchars($apistr);

When ever you just output a string of characters your rendering application is your webbrowser. You also may look at the sourcecode of the webside containing the presumed wrong url. You should see the correct characters in the source. The output of htmlspecialchars($apistr); however would be look wrong in the source code but correct in the rendered webpage.

cmks
  • 527
  • 2
  • 11
1

I have a few things to mention.

  1. I find manually forming the querystring of a url via string concatenation to be very long, messy, and harder to maintain. Furthermore, to ensure that the querystring is correctly encoded, you end up needing to make n number of urlencode() calls which isn't terribly pretty/efficient. I always promote the readability, cleanliness and reliability of http_build_query() for this job. Just note that if you have any null values in your data, then the key-value pair will be omitted.

  2. There ARE legitimate reasons to use & over & as the glue in the querystring. I personally wondered why I see this technique so often in Joomla scripts and got an answer supported by good documentation. Why are Joomla url query strings commonly delimited with “&” instead of “&”?

  3. This issue is more about the visual rendering of the url on an html page than it is about maintaining the viability of the hyperlink. In fact, when written into a href attribute, there is no corruption of the data -- the &s are auto-magically encoded to &. When you mouseover the link, the url is exactly as intended. It is only when the browser is rendering the visible text for the html document that it misinterprets your intended output and converts &curren to ¤. Essentially, you only need to call htmlentities() on the visible text that you are adding to the document.

Here's a demonstration that keeps your code "skinny" (not requiring excessive horizontal scrolling) using http_build_query():

$queryParams = [
    'action' => 'getOnlineQuotes',
    'companyKey' => '23e9b66aspp6z',
    'countryFrom' => 'AU',
    'countryTo' => 'FJ',
    'currencyFrom' => 'AUD',
    'currencyTo' => 'FJD',
    'amount' => '200'
];

$url = 'https://remitradar.com/JsonRequests.aspx?' . http_build_query($queryParams);
echo "<a href=\"{$url}\">" .
         $url .
         "<br><br>" . 
         htmlentities($url) . 
     "</a>";

Source Code:

<a href="https://remitradar.com/JsonRequests.aspx?action=getOnlineQuotes&amp;companyKey=23e9b66aspp6z&amp;countryFrom=AU&amp;countryTo=FJ&amp;currencyFrom=AUD&amp;currencyTo=FJD&amp;amount=200">
    https://remitradar.com/JsonRequests.aspx?action=getOnlineQuotes&amp;companyKey=23e9b66aspp6z&amp;countryFrom=AU&amp;countryTo=FJ¤cyFrom=AUD¤cyTo=FJD&amp;amount=200

    https://remitradar.com/JsonRequests.aspx?action=getOnlineQuotes&amp;companyKey=23e9b66aspp6z&amp;countryFrom=AU&amp;countryTo=FJ&amp;currencyFrom=AUD&amp;currencyTo=FJD&amp;amount=200
</a>

Visible Output:

https://remitradar.com/JsonRequests.aspx?action=getOnlineQuotes&companyKey=23e9b66aspp6z&countryFrom=AU&countryTo=FJ¤cyFrom=AUD¤cyTo=FJD&amount=200

https://remitradar.com/JsonRequests.aspx?action=getOnlineQuotes&companyKey=23e9b66aspp6z&countryFrom=AU&countryTo=FJ&currencyFrom=AUD&currencyTo=FJD&amount=200
mickmackusa
  • 43,625
  • 12
  • 83
  • 136