2

I want to check if a YouTube video is working or not, I have tried to do that with PHP but it is not working. Can someone please help me solve this problem.

Here is my php code

<?php

function vaild( $header )
{
    $headers = get_headers($header);

    switch($headers[0]) {
    case '200':
    // video valid
    return $header = 'video valid';
    break;

    case '403':
    // private video
    return $header =  'private video';
    break;

    case '404':
    // video not found
    return $header =  'video not found';
    break;

    default:
    // nothing  above
    return $header =  'nothing  above';
    break;
    }

}

echo vaild ('http://gdata.youtube.com/feeds/api/videos/qEDEyLjIEHE');

?>
Karl-Henrik
  • 1,113
  • 1
  • 11
  • 17
user3501407
  • 447
  • 6
  • 21
  • http://stackoverflow.com/q/1383073/1853133 http://stackoverflow.com/q/1362345/1853133 Possible duplicate. – Prateek Jun 18 '14 at 04:20

1 Answers1

2

You are already on the right track. Your just have to parse the string which contains the response. Consider this example:

$url = 'http://gdata.youtube.com/feeds/api/videos/qEDEyLjIEHE';

function valid($url) {
    $header = 'Cannot be determined';
    $check = get_headers($url);
    $code = (int) substr($check[0], 9, 3);
    // rules
    switch($code) {
        case 403:
            $header = 'Private Video';
        break;

        // etc your other rules
    }


    return $header;
}

echo valid($url);

Without using functions:

$check = get_headers('http://gdata.youtube.com/feeds/api/videos/qEDEyLjIEHE');
$code = (int) substr($check[0], 9, 3);
// rules
switch($code) {
    case 200:
        $header = 'video valid';
    break;

    case 403:
        $header = 'Private Video';
    break;

    default:
        $header = 'Cannot be determined';
    break;

    // etc your other rules
}

echo $header;
user1978142
  • 7,946
  • 3
  • 17
  • 20