Grab a Piece of code/text from Youtube

Discussion in 'PHP' started by Fl1p, Oct 24, 2008.

  1. #1
    Hey guys,

    Need some PHP help here.

    What I'm trying to do is grab a piece of text/code from youtube page source with PHP.

    Example:
    http://www.youtube.com/watch?v=f-rqN0oUEXY

    If you view the page source of that video above and find the text " "t": " -- what I'm trying to do is grab that value of "t" (FYI this value always change everytime you refresh) with PHP coding.

    Any help will be greatly appreciated.

    Green rep and I can even send some cash to anyone who can help me out here.

    Thanks!
     
    Fl1p, Oct 24, 2008 IP
  2. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #2
    look into fsockopen/fopen/curl/file_get_contents (<- this is the easiest solution)

    and then preg_match or maybe strpos to get the value
     
    Kyosys, Oct 24, 2008 IP
    Fl1p likes this.
  3. Fl1p

    Fl1p Active Member

    Messages:
    511
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Thanks Kyosys. I *think* I got it working!

    Green rep to you!
     
    Fl1p, Oct 24, 2008 IP
  4. zac439

    zac439 Notable Member

    Messages:
    3,074
    Likes Received:
    214
    Best Answers:
    0
    Trophy Points:
    260
    #4
    Can you share which route you went with? I love to see this type of thing in action using different techniques.
     
    zac439, Oct 24, 2008 IP
  5. Sillysoft

    Sillysoft Active Member

    Messages:
    177
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Here is the code I used:

    
    
    //We include the snoopy class because cant use file_get_contents
    //on my server.
    include('snoopy_class.php');
    $snoopy = new Snoopy;
    
    //URL of the youtube page I want to grab the t value from. You can
    //put this into a form instead
    $url = 'http://www.youtube.com/watch?v=f-rqN0oUEXY';
    	
    //We fetch the page above from the url variable then put it into
    //a string variable
    $snoopy->fetch($url);
    $url_content = $snoopy->results;
    	
    //We now find a match within the string from the url above. Then
    //assign to a variable and echo the value.
    preg_match('#"t":[\s]?"(.*?)"#i',$url_content,$url_string);
    $t_string = trim($url_string[1]);
    echo $t_string;
    	
    
    PHP:
    You can see the script in action here: http://www.mlfscout.com/test.php
     
    Sillysoft, Oct 25, 2008 IP