Page 1 of 1

SimpleXML Undefined Variable

Posted: Fri Jun 04, 2010 4:29 am
by nzsystem
Hi

I am just beggining to learn PHP and am trying to use it to parse and XML document i am accessing, the xml document is:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
  <opensearch:Query searchTerms="Transformers"/>
  <opensearch:totalResults>8</opensearch:totalResults>
  <movies>
    <movie>
      <score>9</score>
      <popularity>3</popularity>

      <translated>true</translated>
      <language>en</language>
      <name>Transformers: Revenge of the Fallen</name>
      <alternative_name>Transformers 2</alternative_name>
      <type>movie</type>
      <id>8373</id>

      <imdb_id>tt1055369</imdb_id>
      <url>http://www.themoviedb.org/movie/8373</url>
      <rating>6.2</rating>
      <certification>PG-13</certification>
      <overview>Decepticon forces return to Earth on a mission to take Sam Witwicky prisoner, after the young hero learns the truth about the ancient origins of the Transformers. Joining the mission to protect humankind is Optimus Prime, who forms an alliance with international armies for a second epic battle.</overview>
      <released>2009-06-24</released>

      <images>
        <image type="poster" url="http://i1.themoviedb.org/posters/387/4bc92151017a3c57fe00d387/transformers-revenge-of-the-fallen-original.jpg" size="original" width="1080" height="1602" id="4bc92151017a3c57fe00d387"/>
        <image type="poster" url="http://i2.themoviedb.org/posters/387/4bc92151017a3c57fe00d387/transformers-revenge-of-the-fallen-mid.jpg" size="mid" width="500" height="742" id="4bc92151017a3c57fe00d387"/>
        <image type="poster" url="http://i3.themoviedb.org/posters/387/4bc92151017a3c57fe00d387/transformers-revenge-of-the-fallen-cover.jpg" size="cover" width="185" height="275" id="4bc92151017a3c57fe00d387"/>
        <image type="poster" url="http://i2.themoviedb.org/posters/387/4bc92151017a3c57fe00d387/transformers-revenge-of-the-fallen-thumb.jpg" size="thumb" width="92" height="137" id="4bc92151017a3c57fe00d387"/>
        <image type="backdrop" url="http://i3.themoviedb.org/backdrops/350/4bc92149017a3c57fe00d350/transformers-revenge-of-the-fallen-original.jpg" size="original" width="1920" height="1080" id="4bc92149017a3c57fe00d350"/>
        <image type="backdrop" url="http://i2.themoviedb.org/backdrops/350/4bc92149017a3c57fe00d350/transformers-revenge-of-the-fallen-poster.jpg" size="poster" width="780" height="439" id="4bc92149017a3c57fe00d350"/>
        <image type="backdrop" url="http://i3.themoviedb.org/backdrops/350/4bc92149017a3c57fe00d350/transformers-revenge-of-the-fallen-thumb.jpg" size="thumb" width="300" height="169" id="4bc92149017a3c57fe00d350"/>
      </images>

      <last_modified_at>2010-06-02 01:34:57</last_modified_at>
    </movie>
  </movies>
</OpenSearchDescription>
Now i have been following a few tutorials and they all seem to suggest the same thing but no matter what i have tried i get the following error:

Code: Select all

Notice: Undefined variable: OpenSearchDescription in D:\Apache\htdocs\MovieCatalogue\index.php on line 3
and here is the code I am using:

Code: Select all

<?php 
$xml = simplexml_load_file('http://api.themoviedb.org/2.1/Movie.search/en/xml/.../Transformers');
$name = $OpenSearchDescription->movies->movie->name;
?>
<html xml:lang="en" lang="en">
<head>
  <title><?php
  // The title will be read from the RSS 
  echo $name;
  ?>
  </title>
</head>
<body>

<h1>test</h1>

<?php
// Here we'll put a loop to include each item's title and description
?>

</body>
</html>
I am running PHP 5 and thought that SimpleXML was bundled with it but it just aint working for me, any help would be appreciated.

Thanks in advance!

Re: SimpleXML Undefined Variable

Posted: Fri Jun 04, 2010 4:36 am
by greyhoundcode
Try going through the $xml object:

Code: Select all

