0

I am creating a small php app that pulls data from a remote website its working great but i would like to make it more user friendly now.

I need to get a few specific items from the page and as far as I can tell the page looks like an xml file wen you look at sorce code but it has no style to it and appears as plain text so I don't really know what to do.

The page I am trying to get looks like this

    <channel>
      <name>data</name>
      <id>data</id>
      <img>data</img>
      <auther>data</auther>
      <mp3>data</mp3>
      <bio>data</bio>
   </channel>
    <channel>
      <name>data</name>
      <id>data</id>
      <img>data</img>
      <auther>data</auther>
      <mp3>data</mp3>
      <bio>data</bio>
   </channel>
    <channel>
      <name>data</name>
      <id>data</id>
      <img>data</img>
      <auther>data</auther>
      <mp3>data</mp3>
      <bio>data</bio>
   </channel>
    <channel>
      <name>data</name>
      <id>data</id>
      <img>data</img>
      <auther>data</auther>
      <mp3>data</mp3>
      <bio>data</bio>
   </channel>

I need to get all the data from each tag under the channel tag and keep it in the same order to echo it back out onto my own page in the same way.

How could i do this ? i tried using regex with the following patter

    $pattern = '<channel>
      <name>(.*)</name>
      <id>(.*)</id>
      <img>(.*)</img>
      <auther>(.*)</auther>
      <mp3>(.*)</mp3>
      <bio>(.*)</bio>
   </channel>';

but that doesn't work I really need the best and simplest way to do this.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
saveonsky
  • 51
  • 3
  • maybe `SimpleXMLElement` would be better way to get data from it? – Kamil Karkus Feb 15 '15 at 16:13
  • you mean something like $xml = simplexml_load_string($input); $callback = $xml->{"name"}; but how would i go about getting all the values under each channel and keeping them the same to echo out again as i think just getting them like that could mix stuff up from other channels no ? – saveonsky Feb 15 '15 at 16:16

2 Answers2

0
$SimpleXMLElement = new SimpleXMLElement($str);
foreach ($SimpleXMLElement->children() as $Channel) {
    foreach ($Channel->children() as $Child) {
        echo $Child->getName() . ' = ' . (string) $Child;
    }
}

this way you can use SimpleXMLElement, it's very easy

Kamil Karkus
  • 1,283
  • 1
  • 11
  • 29
  • i need to beable to get every tag under the channel tag one by one and echo them or encode them i dont see how this can be done using this method seen as i dont know witch ones are witch – saveonsky Feb 15 '15 at 17:23
0

I would "sanitize" the incoming data and make an xml document out of it. This can be done by simply wrapping it into a surrounding tag. (I name it channels). Having this, you can parse the data using DOM:

// Sanitize input data. Make an xml out of it
$xml  = '<channels>';
$xml .= file_get_contents($url);
$xml .= '</channels>';

// Create a document
$doc = new DOMDocument();
$doc->loadXML($xml);

// Iterate through channel elements
foreach($doc->getElementsByTagName('channel') as $channel) {
    echo $channel->getElementsByTagName('name')->item(0)->nodeValue . PHP_EOL;
    echo $channel->getElementsByTagName('id')->item(0)->nodeValue . PHP_EOL;
    // And so on ...
}
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • this way looks easy and good if i ever need to edit it i am gonna try it now – saveonsky Feb 15 '15 at 16:27
  • this is not working it adds the to the bottom on a new line but wen its adding the first tag its on the same line as and shows up as red – saveonsky Feb 15 '15 at 16:44
  • what??? Sorry I don't get you. – hek2mgl Feb 15 '15 at 16:45
  • it wasnt putting a space after sanatising to create xml file i fixed that with PHP_EOL it creates the file fine and if i echo it out it works greate i can see every thing but wen i try get the bits i need from inside every channel i get a blank page – saveonsky Feb 15 '15 at 16:57
  • I've tested it with the input data you've posted - it works – hek2mgl Feb 15 '15 at 17:17
  • hmm well wen it comes to live test i can get as far as grabbing the data and creating the xml file but other than that it wont echo out the specific name or auther – saveonsky Feb 15 '15 at 17:19
  • do you have teamviewer maybe you could help me live ? – saveonsky Feb 15 '15 at 17:34
  • i have tried it with error reporting and i am getting this Fatal error: Class 'DOMDocument' not found on line 35 – saveonsky Feb 15 '15 at 19:34
  • Which version of PHP you are using? – hek2mgl Feb 17 '15 at 10:03