abcd
abcd

Reputation: 451

How to get the contents of a streaming with php

How to get the contents of a streaming with php.

ex:

http://www.youtube.com/get_video_info?&video_id=Wd9WOxJXQq4

I try to get the content, but it comes much data missing.

How do I read the entire file, return all data from the URL above?

function curlGet($URL) {
 if ($stream = fopen($URL, 'r')) {
    echo '<pre>';
        echo urldecode(urldecode(urldecode(stream_get_contents($stream))));
    fclose($stream);
    echo '</pre>';
    }
}  

echo curlGet('http://www.youtube.com/get_video_info?&video_id=Wd9WOxJXQq4');

Upvotes: 4

Views: 1233

Answers (1)

Daniel W.
Daniel W.

Reputation: 32350

Why do you triple-urldecode?

The URL you showed is not a stream, it's plaintext.

Use the JSON API, which is easier to handle:

http://gdata.youtube.com/feeds/api/videos/Wd9WOxJXQq4?v=2&alt=json

<?php
$json = json_decode(trim(file_get_contents(
    'http://gdata.youtube.com/feeds/api/videos/Wd9WOxJXQq4?v=2&alt=json'
)));

print_r($json);

To "restream" youtube contents, you'd then use the appropriate Url attribute from the json object and go on with fgets(), and flush it in chunks to the browser using ob_flush(); flush();.

Upvotes: 3

Related Questions