Jump to content


Photo

Cannot use object of type stdClass as array


  • Please log in to reply
5 replies to this topic

#1 npsken

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:
[codebox]$file = "http://gdata.youtube...s/fiAb1HuiHyA";
$xml = simplexml_load_file($file);
echo "Title:". $xml->title;[/codebox]

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

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

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

Can anybody help?

#2 rc69

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

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']
object(SimpleXMLElement)#2 (8) {  ["id"]=>  string(53) "http://gdata.youtube.com/feeds/api/videos/fiAb1HuiHyA"  ["published"]=>  string(24) "2008-11-12T05:50:08.000Z"  ["updated"]=>  string(24) "2009-07-16T19:40:13.000Z"  ["category"]=>  array(15) {    [0]=>    object(SimpleXMLElement)#3 (1) {      ["@attributes"]=>      array(2) {        ["scheme"]=>        string(50) "http://gdata.youtube.com/schemas/2007/keywords.cat"        ["term"]=>        string(5) "cover"      }    }    [1]=>    object(SimpleXMLElement)#4 (1) {      ["@attributes"]=>      array(2) {        ["scheme"]=>        string(50) "http://gdata.youtube.com/schemas/2007/keywords.cat"        ["term"]=>        string(6) "hybrid"      }    }    [2]=>    object(SimpleXMLElement)#5 (1) {      ["@attributes"]=>      array(2) {        ["scheme"]=>        string(37) "http://schemas.google.com/g/2005#kind"        ["term"]=>        string(43) "http://gdata.youtube.com/schemas/2007#video"      }    }    [3]=>    object(SimpleXMLElement)#6 (1) {      ["@attributes"]=>      array(2) {        ["scheme"]=>        string(50) "http://gdata.youtube.com/schemas/2007/keywords.cat"        ["term"]=>        string(8) "umbrella"      }    }    [4]=>    object(SimpleXMLElement)#7 (1) {      ["@attributes"]=>      array(2) {        ["scheme"]=>        string(50) "http://gdata.youtube.com/schemas/2007/keywords.cat"        ["term"]=>        string(4) "2008"      }    }    [5]=>    object(SimpleXMLElement)#8 (1) {      ["@attributes"]=>      array(2) {        ["scheme"]=>        string(50) "http://gdata.youtube.com/schemas/2007/keywords.cat"        ["term"]=>        string(9) "deedlebag"      }    }    [6]=>    object(SimpleXMLElement)#9 (1) {      ["@attributes"]=>      array(2) {        ["scheme"]=>        string(50) "http://gdata.youtube.com/schemas/2007/keywords.cat"        ["term"]=>        string(7) "rihanna"      }    }    [7]=>    object(SimpleXMLElement)#10 (1) {      ["@attributes"]=>      array(2) {        ["scheme"]=>        string(50) "http://gdata.youtube.com/schemas/2007/keywords.cat"        ["term"]=>        string(4) "drum"      }    }    [8]=>    object(SimpleXMLElement)#11 (1) {      ["@attributes"]=>      array(2) {        ["scheme"]=>        string(50) "http://gdata.youtube.com/schemas/2007/keywords.cat"        ["term"]=>        string(9) "potgieter"      }    }    [9]=>    object(SimpleXMLElement)#12 (1) {      ["@attributes"]=>      array(2) {        ["scheme"]=>        string(50) "http://gdata.youtube.com/schemas/2007/keywords.cat"        ["term"]=>        string(5) "cobus"      }    }    [10]=>    object(SimpleXMLElement)#13 (1) {      ["@attributes"]=>      array(2) {        ["scheme"]=>        string(50) "http://gdata.youtube.com/schemas/2007/keywords.cat"        ["term"]=>        string(6) "series"      }    }    [11]=>    object(SimpleXMLElement)#14 (1) {      ["@attributes"]=>      array(2) {        ["scheme"]=>        string(50) "http://gdata.youtube.com/schemas/2007/keywords.cat"        ["term"]=>        string(5) "remix"      }    }    [12]=>    object(SimpleXMLElement)#15 (1) {      ["@attributes"]=>      array(2) {        ["scheme"]=>        string(50) "http://gdata.youtube.com/schemas/2007/keywords.cat"        ["term"]=>        string(7) "vanilla"      }    }    [13]=>    object(SimpleXMLElement)#16 (1) {      ["@attributes"]=>      array(3) {        ["scheme"]=>        string(52) "http://gdata.youtube.com/schemas/2007/categories.cat"        ["term"]=>        string(5) "Music"        ["label"]=>        string(5) "Music"      }    }    [14]=>    object(SimpleXMLElement)#17 (1) {      ["@attributes"]=>      array(2) {        ["scheme"]=>        string(50) "http://gdata.youtube.com/schemas/2007/keywords.cat"        ["term"]=>        string(3) "sky"      }    }  }  ["title"]=>  string(43) "Cobus - Vanilla Sky - Umbrella (Drum Cover)"  ["content"]=>  string(1441) "Laus Deo Semper.[url="http://www.new.facebook.com/pages/Cobus-Potgieter/18074843749"]http://www.new.facebook.com/pages/Cobus-Po...ter/18074843749[/url]www.cobuspotgieter.comThis is a drum cover (or remix) of Vanilla Sky's cover of Rihanna's song, Umbrella.(whoa, now that's one complicated sentence)Crazy amounts of fun, this one. Loved the groovy parts in the intro and outtro, aswell as the heavy stuff in the middle. Obviously the last few choruses were also quite nice to do <img src='http://www.pixel2life.com/forums/public/style_emoticons/<#EMO_DIR#>/smile.gif' class='bbc_emoticon' alt=':(' />(As for the outtro doublebass crossovers; it's the only part of the song that was written beforehand, the rest is all improvised on the spot, to keep things fresh and unpredictable.)I have to emphasize, I know calling this a "cover" is wrong, seeing as I only PARTLY stayed true to the song's drums (or lack thereof), through the WHOLE song i play some variations of the beats, phrasing and fills, since 99% of what i play is improvised. Even though i have done my fair share of session and studio drumming, that has little to zero effect on these video's, I kind of see them as live performances. I'm NOT aiming to be accurate to the original drums, I'm just aiming to enjoy myself. And I thouroughly succeeded in doing that, might I add <img src='http://www.pixel2life.com/forums/public/style_emoticons/<#EMO_DIR#>/smile.gif' class='bbc_emoticon' alt=':(' />Vanilla Sky - Umbrella(originally by Rihanna)is the copyrighted property of it's owner(s).(External) Drums by Cobus Potgieter.For any and all info you might need (equipment, sponsorships, recording process, updates) please visit www.cobuspotgieter.com and have a look around."  ["link"]=>  array(5) {    [0]=>    object(SimpleXMLElement)#18 (1) {      ["@attributes"]=>      array(3) {        ["rel"]=>        string(9) "alternate"        ["type"]=>        string(9) "text/html"        ["href"]=>        string(42) "http://www.youtube.com/watch?v=fiAb1HuiHyA"      }    }    [1]=>    object(SimpleXMLElement)#19 (1) {      ["@attributes"]=>      array(3) {        ["rel"]=>        string(53) "http://gdata.youtube.com/schemas/2007#video.responses"        ["type"]=>        string(20) "application/atom+xml"        ["href"]=>        string(63) "http://gdata.youtube.com/feeds/api/videos/fiAb1HuiHyA/responses"      }    }    [2]=>    object(SimpleXMLElement)#20 (1) {      ["@attributes"]=>      array(3) {        ["rel"]=>        string(51) "http://gdata.youtube.com/schemas/2007#video.related"        ["type"]=>        string(20) "application/atom+xml"        ["href"]=>        string(61) "http://gdata.youtube.com/feeds/api/videos/fiAb1HuiHyA/related"      }    }    [3]=>    object(SimpleXMLElement)#21 (1) {      ["@attributes"]=>      array(3) {        ["rel"]=>        string(44) "http://gdata.youtube.com/schemas/2007#mobile"        ["type"]=>        string(9) "text/html"        ["href"]=>        string(42) "http://m.youtube.com/details?v=fiAb1HuiHyA"      }    }    [4]=>    object(SimpleXMLElement)#22 (1) {      ["@attributes"]=>      array(3) {        ["rel"]=>        string(4) "self"        ["type"]=>        string(20) "application/atom+xml"        ["href"]=>        string(53) "http://gdata.youtube.com/feeds/api/videos/fiAb1HuiHyA"      }    }  }  ["author"]=>  object(SimpleXMLElement)#23 (2) {    ["name"]=>    string(9) "deedlebag"    ["uri"]=>    string(50) "http://gdata.youtube.com/feeds/api/users/deedlebag"  }}

title:
object(SimpleXMLElement)#24 (2) {  ["@attributes"]=>  array(1) {    ["type"]=>    string(4) "text"  }  [0]=>  string(43) "Cobus - Vanilla Sky - Umbrella (Drum Cover)"}


#4 rc69

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

npsken

    Young Padawan

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

Posted 16 July 2009 - 10:14 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)?


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 2require("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
<?phpclass 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

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;





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users