I had an XML file with a url variable containing special characters such as &, = etc
I have two questions:
A. Since the special characters are not allowed in XML, how should I enter the URL? Should I encode the url and then put in XML file?
B. Assuming that I encode the url and put it in XML file and then read in a php script.
$fxml = simplexml_load_file("mylist.xml");
$fxml -> getName();
foreach($fxml->children() as $mylist)
{
$url = $mylist -> url;
$url = urldecode(url);
echo $url;
$feed = curl_init( $url );
curl_setopt($feed, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($feed, CURLOPT_HEADER, 0);
$xml = simplexml_load_string(curl_exec($feed));
curl_close($feed);
//---------------------
//---------------------
}
Here is the sample XML file:
<?xml version="1.0" encoding="UTF-8"?>
<feed>
<mylist>
<name>
SISC
</name>
<url>
http://epubs.siam.org/action/showFeed?ui=0&mi=3chvf9&ai=s0&jc=sjnaam&type=etoc&feed=rss
</url>
</mylist>
</feed>
Notice that I have replaced the URL in encoded form where original & is replaced by its code. I also tried not using urldecode but I only get echo $url correctly.
If I copy paste the url in curl_init($url ) under single quotes, everthing works.
But when I encode the url and put it in XML and decode back and put it in curl_init($url ), it does not work.
When I echo the decoded $url (see above) then I get the correct url list displayed. What am I doing wrong ?
I read closely related posts, but could not solve this.
Let’s use
&for the sake of example.If you want a
&in the data of a URL (as opposed to indicating the start of a new query string key/pair). Then you need to URL encode it (as%26) before adding it to the URL.If you want a
&in the data of an XML document (e.g. if the data is a URL with a query string) then you need to XML encode it (as&). You should be constructing the XML with an XML library (and not string bashing or templating) and the library will take care of it for you.You should not have to
urldecodedata you take out of XML before using it as a URL.