wplearner
wplearner

Reputation: 415

simplexml_load_file not able to fetch data from a URL

I am using simplexml_load_file function to fetch data from a URL which contains XML content. There are few parameters in URL and simplexml_load_file is working correctly normally.

But there are few parameters which contains & and for these simplexml_load_file function is not working. This is my code. I tried urlencoding function but even this one is not working. This is how my code look like

$api_url = urlencode($api_url);
$xml = simplexml_load_file($api_url);

This is how URL look like

https://atlas.atdw-online.com.au/api/atlas/products?key=MYKEY&ar=Merimbula & Sapphire Coast&cats=ATTRACTION&size=10

In above URL you can see parameter ar has value which contains & and simplexml_load_file is not working for these type of parameters. Any suggestion regarding this will be much appreciated.

Upvotes: 0

Views: 129

Answers (1)

Ro Achterberg
Ro Achterberg

Reputation: 2704

As you can see in your URL, the ampersand (&) has two functions:

  • As a separator for URL parameters.
  • As a character that is part of the value of such a parameter.

To avoid the ambiguity that is presented in your question, you generally urlencode() the URL parameter values.

You would then do something like $ar = urlencode('Merimbula & Sapphire Coast');. If you have no control over how the ar parameter is constructed, then you'll have to remove the ampersand from "Merimbula & Sapphire Coast" and replace it with something else (like 'and').

Upvotes: 1

Related Questions