Jump to content

reading an xml file + remotely


map200uk

Recommended Posts

hi,

 

i asked about reading lyrics for a song in before based on metadata, whilst i know the copyright implications etc, it is for my personal use, no profit or money gained from this at all, so i have been looking into it, i found somewhere with a nice database of lyrics which is used by some commercial products,

 

 

basically it works like this, u request a songs lyrics i.e. http://bla.com/lyrics.php?artist=a&song=b

 

now 1 xml file is returned saying the results,

 

<?xml version="1.0" encoding="UTF-8"?>

<lyrics>

<response code="0">SUCCESS</response>

<searchResults>

  <result id="120741" hid="VxwOBYpM3iY=" exactMatch="true">

    <title>Comfort Eagle</title>

    <feat/>

    <artist>

      <name>Cake</name>

    </artist>

  </result>

</searchResults>

</lyrics>

 

this is the result file, so we can see search result id is the id of it, and title obviously title etc

 

 

 

the hid is the element which we need-this we can use to get the lyrics for that specific song

 

http://bla/api_lyrics.php?auth=duane&hid=VxwOBYpM3iY=

 

 

which returns a file like:

 

<?xml version="1.0" encoding="UTF-8"?>

<lyrics>

<response code="0">SUCCESS</response>

<lyric hid="VxwOBYpM3iY=" id="120741">

  <title>Comfort Eagle</title>

  <feat/>

  <artist>

    <name>Cake</name>

  </artist>

  <albums>

    <album>

      <name>Comfort Eagle</name>

      <imageUrl>http://images.amazon.com/images/P/B00005MCW5.01.MZZZZZZZ.jpg</imageUrl>

    </album>

  </albums>

  <writer/>

  <text>We are building a religion

We are building it bigger

.................

Pendant keychains</text>

</lyric>

</lyrics>

 

so of this the only important bit is the actual lyrics or more like thats the only bits i am after, so the data between <text> and </text>

 

would it be possible to have a user input say xyz then have it read the file based on this

i.e.

 

fread(bla.com/song.php?artist=a&song=b)

 

then echo out the result id and search lyrics data from the 1st xml file?

 

then use those results to create links to a page which would in turn drag the data off the url where hid=id from XML file 1 , and then echoe out the data between <text></text>

 

uve been looking at php.net/xml and simplexml just wondered if anyone could possibly advise me

 

i wasnt sure if i was going about reading it correctly as the file is not localy, it will be like the script would have to read in a file based on a variable and use this to pass in a url to find the results response, to get the hid in order to get tjhe lyrics contained in 2nd xml file

 

the urls when access both 1st for the search and 2nd for lyrics return xml files, so readig it in even tho remotely should be the same as for local? Thanks

 

what i mean-in not so many words, would it work the same as what ive been reading about using xml with php if the file is a remote xml file? can i just use fread(location\blabla) ?

 

 

 

Thanks

 

any advice appreciate,

 

Mark,

 

mark

 

 

Link to comment
https://forums.phpfreaks.com/topic/48874-reading-an-xml-file-remotely/
Share on other sites

I haven't refer your long description.

 

Just refer this post.

 

<?php 

$arfiles= array("www.anysite.com/folder/basic2.xml"); 

foreach($arfiles as $arFile) 
{ 
    echo "<BR>-------------------------------------------------<BR>"; 
    echo "<BR>Parsing ".$arFile."<BR>"; 
    $insXmlParser= new clsXmlParser($arFile); 
     if($aArray=$insXmlParser->Parse()) {
    echo "<pre>";
print_r($aArray);
//echo LIST_CONTENTS($aArray); 
}
    echo "<BR>-------------------------------------------------<BR>"; 
} 
// Simple XML Parser  

