SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    String could not be parsed as xml

    Alrighty, here's the xml I'm trying to parse:

    Code:
    http://www.wowarmory.com/character-sheet.xml?r=Nazjatar&n=Underdeath
    PHP Code:
    $xml simplexml_load_file('http://www.wowarmory.com/character-sheet.xml?r=Nazjatar&n=Underdeath'); 
    Could anybody tell me what's wrong with that XML, I am not an expert but it seems ok to me.

    Thank you.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2009
    Location
    Austria
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is no xml-file and itīs no xhtml-file. Itīs just a html-file.
    Google: embedded javascript xhtml

  3. #3
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blubb View Post
    This is no xml-file and it´s no xhtml-file. It´s just a html-file.
    Google: embedded javascript xhtml
    It is XML in Firefox (try looking at page source). In Opera it shows as HTML. That's weird.

  4. #4
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Solved:

    PHP Code:
                    $url .= 'http://www.wowarmory.com/character-sheet.xml?r=Nazjatar&n=Underdeath';

                    
    $ch curl_init();
                    
    curl_setopt ($chCURLOPT_URL$url);
                    
    curl_setopt ($chCURLOPT_CONNECTTIMEOUT2);
                    
    curl_setopt ($chCURLOPT_RETURNTRANSFER1);
                    
    curl_setopt ($chCURLOPT_USERAGENT,
                                 
    'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
                    
    $xml curl_exec($ch);
                    
    curl_close($ch);

                    
    $xml simplexml_load_string($xml); 

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    The site responds with XML if it thinks the browser will apply the XSL transformations properly. We can use a stream context to send over a custom user-agent string in the headers of our request containing a value which will make the site respond with the desired XML.

    A brief example of using a stream context with SimpleXML:
    PHP Code:
    $url 'http://www.wowarmory.com/character-sheet.xml?r=Nazjatar&n=Underdeath';

    // Create a stream context, mention "Firefox/3.0" so the WoW Armory gives us XML
    $ctx stream_context_create(array('http' => array(
        
    'user_agent' => 'Sitepoint/1.0 (Not Firefox/3.0; See http://www.sitepoint.com/forums/showthread.php?t=624968)'
    )));

    // Ask libxml to use the context
    libxml_set_streams_context($ctx);

    $xml = new SimpleXMLElement($urlLIBXML_NOCDATATRUE);
    echo 
    $xml->asXML(); 
    Salathe
    Software Developer and PHP Manual Author.

  6. #6
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This means this site sends different output depending on which browser it detected.

    Maybe it uses other things when deciding which content to send, like weather or not your browser accept cookies or session cookies.

    Anyway, it looks like this website uses XSL templates and sends XML to browser in most cases but if it's not sure that the browser can transform XML to HTML correctly, then it does the transformation of the server and sends the HTML.

    The weird thing to me is that it sends XML to Firefox but not to Chrome. It's a known fact that Firefox handles XSL transformation the worst of all browsers.

  7. #7
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes that is the case: the website serves different content depending on web browser. I have solved the problem like this:

    PHP Code:
                    $ch curl_init();
                    
    curl_setopt ($chCURLOPT_URL$url);
                    
    curl_setopt ($chCURLOPT_CONNECTTIMEOUT2);
                    
    curl_setopt ($chCURLOPT_RETURNTRANSFER1);
                    
    curl_setopt ($chCURLOPT_USERAGENT,
                                 
    'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11');
                    
    $xml curl_exec($ch);
                    
    curl_close($ch); 

  8. #8
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    I sent two replies yesterday showing you how to use a custom user-agent with streams, any idea why they would be moderated?
    Salathe
    Software Developer and PHP Manual Author.

  9. #9
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    I sent two replies yesterday showing you how to use a custom user-agent with streams, any idea why they would be moderated?
    I don't know but it happened also to me, 2 or 3 three of my posts from yesterday disappeared...


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
  •