$xml = simplexml_load_file('http://api.themoviedb.org/2.1/Movie.search/en/xml/.../Transformers');
$name = $xml->OpenSearchDescription->movies->movie->name;
(Not tested! Can't remember if you need the parent element or not)

Re: SimpleXML Undefined Variable

Posted: Fri Jun 04, 2010 4:38 am
by markusn00b
Edit: too late.

No, you don't reference to document-root (@previous poster).

Re: SimpleXML Undefined Variable

Posted: Fri Jun 04, 2010 5:20 am
by nzsystem
Yeah adding xml to the start just gives this error instead:

Code: Select all

Notice: Trying to get property on non-object in D:\Apache\htdocs\MovieCatalogue\index.php on line 3
Any other suggestions as to what I could be doing wrong?

Re: SimpleXML Undefined Variable

Posted: Fri Jun 04, 2010 5:28 am
by markusn00b
nzsystem wrote:Yeah adding xml to the start just gives this error instead:

Code: Select all

Notice: Trying to get property on non-object in D:\Apache\htdocs\MovieCatalogue\index.php on line 3
Any other suggestions as to what I could be doing wrong?
Post the code you're now using.

Re: SimpleXML Undefined Variable

Posted: Fri Jun 04, 2010 5:32 am
by nzsystem
The code i tried using was exactly the same as the original apart from it started at xml and not OpenSearchDescription as shown below:

Code: Select all

<?php 
$xml = simplexml_load_file('http://api.themoviedb.org/2.1/Movie.search/en/xml/.../Transformers');
$name = $xml->OpenSearchDescription->movies->movie->name;
?>
<html xml:lang="en" lang="en">
<head>
  <title><?php
  // The title will be read from the RSS 
  echo $name;
  ?>
  </title>
</head>
<body>

<h1>test<?php // The title will be read from the RSS again ?></h1>

<?php
// Here we'll put a loop to include each item's title and description
?>

</body>
</html>

Re: SimpleXML Undefined Variable

Posted: Fri Jun 04, 2010 5:34 am
by markusn00b
Like I said before, you don't reference the document-root; your $xml variable already does this. Your document-root is the root element of your XML (OpenSearhDescription).

So, simply remove OpenSearchDescription.

var_dump() your $xml to see it's layout.

Re: SimpleXML Undefined Variable

Posted: Fri Jun 04, 2010 4:01 pm
by nzsystem
I tried changing it so it started with movies and it is still coming up with the error:

Code: Select all

Notice: Undefined variable: movies in D:\Apache\htdocs\MovieCatalogue\index.php on line 3
and also has this error at the same time:

Code: Select all

Notice: Trying to get property of non-object in D:\Apache\htdocs\MovieCatalogue\index.php on line 3
This is my code i am trying to use now:

Code: Select all

<?php 
$xml = simplexml_load_file('http://api.themoviedb.org/2.1/Movie.search/en/xml/9301bee61d34d15ab5ef8f9934263112/Transformers');
$name = $movies->movie->name;
?>
<html xml:lang="en" lang="en">
<head>
  <title><?php
  // The title will be read from the RSS 
  echo $name;
  
  ?>
  </title>
</head>
<body>

<h1>
test
<?php
// The title will be read from the RSS again 
?>
</h1>

<?php
// Here we'll put a loop to include each item's title and description
?>

</body>
</html>
and this is the start of what the var_dump as suggested above gives out:

Code: Select all

object(SimpleXMLElement)#1 (1) {
  ["movies"]=>
  object(SimpleXMLElement)#2 (1) {
    ["movie"]=>
    array(8) {
      [0]=>
      object(SimpleXMLElement)#3 (16) {
        ["score"]=>
        string(1) "9"
        ["popularity"]=>
        string(1) "3"
        ["translated"]=>
        string(4) "true"
        ["language"]=>
        string(2) "en"
        ["name"]=>
        string(35) "Transformers: Revenge of the Fallen"
        ["alternative_name"]=>
        string(14) "Transformers 2"
        ["type"]=>
        string(5) "movie"
        ["id"]=>
        string(4) "8373"
        ["imdb_id"]=>
        string(9) "tt1055369"
        ["url"]=>
        string(36) "http://www.themoviedb.org/movie/8373"
        ["rating"]=>
        string(3) "6.2"
        ["certification"]=>
        string(5) "PG-13"
        ["overview"]=>
        string(295) "Decepticon forces return to Earth on a mission to take Sam Witwicky prisoner, after the young hero learns the truth about the ancient origins of the Transformers. Joining the mission to protect humankind is Optimus Prime, who forms an alliance with international armies for a second epic battle."
        ["released"]=>
        string(10) "2009-06-24"
        ["images"]=>
        object(SimpleXMLElement)#11 (1) {
          ["image"]=>
          array(7) {
            [0]=>
            object(SimpleXMLElement)#12 (1) {
              ["@attributes"]=>
              array(6) {
                ["type"]=>
                string(6) "poster"
                ["url"]=>
                string(109) "http://i1.themoviedb.org/posters/387/4bc92151017a3c57fe00d387/transformers-revenge-of-the-fallen-original.jpg"
                ["size"]=>
                string(8) "original"
                ["width"]=>
                string(4) "1080"
                ["height"]=>
                string(4) "1602"
                ["id"]=>
                string(24) "4bc92151017a3c57fe00d387"
              }
            }
            [1]=>
From the var dump it looks like this should be working shouldn't it?

Re: SimpleXML Undefined Variable

Posted: Fri Jun 04, 2010 4:15 pm
by mikosiko
try:

$name = $xml->movies->movie->name;

Re: SimpleXML Undefined Variable

Posted: Sun Jun 06, 2010 12:22 am
by nzsystem
It worked, thanks heaps for the help guys!