• Hello, I have a website where people can share their YouTube videos. Right now, they have to fill in a form with the video URL, title, description etc. What I want to do is get the title, description, etc automatically from the video URL. Is there such a script or does someone know how to code this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • You might be able to used the google Youtube data API

    https://developers.google.com/youtube/

    But that might only give you access to your own videos.. the other alternative is to scrape the information i.e. load the video page in question in code and parse it for the information you want, but thats not exactly kosher behaviour and don’t be surprised if your server gets blocked by google.

    If google haven’t exposed the info in an api then they don’t want you to steal it..!

    Yes it can be done, I recommend using the youtube oembed api to grab basic information.

    For example:
    https://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch%3Fv%3DbDOYN-6gdRE&format=json

    Look into wp_oembed_get() If you pass in the url it will do a lot of the work for you.

    Thread Starter xWafflecakes

    (@xwafflecakes)

    I’ve been trying to get this done now for hours, but it just doesn’t work. This is what I have, for some reason it’s not showing the title:

    $youtube = "https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=" . $video_id . "&format=json";
    
    $json_code = file_get_contents($youtube);
    $json_object = json_decode($json_code);
    $video_title = $json_object->title;
    
    return $video_title;

    ($video_id is a variable declared by another function)

    Thread Starter xWafflecakes

    (@xwafflecakes)

    Ok so I tried to just echo $video_title and it seems that the title IS actually stored in the variable. So there must be a problem with Gravity Forms… I’m trying to populate the title field of the form with this variable using the save_field_value function:

    add_filter("gform_save_field_value", "get_title", 10, 4);
        	function get_title($value, $lead, $field, $form){
        	global $video_id;
    
        	//if not the form with fields to encode, just return the unaltered value without checking the fields
        	if(absint($form["id"]) <> 1)
                return $value;
    
        	//array of field ids to encode
        	$encode_fields = array(7);
    
        	$youtube = "https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=" . $video_id . "&format=json";
    
        	$json_code = file_get_contents($youtube);
        	$json_object = json_decode($json_code);
        	$video_title = $json_object->title;
    	echo $video_title;
    
        	//see if the current field id is in the array of fields to encode; encode if so, otherwise return unaltered value
        	if(in_array($field["id"],$encode_fields))
        	        return $video_title;
        	else
                	return $value;
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Getting YouTube video information automatically’ is closed to new replies.