Jump to content


Cannot use object of type stdClass as array


5 replies to this topic

#1 npsken

    Young Padawan

  • Members
  • Pip
  • 26 posts
  • Gender:Male
  • Interests:Computer Programming<br />Graphic Design

Posted 16 July 2009 - 02:15 PM

The following code works:
$file = "http://gdata.youtube.com/feeds/api/videos/fiAb1HuiHyA";
$xml = simplexml_load_file($file);
echo "Title:". $xml->title;

The following code returns an error:
$data = array();
$file = "http://gdata.youtube.com/feeds/api/videos/fiAb1HuiHyA";
$obj_video = simplexml_load_file($file);
$data[0] = (string) $obj_video->title;

The error being:
Fatal error: Cannot use object of type stdClass as array in C:\wamp\www\video\video.php on line 58

The overall goal is to get the title of the youtube video into an array.

Can anybody help?

#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 16 July 2009 - 05:57 PM

var_dump($obj_video);
var_dump($obj_video->title);
print_r($data);
Add that after you set $obj_video. What is the resulting output?

Edited by rc69, 16 July 2009 - 06:01 PM.


#3 npsken

    Young Padawan

  • Members
  • Pip
  • 26 posts
  • Gender:Male
  • Interests:Computer Programming<br />Graphic Design

Posted 16 July 2009 - 07:57 PM

[quote name='rc69' post='270301' date='Jul 16 2009, 06:57 PM']

title:


#4 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 16 July 2009 - 10:08 PM

Ok, i just tested your code and it worked fine for me (not even a warning). What version of PHP are you using?

Also, it looks like you trimmed down the code a bit to keep things simple, could you post the whole containing block and include a comment next to the line which is generating the error (i.e. what ever if-statment/while loop this code is running in)?

#5 npsken

    Young Padawan

  • Members
  • Pip
  • 26 posts
  • Gender:Male
  • Interests:Computer Programming<br />Graphic Design

Posted 16 July 2009 - 10:14 PM

View Postrc69, on Jul 16 2009, 11:08 PM, said:

Ok, i just tested your code and it worked fine for me (not even a warning). What version of PHP are you using?

Also, it looks like you trimmed down the code a bit to keep things simple, could you post the whole containing block and include a comment next to the line which is generating the error (i.e. what ever if-statment/while loop this code is running in)?

PHP 5.2.9-2

I've made a few changes, no more errors, but I can't extract data from the simplexml object. My original plan was to put everything into a data array, but I tried a slightly different approach that still isn't working.

If you could find a way to get all the data I want into an array that I can access in test2.php you would be a hero.

test2.php
<?php
//test 2
require("video.php");
$grabbr = new grabbr();
$grabbr->get_youtube("http://www.youtube.com/watch?v=fiAb1HuiHyA&eurl=http%3A%2F%2Fwww.youtube.com%2Fuser%2Fdeedlebag&feature=player_profilepage");
echo "<pre>";
echo $grabbr->get_title();
?>

video.php
<?php
class grabbr
{

// this is all the data
  public $id = null;
  public $title = null;
  public $postdate = null;
  public $description = null;
/////////////////////////////////

  //Youtube Function
  function get_youtube($url)
  {
    //parse youtube url into the id
    if (preg_match('%youtube\\.com/(.+)%', $url, $id)) {
      $id = $id[1];
      $replace = array("watch?v=", "v/", "vi/");
      $id = substr(str_replace($replace, "", $id), 0, 11);
    } else {
      return "Error with url.";
    }
   //get video object
   $file = "http://gdata.youtube.com/feeds/api/videos/".$id;
   $obj_video = simplexml_load_file($file);
   echo "<pre>";
   print_r($obj_video);
//this is where it's going wrong
   $title = $obj_video->title;
////////////////////////////////////////
  }
}
?>


#6 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 18 July 2009 - 12:10 PM

Well, like i said, i didn't have any problem with the code you posted earlier ($data[0] = ...). So if that doesn't work, i don't know what will.

The one obvious problem i see with test2.php though, is that there doesn't appear to be a get_title() method in grabbr. Even if there were, i have been working with Java for too long to remember PHP's scope resolution, but i believe you are setting your $title incorrectly.
$title = $obj_video->title;
// should probably be
$this->title = $obj_video->title;

//Then since it is public, you could just do the following in test2.php
echo $grabbr->title;






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users