SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    flcikr api...parsing xml in to php

    HI,

    I need to mess about with the flickr api....i have constructed this url to do some testing with. It just gets a list of all the recent photos from my account. It works and i successfully get some xml back when i put my url in....

    Now ive never parsed xml in to php before ( ive done php in to xml but thats easy!) so i found this tutorial.....it uses this code.....thing is im stuck because the tutorial illustrates how to parse xml form a file ( using fopen etc) and doesnt deal with getting xml data from a url...heres the code:

    Code PHP:
      <?php
    $file = "xml_beginner.xml";
     
    function contents($parser, $data){
        echo $data;
    }
     
    function startTag($parser, $data){
        echo "<b>";
    }
     
    function endTag($parser, $data){
        echo "</b><br />";
    }
     
    $xml_parser = xml_parser_create();
     
    xml_set_element_handler($xml_parser, "startTag", "endTag");
     
    xml_set_character_data_handler($xml_parser, "contents");
     
    $fp = fopen($file, "r");
     
    $data = fread($fp, 80000);
     
    if(!(xml_parse($xml_parser, $data, feof($fp)))){
        die("Error on line " . xml_get_current_line_number($xml_parser));
    }
     
    xml_parser_free($xml_parser);
     
    fclose($fp);
     
    ?>

    So could anyone point me in the right direction to use a url instead of the file? Thanks

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I think a good way would be to save the retreived data into a temporary file, then run the script.

    For example:
    PHP Code:
    <?
    $tempxml 
    "tempxmlfile.xml";
    $cont file_get_contents("http://the-flickr-url/");
    $filewrite fopen($tempxml"w");
    fwrite($filewrite$cont);
    fclose($filewrite);
    $file $tempxml;
    function 
    contents($parser$data){
        echo 
    $data;
    }
     
    function 
    startTag($parser$data){
        echo 
    "<b>";
    }
     
    function 
    endTag($parser$data){
        echo 
    "</b><br />";
    }
     
    $xml_parser xml_parser_create();
    xml_set_element_handler($xml_parser"startTag""endTag");
    xml_set_character_data_handler($xml_parser"contents");
    $fp fopen($file"r");
    $data fread($fp80000);
    if(!(
    xml_parse($xml_parser$datafeof($fp)))){
        die(
    "Error on line " xml_get_current_line_number($xml_parser));
    }
    xml_parser_free($xml_parser);
    fclose($fp);
    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aha. I'll give that a whirl...thanks arkinstall


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
  •