Skip to content
Advertisement

Running youtube-dl with php exec

I am trying to fetch video details as JSON using PHP exec()
I have youtube-dl installed on my CentOS server.
Running youtube-dl -J <VideoURL> via SSH/Terminal just works fine.
My test.php script returns empty page 🙁

echo exec("youtube-dl -J <VideoURL>");
//Installed via pip

//OR

echo exec("python /home/site/youtube-dl -J <VideoURL>");
//Downloaded as file named youtube-dl

exec is enabled if I test it like:

if(function_exists('exec')) {
    echo "exec is enabled";
}

IP of server is not blocked by YouTube as i am able to successfully run command via terminal

Advertisement

Answer

I was able to achieve what you are by doing the following in my test.php file

<?php

if(function_exists('shell_exec')) {
    header('Content-Type: application/json');
    echo shell_exec("youtube-dl -J https://www.youtube.com/watch?v=zGDzdps75ns");
}

?>
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement