Jump to content

[SOLVED] Needing Youtube thumb nail fix.


NightZ

Recommended Posts

I have a script on a site that pulls in you-tube videos,

The problem is, that the thumb nails for the site, no longer show up.

 

I have tried to add codes to remove the line causing the problem but this fails me.

The code is this if anyone has a way to fix it.

 

<?php

 

function yt($ytsearch) {

 

global $ytcurrenttag;

global $ytvids;

global $ytid;

$ytvids = array();

$ytid=0;

 

function ytinsidethetag($ytparser, $ytdata){

global $ytcurrenttag;

global $ytvids;

global $ytid;

if(!strcmp(strtolower($ytcurrenttag),"title")) {

$ytvids[$ytid]['type'] = "yt";

$ytvids[$ytid]['title']=$ytdata;

$ytvids[$ytid]['title'][0] = ucwords($ytvids[$ytid]['title'][0]);

}

 

}

 

function ytopentag($ytparser, $ytdata, $ytattr=''){

global $ytcurrenttag;

$ytcurrenttag = $ytdata;

global $ytvids;

global $ytid;

if(!strcmp(strtolower($ytcurrenttag),"media:player")) {

$ytvids[$ytid]['link']=str_replace("watch?v=","v/",$ytattr['URL']);

$ytvids[$ytid]['thumb']=str_replace("http://www.youtube.com/watch?v=","http://img.youtube.com/vi/",$ytvids[$ytid]['link']);

$ytvids[$ytid]['thumb'].="/2.jpg";

}

}

 

function ytclosetag($ytparser, $ytdata){

global $ytid;

if(!strcmp(strtolower($ytdata),"entry"))

$ytid++;

}

 

$ytxml_parser = xml_parser_create();

 

xml_set_element_handler($ytxml_parser, "ytopentag", "ytclosetag");

 

xml_set_character_data_handler($ytxml_parser, "ytinsidethetag");

 

 

 

include("fetch.php");

$ytdata = fetchrssyt("http://gdata.youtube.com/feeds/videos?vq=$ytsearch&start-index=1&max-results=40");

if(!(xml_parse($ytxml_parser, $ytdata)))

die("Error on line " . xml_get_current_line_number($ytxml_parser));

 

xml_parser_free($ytxml_parser);

 

return $ytvids;

}

 

?>

 

The code in the browser for the thumb comes up.

http://img.youtube.com/vi/lOgQyIMX_XU&feature=youtube_gdata/2.jpg

 

and i think for it to work i need.

 

http://img.youtube.com/vi/lOgQyIMX_XU/2.jpg

lOgQyIMX_XU = whatever video needs to be displayed.

 

Someone said to try this.

$ytid = str_replace('feature=youtube_gdata', '',$ytid);

$ytid = str_replace('&', '',$ytid);

$ytid = str_replace('amp;', '',$ytid);

 

but it did nothing for the problem

Thanks

Link to comment
https://forums.phpfreaks.com/topic/178338-solved-needing-youtube-thumb-nail-fix/
Share on other sites

I can get a single image to show up for all the videos, by using your way.

This is what i have used up to now.

but this is still not right.

 

You just need to loop the xml results.

 

If you look at this url feed:

http://gdata.youtube.com/feeds/videos?vq=funny

 

You will get the source, now just extract what you want with pregmatch:

 

<media:thumbnail url="http://i.ytimg.com/vi/IytNBm8WA1c/2.jpg" height="90" width="120" time="00:03:54.500"/>

 

 

$src = file_get_contents("http://gdata.youtube.com/feeds/videos?vq=funny");

preg_match_all('~thumbnail url\s?=\s?[\'"](.*?).jpg[\'"]~is', $src, $match);

foreach ($match[1] as $tmb) {

echo "<img src='{$tmb}.jpg'  height='90' width='120'><br>";
}

 

Simple  ;)

Ive revised this, this will give you title, player and thumb

 

$src = file_get_contents("http://gdata.youtube.com/feeds/videos?vq=Gitano%20corazon&start-index=1&max-results=10");

preg_match_all('~<media:player url\s?=\s?[\'"](.*?)</media:title>~is', $src, $match);

foreach ($match[1] as $stuff) {

$tmb = explode('watch?v=',$stuff);
$tmb = explode('&feature',$tmb[1]);
$tmb = $tmb[0];

$title = explode("<media:title type='plain'>",$stuff);
$title = $title[1];

$play = explode("'/><media:thumbnail",$stuff);
$play = $play[0];


echo "<img src='http://i.ytimg.com/vi/{$tmb}/2.jpg'  height='90' width='120'><br>Title: {$title}<br>Player: {$play}<br>";
}

 

If i change this code part from this.

 

if(!strcmp(strtolower($ytcurrenttag),"media:player")) {

        $ytvids[$ytid]['link']=str_replace("watch?v=","v/",$ytattr['URL']);

        $ytvids[$ytid]['thumb']=str_replace("http://www.youtube.com/watch?v=","http://img.youtube.com/vi/",$ytvids[$ytid]['link']);

        $ytvids[$ytid]['thumb'].="/2.jpg";

      }

  }

 

 

