I'm creating a CLI app in PHP and I want to open the help page in the default web browser. It's similar to what git does when you run git help <command>
where it opens the relevant URL in the default browser.
On python there is the webbrowser.open
function which takes care of it, but I couldn't find any package for PHP.
This is my approach so far but I'm not able to test it properly (i.e. Windows 7, Debian, MacOS, etc) so it is kind of hacky:
<?php
$uname = strtolower(php_uname());
$os = (strpos($uname, "darwin") !== false) ? 'osx' : ((strpos($uname, "win") !== false) ? 'win32' : 'linux');
$end = $os == 'win32' ? '' : '&';
$cmd1 = sprintf("%s $url $end", $os == 'win32' ? 'start ""' : ($os == 'osx' ? 'open' : 'xdg-open'));
pclose(popen($cmd1, "r"));
My questions are:
Is there a PHP library equivalent of python's
webbrowser
library that handles all OSes?Is there another simpler approach to accomplishing it?