class clsXmlParser { 

// general vars 
var $sTitle = ""; 
var $sLink = ""; 
var $sDescription = ""; 
var $arItems = array(); 
var $arsub = array(); 
var $itemCount = 0; 
var $prvTag="";   
var $uFiles = ''; 
var $xml_parser; 
var $curTag=""; 

function clsXmlParser($uFiles) 
{ 
     
    $this->uFiles = $uFiles; 
     
} 

function startElement($parser, $name, $attrs) { 
  
  $this->curTag .= "^$name";   
  //echo "start:  ".$this->curTag." <BR>"; 
  
} 

function endElement($parser, $name) 
{ 
   
  $caret_pos = strrpos($this->curTag,'^'); 
  $this->curTag = substr($this->curTag,0,$caret_pos); 
  //echo "end:  ".$this->curTag." <BR>"; 
} 

function characterData($parser, $data) {  
  
  if(trim($data) != "") 
  { 
        if(trim($this->prvTag) == "") 
              $this->prvTag=$this->curTag; 
      elseif(trim($this->prvTag) == trim($this->curTag)) 
      { 
          $this->arItems[] = $this->arsub; 
          $this->arsub = array(); 
         
        } 
   
        //find current element 
            $c_pos = strrpos($this->curTag,'^'); 
        $c_len = strlen($this->curTag); 
        $c_val = substr($this->curTag,($c_pos+1),$c_len); 

        //set data to sub array with the element name as the key 
        $this->arsub[$c_val] = $data; 
         
       
  } 

} 



function Parse() 
{ 
    $this->xml_parser = xml_parser_create(); 
         
     
    xml_set_object($this->xml_parser, &$this); 
     
    xml_set_element_handler($this->xml_parser, "startElement", "endElement"); 
    xml_set_character_data_handler($this->xml_parser, "characterData"); 
     
    if (!($fp = fopen($this->uFiles,"r")))  
    { 
      die ("could not open XML for input"); 
    } 
     
    while ($data = fread($fp, 4096))  
    { 
      if (!xml_parse($this->xml_parser, $data, feof($fp))) 
      { 
        die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->xml_parser)), xml_get_current_line_number($this->xml_parser))); 
      } 
    } 
    xml_parser_free($this->xml_parser); 
         
    //to handle the last array element 
    if(count($this->arsub)>0) 
    { 
         $this->arItems[] = $this->arsub; 
         $this->arsub = array(); 
    } 
         
    return $this->arItems; 

} 

} 

///----------END OF CLASS 


function LIST_CONTENTS($arrayname,$tab="&nbsp&nbsp&nbsp&nbsp",$indent=0)  
    {    
    // recursively displays contents of the array and sub-arrays:  
        // This function (c) Peter Kionga-Kamau  
        // Free for unrestricted use, except sale - do not resell.  
            // use: echo LIST_CONTENTS(array $arrayname, string $tab, int $indent);  
        // $tab = string to use as a tab, $indent = number of tabs to indent result  
        $retval=$currenttab=""; 
        while(list($key, $value) = each($arrayname))  
        {  
               for($i=0; $i<$indent; $i++) $currenttab .= $tab;  
            if (is_array($value))  
            {  
                $retval .= "$currenttab$key : Array: <BR>$currenttab{<BR>";  
                  $retval .= LIST_CONTENTS($value,$tab,$indent+1)."$currenttab}<BR>";  
            }  
               else $retval .= "$currenttab$key => $value<BR>";  
               $currenttab = NULL;  
        }  
        return $retval;  
    }  


?>

Your description leaves me wondering exactly what you want to do.  A while back I wrote a php app that grabbed RSS feeds (which are xml files) from news sites, parsed the data, and printed onto my page as html.  If this is what your are looking to do then you will want to investigate php's simplexml_file_load() function.  There is a post on my blog about this: http://www.cnizz.com/blog/?p=34 if you're interested.

hi

 

not sure if im being stupid, but

 

i cant get it to work

 

ive read some tutorials and whilst i can get some of it to work-the main part i need wont

 

<?xml version="1.0" encoding="UTF-8"?>

<lyrics>

<response code="0">SUCCESS</response>

<searchResults>

  <result id="120741" hid="VxwOBYpM3iY=" exactMatch="true">

    <title>Comfort Eagle</title>

    <feat/>

    <artist>

      <name>Cake</name>

    </artist>

  </result>

</searchResults>

</lyrics>

 

i need the id and hid which are attributed of the result tag and obviously i cannot do result->id as there is a whitespace (or at least i couldnt get it to work)!

 

i had a look at dom,http://www-128.ibm.com/developerworks/library/os-xmldomphp/

 

but im still no clearer on how to extract the id and hid as it is an attribute of the tag not a tag itself,

 

if anyone could offer some advice or pointers id be very greatful, im looking to extract just the id and hid from the result tag-im sure its easy and im being a fool

 

i did read about a class to read it al into an array, however i wanted to do this myself and learn-if anyone has any insight-much appreciated

 

thanks in advance guys!

 

mark,

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.