to this.

if(!strcmp(strtolower($ytcurrenttag),"media:player")) {

        $ytvids[$ytid]['link']=str_replace("watch?v=","v/",$ytattr['URL']);

        $ytvids[$ytid]['thumb']=str_replace("http://www.youtube.com/watch?v=","http://img.youtube.com/vi/",$ytvids[$ytid]['thumb']);

        $ytvids[$ytid]['thumb'].="http://i4.ytimg.com/vi/KLpFnw-zFX4/default.jpg";

      }

  }

 

 

I get the thumb nails for all the videos,but they are all the same picture.(for every search result)

 

PHP already knows how to parse XML:

 

<?php
$xml = new SimpleXMLElement('http://gdata.youtube.com/feeds/videos?vq=Gitano%20corazon&start-index=1&max-results=10', null, true);

$xml->registerXPathNamespace('m', 'http://search.yahoo.com/mrss/');
foreach ($xml->xpath('//m:player') as $player) {
parse_str(parse_url($player['url'], PHP_URL_QUERY), $query);
$thumbnail = 'http://i.ytimg.com/vi/' . $query['v'] . '/2.jpg';

echo '<img src="' . $thumbnail . '">';
}

This code doesn't work right,

it is bringing in, a line of thumbnails across the top.(only the thumbs from the rss page show)

and are fixed in place.

 

I need the main code i'm using to be the same.

 

if(!strcmp(strtolower($ytcurrenttag),"media:player")) {

$ytvids[$ytid]['link']=str_replace("watch?v=","v/",$ytattr['URL']);

$ytvids[$ytid]['thumb']=str_replace("http://www.youtube.com/watch?v=","http://img.youtube.com/vi/",$ytvids[$ytid]['link']);

$ytvids[$ytid]['thumb'].="/2.jpg";

}

}

 

As the thumbs try to load, they are adding in address-bar the line.

 

&feature=youtube_gdata

 

This is the problem,if i take this out from the address-bar,the thumb appears.

 

(also can you tell me, where to place the codes that you give.Am i meant to just add them within my original code or is it to replace my code)

 

 

 

 

  • 2 weeks later...

The code you gave me doesn't works with the site,

 

Here is the site error.

http://holybiblez.com/music/video/index.php?search=meet%20joe%20black&source=all

 

Your code brings in a line of thumbnails,across the top but not in the search boxes.

(im php retarded as well)

Well, it's obviously not plug and play.

 

If you have this:

<?php
$xml = new SimpleXMLElement('http://gdata.youtube.com/feeds/videos?vq=Gitano%20corazon&start-index=1&max-results=10', null, true);

$xml->registerXPathNamespace('m', 'http://search.yahoo.com/mrss/');

$videos = array();
foreach ($xml->xpath('//m:player') as $player) {
    parse_str(parse_url($player['url'], PHP_URL_QUERY), $query);
    
    $videos[] = array(
        'url' => (string) $player['url'],
        'thumbnail' => 'http://i.ytimg.com/vi/' . $query['v'] . '/2.jpg',
    );
}

print_r($videos);

 

You'll get something like this:

Array
(
    [0] => Array
        (
            [url] => http://www.youtube.com/watch?v=Fi679_RsBGM&feature=youtube_gdata
            [thumbnail] => http://i.ytimg.com/vi/Fi679_RsBGM/2.jpg
        )

    [1] => Array
        (
            [url] => http://www.youtube.com/watch?v=S2afjJjeHhk&feature=youtube_gdata
            [thumbnail] => http://i.ytimg.com/vi/S2afjJjeHhk/2.jpg
        )

    [2] => Array
        (
            [url] => http://www.youtube.com/watch?v=CEX8kG6DRGA&feature=youtube_gdata
            [thumbnail] => http://i.ytimg.com/vi/CEX8kG6DRGA/2.jpg
        )

    [3] => Array
        (
            [url] => http://www.youtube.com/watch?v=bfg4d8_xJes&feature=youtube_gdata
            [thumbnail] => http://i.ytimg.com/vi/bfg4d8_xJes/2.jpg
        )

    [4] => Array
        (
            [url] => http://www.youtube.com/watch?v=voYyPpTFSdM&feature=youtube_gdata
            [thumbnail] => http://i.ytimg.com/vi/voYyPpTFSdM/2.jpg
        )

    [5] => Array
        (
            [url] => http://www.youtube.com/watch?v=ht79Rq4dQKo&feature=youtube_gdata
            [thumbnail] => http://i.ytimg.com/vi/ht79Rq4dQKo/2.jpg
        )

    [6] => Array
        (
            [url] => http://www.youtube.com/watch?v=K2JV3f52p3E&feature=youtube_gdata
            [thumbnail] => http://i.ytimg.com/vi/K2JV3f52p3E/2.jpg
        )

    [7] => Array
        (
            [url] => http://www.youtube.com/watch?v=brK9xMdziac&feature=youtube_gdata
            [thumbnail] => http://i.ytimg.com/vi/brK9xMdziac/2.jpg
        )

    [8] => Array
        (
            [url] => http://www.youtube.com/watch?v=P1jBKFKyMJA&feature=youtube_gdata
            [thumbnail] => http://i.ytimg.com/vi/P1jBKFKyMJA/2.jpg
        )

    [9] => Array
        (
            [url] => http://www.youtube.com/watch?v=ZzT69vbtPSc&feature=youtube_gdata
            [thumbnail] => http://i.ytimg.com/vi/ZzT69vbtPSc/2.jpg
        )

)

