Jump to content

Recommended Posts

My host turns allow_url_fopen to off for security reasons :( I used to parse RSS feeds just using

$doc = new DOMDocument(); //start new DOM

$doc->load( 'http://feedURLgoeshere ); //load XML file into the DOM

 

Which I can't do now. I have a replacement method using CURL from my host but it literally takes five minutes to load the page if it even works:

 

function My_simplexml_load_file($URL){ 
  
  $ch = curl_init($URL);

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HEADER, 0);

  $xml = simplexml_load_string(curl_exec($ch));

  curl_close($ch);

  return $xml;
}


function showFeed($URL){
  $xml = My_simplexml_load_file($URL);
  $i = 1;
  foreach( $xml->channel->item as $item ){ 
  	
if ($i <= 5){
  	echo "<li><a href=\"".trim($item->link)."\" >".htmlspecialchars(trim($item->title))."</a></li>\n";
	//echo "hi<br>\n";
	}
$i++;
}
}

showFeed('http://digg.com/rss/index.xml');

 

I'm not sure if it's because CURL is inefficient or if the code is not written right. I'm sure there's a better way than this.

To "get" the xml data use curl, rest, or soap.

If you want to parse it try

SimpleXML, Expat, or XMLDom.  However I don't personally recommend any of the

three.

I recommend googling (xml->array) for php.

In fact I put the data here for you.  As well as an example on some stuff I did in my spare time

with amazon services.

XML Class

<?php
/*

XML Parser Class
by Eric Rosebrock
http://www.phpfreaks.com

Class originated from: kris@h3x.com AT: http://www.devdump.com/phpxml.php

Usage:

<?php
include 'clsParseXML.php';

$xmlparse = &new ParseXML;
$xml = $xmlparse->GetXMLTree('/path/to/xmlfile.xml');

echo "<pre>";
print_r($xml);
echo "</pre>";
?>

The path to the XML file may be a local file or a URL.
Returns the elements of the XML file into an array with
it's subelements as keys and subarrays.

*/

class ParseXML{
      function GetChildren($vals, &$i) {
         $children = array(); // Contains node data
         if (isset($vals[$i]['value'])){
            $children['VALUE'] = $vals[$i]['value'];
         }

         while (++$i < count($vals)){
            switch ($vals[$i]['type']){

            case 'cdata':
               if (isset($children['VALUE'])){
                  $children['VALUE'] .= $vals[$i]['value'];
               } else {
                  $children['VALUE'] = $vals[$i]['value'];
               }
            break;

            case 'complete':
               if (isset($vals[$i]['attributes'])) {
                  $children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
                  $index = count($children[$vals[$i]['tag']])-1;

                  if (isset($vals[$i]['value'])){
                     $children[$vals[$i]['tag']][$index]['VALUE'] = $vals[$i]['value'];
                  } else {
                     $children[$vals[$i]['tag']][$index]['VALUE'] = '';
                  }
               } else {
                  if (isset($vals[$i]['value'])){
                     $children[$vals[$i]['tag']][]['VALUE'] = $vals[$i]['value'];
                  } else {
                     $children[$vals[$i]['tag']][]['VALUE'] = '';
                  }
               }
            break;

            case 'open':
               if (isset($vals[$i]['attributes'])) {
                  $children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
                  $index = count($children[$vals[$i]['tag']])-1;
                  $children[$vals[$i]['tag']][$index] = array_merge($children[$vals[$i]['tag']][$index],$this->GetChildren($vals, $i));
               } else {
                  $children[$vals[$i]['tag']][] = $this->GetChildren($vals, $i);
               }
            break;

            case 'close':
               return $children;
         }
      }
   }

      function GetXMLTree($xmlloc){
         if (file_exists($xmlloc)){
            $data = implode('', file($xmlloc));
         } else {
            $fp = fopen($xmlloc,'r');
            while(!feof($fp)){
               $data = $data . fread($fp, 1024);
            }

            fclose($fp);
         }

         $parser = xml_parser_create('ISO-8859-1');
         xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
         xml_parse_into_struct($parser, $data, $vals, $index);
         xml_parser_free($parser);

         $tree = array();
         $i = 0;

         if (isset($vals[$i]['attributes'])) {
            $tree[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
            $index = count($tree[$vals[$i]['tag']])-1;
            $tree[$vals[$i]['tag']][$index] =  array_merge($tree[$vals[$i]['tag']][$index], $this->GetChildren($vals, $i));
         } else {
            $tree[$vals[$i]['tag']][] = $this->GetChildren($vals, $i);
         }
      return $tree;
      }
}

Example

<?php
require_once("./amazon.class.php");
require_once("./xml.class.php");
// request 1
$amazon = new AmazonECS_Item;
$results = $amazon->ItemSearch('psychic', 'All', 'Medium');
$fp = fopen('temp.xml', 'w');
fwrite($fp, $results);
fclose($fp);
$xmlparse = &new ParseXML;
$xml = $xmlparse->GetXMLTree('temp.xml');
// second request
$amazon = new AmazonECS_Item;
$results = $amazon->ItemSearch('paranormal', 'All', 'Medium');
$fp = fopen('temp.xml', 'w');
fwrite($fp, $results);
fclose($fp);
$xmlparse = &new ParseXML;
$xml2 = $xmlparse->GetXMLTree('temp.xml');

//echo '<pre>';
//echo print_r($xml);
//echo '</pre>';
//exit;
?>

After that you'll have yourself an array that's easier than straight parsing xml.

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.