Jump to content

PHP Foreach Appending XML


codeinphp

Recommended Posts

Having some issues with appending an XML file thru a foreach loop. First some info on the script. The script opens an XML file on my server and reads thru the contents extracting url values. It takes that url value (stored in $urlhtml) and passes to a curl function which in turn curls the url, parse information and passes that thru variable $html back to script. The value of $html is then appended to the XML in a particular location. The initial curl ($urlhtml) is a website with TV shows and the $urlhtml represents a particular show. The $html value represents each episode returned from the curl, which needs to be stored under the show being curled. Following an example of the xml sturcture and below that is the script that does this.  What is happening is that the url of the is taken from the XML, the foreach loop is entered, the curl is made and the url for each episode is extracted and appended into the xml, but the problem is that instead of the episode url being appended under respective item element in the xml(where the show is located), all the urls for all episodes are appended under the first item in the XML file. So after executing, if I look at the xml file, all of my shows are under a different item element as needed, but all episode titles and episode urls for all shows are located under one item element, not under it's respective show. Any help or advice in getting episodes under it's show would be greatly appreciated.

Thanks

<feed>
      <item>
         <showname>Name of Show Goes Here</showname>
         <streamUrl>http://locationofshowstream.com</streamUrl>
         <episodetitle>Name of Episode Goes Here</episodetitle>
         <episodeurl>URL of the Episode Goes Here</episodeurl>
     </item>
</feed>


$xml = new SimpleXMLElement(file_get_contents($xmllocation));
if (!$xml){
echo 'ERROR WHILE PARSING DOCUMENT';
} else{
               foreach($xml->item as  $item){
               $url=$item->streamUrl;
              if (!$url){
              echo 'ERROR WHILE PARSING';
}else{
                       
$html = file_get_html($url);
$xmlappend=simplexml_load_file($xmllocation);
$elements=$html->find('a[class=main-tt]');
    foreach($elements as $element) {
                      $stitle=$element->title;
$streamer=$element->href;
$urlhtml=curlurl2($streamer); //CURL EACH 
$episodeurl=substr($urlhtml,2); //CREATES THE FULL URL PATH
$epitem= $xmlappend->item;
$episodetitle=$xmlappend->item->addChild('eptitle', $stitle);
$episode=$xmlappend->item->addchild('epiurl', $episodeurl);
} 
$xmlappend->asXML($xmlsave);
   }
  }
    } 
Edited by codeinphp
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.