1

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?

supersan
  • 5,085
  • 2
  • 39
  • 61
  • "start $url" in Windows ... have you tried it? I thought it would be "explorer $url" .... – Kevin_Kinsey Oct 10 '17 at 17:47
  • @Kevin_Kinsey yes tried it (it's also in the solution above), unfortunately it's not cross platform compatible. – supersan Oct 10 '17 at 18:41
  • My point was that "start $url" didn't even work on Windows here; on my system you have to call "explorer $url". 'Nix may be your real downfall though ... what if X isn't even running? – Kevin_Kinsey Oct 11 '17 at 17:04
  • hi, it's `start "" $url`.. there are double quotes after start for it to work. If X isn't running then a browser like `lynx` can do the job. – supersan Oct 12 '17 at 08:53

1 Answers1

1

Here are two small libraries that offer this functionality:

It looks to me like the latter is more fully featured.

pjcdawkins
  • 1,146
  • 6
  • 8