This is the main page code.

 

<?php

 

function yt($ytsearch) {

 

  global $ytcurrenttag;

  global $ytvids;

  global $ytid;

  $ytvids = array();

  $ytid=0;

 

  function ytinsidethetag($ytparser, $ytdata){

      global $ytcurrenttag;

      global $ytvids;

      global $ytid;

      if(!strcmp(strtolower($ytcurrenttag),"title")) {

        $ytvids[$ytid]['type'] = "yt";

        $ytvids[$ytid]['title']=$ytdata;

        $ytvids[$ytid]['title'][0] = ucwords($ytvids[$ytid]['title'][0]);

      }

       

  }

 

  function ytopentag($ytparser, $ytdata, $ytattr=''){

      global $ytcurrenttag;

      $ytcurrenttag = $ytdata;

      global $ytvids;

      global $ytid;

      if(!strcmp(strtolower($ytcurrenttag),"media:player")) {

        $ytvids[$ytid]['link']=str_replace("watch?v=","v/",$ytattr['URL']);

        $ytvids[$ytid]['thumb']=str_replace("http://www.youtube.com/watch?v=","http://img.youtube.com/vi/",$ytvids[$ytid]['link']);

        $ytvids[$ytid]['thumb'].="/2.jpg";

      }

  }

 

  function ytclosetag($ytparser, $ytdata){

      global $ytid;

      if(!strcmp(strtolower($ytdata),"entry"))

        $ytid++;

  }

 

  $ytxml_parser = xml_parser_create();

 

  xml_set_element_handler($ytxml_parser, "ytopentag", "ytclosetag");

 

  xml_set_character_data_handler($ytxml_parser, "ytinsidethetag");

 

 

 

  include("fetch.php");

  $ytdata = fetchrssyt("http://gdata.youtube.com/feeds/videos?vq=$ytsearch&start-index=1&max-results=40");

  if(!(xml_parse($ytxml_parser, $ytdata)))

      die("Error on line " . xml_get_current_line_number($ytxml_parser));

 

  xml_parser_free($ytxml_parser);

 

  return $ytvids;

}

 

?>

Right, so just replace that with:

 

function yt($search)
{
    $xml = new SimpleXMLElement('http://gdata.youtube.com/feeds/videos?vq=' . urlencode($search) . '&start-index=1&max-results=40', null, true);

    $xml->registerXPathNamespace('m', 'http://search.yahoo.com/mrss/');

    $videos = array();
    foreach ($xml->xpath('//m:player') as $player) {
        parse_str(parse_url($player['url'], PHP_URL_QUERY), $query);
        
        $videos[] = array(
            'link' => (string) $player['url'],
            'thumb' => 'http://i.ytimg.com/vi/' . $query['v'] . '/2.jpg',
        );
    }

    return $videos;
}

 

Then yt('test') will give you an array of links to videos and their corresponding thumbnails when searching for "test".

<?php

 

function yt($search)

{

    $xml = new SimpleXMLElement('http://gdata.youtube.com/feeds/videos?vq=' . urlencode($search) . '&start-index=1&max-results=40', null, true);

 

    $xml->registerXPathNamespace('m', 'http://search.yahoo.com/mrss/');

 

    $videos = array();

    foreach ($xml->xpath('//m:player') as $player) {

        parse_str(parse_url($player['url'], PHP_URL_QUERY), $query);

       

        $videos[] = array(

            'link' => (string) $player['url'],

            'thumb' => 'http://i.ytimg.com/vi/' . $query['v'] . '/2.jpg',

        );

    }

 

    return $videos;

}

 

?>

Do you mean the fetch php?

That is.

 

<?

 

 

function fetchrssyt($rss){

if (!function_exists('curl_init'))

die('please install cURL');

 

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $rss);

curl_setopt($curl, CURLOPT_REFERER, "http://localhost/");

curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0;Windows NT 5.1)");

curl_setopt($curl, CURLOPT_HEADER, 0);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

curl_setopt($curl, CURLOPT_TIMEOUT, 30);

$output = curl_exec($curl);

curl_close($curl);

return $output;

}

 

?>

 

 

 

or do you mean,the bottom of my code.

 

include("fetch.php");

$ytdata = fetchrssyt("http://gdata.youtube.com/feeds/videos?vq=$ytsearch&start-index=1&max-results=40");

if(!(xml_parse($ytxml_parser, $ytdata)))

die("Error on line " . xml_get_current_line_number($ytxml_parser));

 

xml_parser_free($ytxml_parser);

 

return $ytvids;

}

 

no too sure which it is,

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.