Link to home
Start Free TrialLog in
Avatar of rwone
rwone

asked on

how to get youtube video data with json and php?

hello,

i am trying to get the:

1.  title,
2.  date,
3.  duration,
4.  view count and
5.  description

of a youtube video via json and php.  

i've tried at least a dozen methods over two days to get this working and no luck, so working code examples appreciated as i've done all the research i think i can do (see relevant links to the api documentation at the end of this post).  

so what i am doing is:

- i have a form
- upon submit it is processed by a php file (insert.php)
- which makes some changes to the data and then submits it to the database

one of the errors i am getting is:

Catchable fatal error: Object of class stdClass could not be converted to string
this is the relevant code from insert.php:

(i think everything is fine before '// start getting youtube information')

// basic form information

$field1 = $_POST["field1"];
$field2 = $_POST["field2"];
$original_link = $_POST["link"];
$random_text = array_pop(explode('/',$original_link)); 

// add class and video display information

$final_value = "<a class=\'youtube\' href=\"http://www.youtube.com/embed/".$random_text."?rel=0&autohide=1&showinfo=0&autoplay=0&iv_load_policy=3&amp;wmode=transparent\">link</a>";
$thumb = "<img src=\"http://i.ytimg.com/vi/".$random_text."/mqdefault.jpg\">";

//start getting youtube information

$json = file_get_contents("http://gdata.youtube.com/feeds/api/videos/{$random_text}?v=2&alt=json");
$json_data = json_decode($json);
$video_title = $json_data->entry->title;
$video_date = $json_data->entry->published;
$video_duration = $json_data->entry->media:group->yt:duration;
$video_views = $json_data->entry->ytstatistics->viewCount;
$video_description = $json_data->entry->content;

// put it all together to create an <li>

$final_li_code = "<li class=\".{$field1} .{$field2}\">{$thumb}<div id=\"video_information\"><h3>{$video_title}</h3><div id=\"video_information_left\"><span id=\"date\">{$video_date}</span><span id=\"duration\">{$video_duration}</span><span id=\"another_id\">{$field2}</span></div><div id=\"video_information_right\"><span id=\"video_views\">{$video_views}</span><span id=\"yet_another_id\">{$field1}</span></div><span id=\"description\">{$video_description}</span></div></li>";

Open in new window

research

the feed and entry structure:

https://developers.google.com/youtube/2.0/developers_guide_protocol_understanding_video_feeds#Understanding_Video_Entries

the contents of an entry:

https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_entry

title tag:

https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_title

published tag:

https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_published

yt:duration tag:

https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_yt:duration

yt:statistics > viewCount tag:

https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_yt:statistics

content tag (video description):

https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_content
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rwone
rwone

ASKER

thank you, that url contains a variable ($random_text) that represents a video id, so it can be any video, for example, i just grabbed this video id off the front page:

http://gdata.youtube.com/feeds/api/videos/eiAx2kqmUpQ?v=2&alt=json

thank you.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rwone

ASKER

thank you.

i will describe what happens on form submit:

taken to the insert.php page.
the video details required are shown with items like string(41) prepending them and that numeral seems to represent the number of characters in the following string.  
it then displays the error message:
Catchable fatal error:  Object of class stdClass could not be converted to string
this refers to the line that starts the block of the <li> code and references the variables that have just been created with json (note: i do not know how to reference these variables the correct way):
$final_li_code = "<li class=\".$field_1 .$field_2\">$thumb<div id=\"video_information\"><h3>$video_title</h3><div id=\"video_information_left\"><span id=\"date\">$video_date</span><span id=\"duration\">$video_duration</span><span id=\"another_id\">$field_1</span></div><div id=\"video_information_right\"><span id=\"video_views\">$video_views</span><span id=\"yet_another_id\">$field_1</span></div><span id=\"description\">$video_description</span></div></li>";

Open in new window

the data is not added to the database

thank you.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rwone

ASKER

thank you for that extra information.

yes, the $final_li_code is posted above your last comment.  

the script is exactly as you defined except i just changed the $thing variables to the variables that i am using $video_title, $video_date etc.  

do you require this 'version' of the script to be posted here as well, or just the $final_li_code line above?  i thought perhaps the error was just coming from myself not knowing how to reference the variables properly in that line.    

thank you.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rwone

ASKER

thank you.  

there are 5 variables defined in the json area:

1.  $video_title
2.  $video_date
3.  $video_duration
4.  $video_views
5.  $video_description

these are all referenced in the $final_li_code variable as well as three other variables elsewhere in the document:

6.  $thumb
7.  $field1
8.  $field_2 (intentional underscore to represent actual syntax)

// get the data source
$url = "http://gdata.youtube.com/feeds/api/videos/eiAx2kqmUpQ?v=2&alt=json";
$jso = file_get_contents($url);

// MUNG THE JSON STRING TO MAKE THE PROPERTIES ACCESSIBLE
$jso = str_replace('$', '_', $jso);

// make the object
$obj = json_decode($jso);

// ACTIVATE THIS TO SEE THE OBJECT
// var_dump($obj);

// get the video title
$video_title = (string)$obj->entry->title->_t;
echo "TITLE: ";
var_dump($video_title);

// get the date the video was published
$video_date = $obj->entry->published->_t;
echo "DATE: ";
var_dump($video_date);

// get the duration of the video
foreach ($obj->entry->media_group->media_content as $content)
{
    $video_duration = (string)$content->duration;
}
echo "DURATION: ";
var_dump($video_duration);

// get the video view count
$video_views = $obj->entry->yt_statistics;
echo "VIEW COUNT: ";
var_dump($video_views->viewCount);

// get the video description
$video_description = $obj->entry->media_group->media_description->_t;
echo "DESCRIPTION: ";
var_dump($video_description);

// generate the li
$final_li_code = "<li class=\".$field1 .$field_2\">$thumb<div id=\"video_information\"><h3>$video_title</h3><div id=\"video_information_left\"><span id=\"date\">$video_date</span><span id=\"duration\">$video_duration</span><span id=\"field_2\">$field_2</span></div><div id=\"video_information_right\"><span id=\"video_views\">$video_views</span><span id=\"field1\">$field1</span></div><span id=\"description\">$video_description</span></div></li>";

Open in new window

thank you.
Avatar of rwone

ASKER

thank you for assistance thus far. signing off for the evening and will resume tomorrow.  

thank you.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rwone

ASKER

thank you.  

i have copied and pasted the code from:

line 9:  // get the data source

to:

line 41:  // GENERATE THE LI

the only change i made was to replace the variable $thing with the actual variable i was using $video_statistics ie on line 35:
$thing = $obj->entry->yt_statistics;
$video_views = (string)$thing->viewCount;

Open in new window

became:
$video_statistics = $obj->entry->yt_statistics;
$video_views = (string)$video_statistics->viewCount;

Open in new window

so referring back to the task of the insert.php file (ie to change the data submitted and then insert it into the database) i am assuming line 41 - 59 ($final_li_code) are in the format above for testing purposes however i would still need them shown as a string?  

when i leave the $final_li_code in the format below i get the error shown at the end of this post.  
$final_li_code = "<li class=\".$field1 .$field_2\">$thumb<div id=\"video_information\"><h3>$video_title</h3><div id=\"video_information_left\"><span id=\"date\">$video_date</span><span id=\"duration\">$video_duration</span><span id=\"another_id\">$field_2</span></div><div id=\"video_information_right\"><span id=\"video_views\">$video_views</span><span id=\"yet_another_id\">$field1</span></div><span id=\"description\">$video_description</span></div></li>";

Open in new window

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't smell: http://en.wikipedia.org/wiki/Wikipedia:Reference_desk/Archives/Language' at line 3

this is referring to the description area (ie $video_description) of this video:

http://www.youtube.com/watch?v=eiAx2kqmUpQ

and the error occurs at the first instance of a single apostrophe (').  

on another video i tested, the error occurred before the first instance of:
.

*****

Open in new window

so perhaps some sort of regex strip characters feature (that is a guess) is needed to escape these sorts of characters in the video description?  

thank you.
Avatar of rwone

ASKER

update: i have now read about heredoc and used the code for $final_li_code as was displayed in your last comment:

http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

it is outputting the same error as in my last post ie an error in the sql syntax corresponding to the $video_description variable at the first instances of characters like a full stop (.) or an apostrophe(') in the video description.  

thank you.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rwone

ASKER

thank you.

so the remaining section of code is (i know this is insecure but it is not public facing ie behind htaccess/passwd, so it is ok for the time being):
$con = mysql_connect("localhost","user_name","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("user_dbname", $con);

$sql="INSERT INTO data (fielda, field1, field_2, field3, link,li)
VALUES
('$_POST[fielda]','$_POST[field1]','$_POST[field_2]','$_POST[field3]','$final_value','$final_li_code')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
header ('Location: http://path/to/success.html');

mysql_close($con);
?>

Open in new window

so my question is now how do i use mysql_real_escape_string() on $final_li_code ?

i tried this but got the same error message:
$sql="INSERT INTO data (fielda, field1, field_2, field3, link,li)
VALUES
('$_POST[fielda]','$_POST[field1]','$_POST[field_2]','$_POST[field3]','$final_value','$final_li_code',mysql_real_escape_string($final_li_code))";

Open in new window

thank you.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rwone

ASKER

thank you very much for your assistance.

the code i modified to get it working was:
// escape characters
$safe_li = mysql_real_escape_string($final_li_code);

$sql="INSERT INTO data (fielda, field1, field_2, field3, link,li)
VALUES
('$_POST[name]','$_POST[type]','$_POST[muscle_group]','$_POST[field3]','$link','$safe_li')";
;

Open in new window


thank you.