Reputation: 23
how to extract the description of a video from youtube using php?
if possible with the formatting of line breaks.
want to extract plain text it will save to txt with php.
thank you
Upvotes: 0
Views: 2370
Reputation: 1203
None of the answers I found online for this question helped me. This is what I did instead:
$json = json_decode( file_get_contents('https://www.googleapis.com/youtube/v3/videos?id=YOUR_VIDEO_ID&key=YOUR_GOOGLE_API_KEY&part=snippet,contentDetails,statistics,status'), TRUE );
$description = nl2br( $json['items'][0]['snippet']['description'] );
You can get the Google API key from here (Only steps 1 and 2 need to be followed since you only need the API key).
The nl2br function is used to convert newlines into line breaks.
Hope this saves someone else some time.
Upvotes: 1
Reputation: 4112
I use the following class to access YouTube.
http://www.phpclasses.org/package/6336-PHP-Retrieve-information-about-videos-in-YouTube.html
A quick example of how it works;
<?
$obj = new youtube;
$obj->url = "http://www.youtube.com/watch?v=8B7s01DpBTg";
$obj->player("480","385");
print $info["description"];
?>
Upvotes: 0
Reputation: 1401
You can use the YouTube Data API.
Use this URL to get a json array for the specific video-id
KQdDiW_C2E4
is the video-id
v=2
tells the server to use v2 of the API
alt=json-in-script
tells the API to return a json array
If you json_decode
the returned array, you will find the description in
array->entry->media$group->media$description
Upvotes: 2
Reputation: 174
You could request a youtube video's html document using php's file_get_contents() function and then search for the <div>
containing the description and extract it that way.
I just looked at a youtube video page, and it looks like the description is inside the <p id="eow-description">
tag, although it is obviously not guaranteed to stay that way.
EDIT: I didn't realize Youtube provided an API. It would obviously be cleaner to use their API and ask for the information you want than to try and parse their html pages. Thanks to Sander for pointing this out.
Upvotes: 0