1

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

Sukhchain Singh
  • 834
  • 1
  • 8
  • 26
  • If you want PHP to return you the console output of your command, use `shell_exec` instead of `exec` – Bitz Nov 05 '19 at 19:23
  • @Bitz i've tried both, no result. – Sukhchain Singh Nov 05 '19 at 19:27
  • You could be hitting an error, which are hidden by default. https://stackoverflow.com/questions/9476233/why-does-php-sometimes-return-a-blank-page-without-an-error I tried what you are trying to achieve and got it working properly, I will provide the code inside my test.php in an Answer below. – Bitz Nov 05 '19 at 19:43

1 Answers1

1

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");
}

?>
Bitz
  • 1,128
  • 11
  • 33
  • I have tested this before, nothing worked. I don't know how this is working fine. Thank you. – Sukhchain Singh Nov 05 '19 at 19:51
  • The only noteworthy difference I can see between the code you provided and mine are the tags around the code, but if those were not included, the contents of the file would have been output in plain text. Very strange. Glad you got it working. :) – Bitz Nov 05 '19 at 19:53
  • No, they were there before. I am also confused. It is even working with `exec()` as of now. – Sukhchain Singh Nov 05 '19 at 19:56