SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    Nov 2009
    Posts
    471
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    using simplexml_load_string

    I am using a third party ip lookup service which returns an xml formatted string (see $output below) as follows but $xml resolves to null in the statement simplexml_load_string($output):

    Code:
    	$request_url = "srvc name". $ip ."/key";
    
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL, $request_url);
    
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	$output = curl_exec($ch);
    	curl_close($ch);
    
    	$xml = simplexml_load_string($output);
    where $output resolves to:

    string(321) "
    <IpLookup>
    <Ip>xx.xxx.xx.xxx</Ip>
    <Code>US</Code>
    <Country>United States</Country>
    <Flag>http://srvc/flags/us.png</Flag>
    <City>West Hartford</City>
    <Region>Connecticut</Region>
    <Isp>AT&T Internet Services</Isp>
    <Organization>SBC Internet Services</Organization>
    <Lat>41.762</Lat>
    <Lng>-72.742</Lng>
    </IpLookup>
    "

  2. #2
    SitePoint Evangelist
    Join Date
    Nov 2009
    Posts
    471
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After further investigation, if you remover the & from AT&T the xml interpretation works fine. Does anyone know why or what other characters will cause this to fail?

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the XML is to construct by yourself then you can put those values inside CDATA. XML parser will just ignore anything inside CDATA in the XML node. Look at the page here http://www.w3schools.com/xml/xml_cdata.asp for more details and usage.

    If you do not have control over the XML, I hope they will add those texts/special characters in CDATA in their XML whoever prepares the XML.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Evangelist
    Join Date
    Nov 2009
    Posts
    471
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the info, but I am confused which isn't unusual. So does that mean that I cannot send the company name AT&T in XML? Is there not a way to escape the ampersand to use it as a regular character? I have tried &#38; which does not seem to work either. Again, thanks for the input.

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you did not see the link above, if you have seen it you would have seen the example too. It is possible to send those special characters too.
    [xml]
    <IpLookup>
    <Ip>xx.xxx.xx.xxx</Ip>
    <Code>US</Code>
    <Country>United States</Country>
    <Flag>http://srvc/flags/us.png</Flag>
    <City>West Hartford</City>
    <Region>Connecticut</Region>
    <Isp><![CDATA[AT&T Internet Services]]></Isp>
    <Organization>SBC Internet Services</Organization>
    <Lat>41.762</Lat>
    <Lng>-72.742</Lng>
    </IpLookup>
    [/xml]

    Look at the node <lsp> and text inside it and try.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  6. #6
    SitePoint Evangelist
    Join Date
    Nov 2009
    Posts
    471
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did see the example, it is possible you do not understand what I am asking. If I am getting a string file from a third party and using simplexml_load_string to be able to parse the elements in xml, and the string file contains an ampersand, simplexml_load_string will return null. So to be safe I need to remove the &. I do not know the xml ellements since it is a string file, not yet parseable in xml. right?

    Are there any additional characters besides & that will cause simplexml_load_string to return null?

    In a perfect world I should tell the third party sender not to send those files in that format I suppose. But if for whatever reason they can't or won't do that, do I have any alternatives other than str_replace to remove the & altogether? Or are you suggesting I write a routine to search for & and then find the previous > and the following < and concatenate and replace all the text enclosed within that?

  7. #7
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,095
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    simplexml_load_string should work if you replace & with &amp;
    PHP Code:
    $xml str_replace('&''&amp;'$xml); 
    There are a few more characters that need to be replaced, but that's harder to do. See http://stackoverflow.com/questions/7...ial-characters

    Are there any other formats available from that third party, like JSON or something? They way they output XML is clearly wrong, but maybe they got other formats right?
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  8. #8
    SitePoint Evangelist
    Join Date
    Nov 2009
    Posts
    471
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That works, thank you. I will check with them on JSON and tell them to correct their xml output but not sure if they will do it. In any event, I can easily to it myself.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •