user3501407
user3501407

Reputation: 457

How to check youtube video works from a php function

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');

?>

Upvotes: 2

Views: 574

Answers (1)

user1978142
user1978142

Reputation: 7948

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;

Upvotes: 2

Related Questions