How to generate download link from any Youtube videos in PHP?


I'm working on a web application that allow users to post any Youtube videos to their profile or pages they manages. This is the code that I use to post Youtube video to my profile

 $videoUpload = $fb->post('/me/videos', ['description' => 'example description', 'file_url' => ',ei,expire,id,initcwndbps,ip,ipbits,itag,lmt,mime,mm,mn,ms,mv,pl,ratebypass,requiressl,source&key=cms1&mime=video%2Fmp4&dur=301.952&fvip=2&ipbits=0&c=WEB&expire=1519648901&ratebypass=yes&ei=JayTWtSaDcWqoQORionoCg&source=youtube&redirect_counter=1&cm2rm=sn-jvbxuxavoapox-2oil7e&fexp=23712580&req_id=21384f45d53ba3ee&cms_redirect=yes&mm=29&mn=sn-npoeene7&ms=rdu&mt=1519627218&mv=m'], $access_token);
 $videoUpload = $videoUpload->getGraphNode()->asArray();

The problem is that Facebook not allow to post Youtube link directly, so I need to generate a downloadable link from that video in order to post to Facebook. Here is the PHP code that use to generate a download link from a Youtube video


$id = 'RllJtOw0USI'; 

if (isset($_GET["id"]))
    $id = $_GET["id"];

parse_str(file_get_contents(''.$id), $video_data);

$streams = $video_data['url_encoded_fmt_stream_map'];
$streams = explode(',',$streams);
$counter = 1;

foreach ($streams as $streamdata) {
    printf("Stream %d:<br/>

It's only work with RllJtOw0USI if I change the $id to dMK_npDG12Q it's not working anymore, and I got the errors as follow:


How can I get the link from this kind of video?



------<br/><br/>", $counter); parse_str($streamdata,$streamdata); foreach ($streamdata as $key => $value) { if ($key == "url") { $value = urldecode($value); printf("<strong>%s:</strong> <a href='%s'>video link</a><br/>", $key, $value); } else { printf("<strong>%s:</strong> %s<br/>", $key, $value); } } $counter = $counter+1; printf("<br/><br/>"); } ?>|||status=fail&errorcode=150&errordetail=0&



If you look at reason in json, you can see that

This video contains content from Vevo. It is restricted from playback on certain sites or applications

So you should obey their restrictions, or find any other link which is not restricted yet.

People are also looking for solutions to the problem: php - Changing the location of the User Table in Laravel 5.6


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.