1

In one of scripts I use daily I have something like this:

<?php

$args = explode("\n", $argv[1]);

// loop through args
foreach ($args as $arg) {
    
    // create the url
    $url = 'https://trends.google.pl/trends/explore?date=now%207-d&geo=PL&q=,' . urlencode(trim($arg));

    // open the url in the default browser
    shell_exec('open ' . escapeshellarg($url));
}

?>

How to change this part

// open the url in the default browser
    shell_exec('open ' . escapeshellarg($url));

to open the URL in chrome browser? Because for now it opens it in default browser and I don't want to change the default browser to chrome in my os (macos)

Michal K
  • 69
  • 1
  • 7
  • Does this answer your question? [PHP page redirect](https://stackoverflow.com/questions/2112373/php-page-redirect) – kiner_shah Jan 08 '22 at 10:16
  • @kiner_shah i think want to open browser with that url using the CLI – Jerson Jan 08 '22 at 10:20
  • Which OS? Also, have you already researched what commands you can use on your CLI to open a browser? – kiner_shah Jan 08 '22 at 10:21
  • macos, I have no idea what cli is. – Michal K Jan 08 '22 at 10:22
  • @MichalK what's the problem it won't open? or something error? did you try to debug and check the url contain using var_dump or print? – Jerson Jan 08 '22 at 10:26
  • there is no problem at all with this code, it just opens the URL in default browser. and I would like to open it in CHROME – Michal K Jan 08 '22 at 10:27
  • 2
    No need to set Chrome as the default (unless you want it as default all the time). Just call chrome specifically instead of just "open". Something like: `shell_exec('chrome ' . escapeshellarg($url));` (not sure what the executable is called on MacOS) – M. Eriksson Jan 08 '22 at 10:29
  • 1
    shell_exec('open -a "Google Chrome" '.escapeshellarg($url)); ? – Jerson Jan 08 '22 at 10:30
  • @Jerson it works! Thanks! You can add a standalone reply and I will accept it if you want to. – Michal K Jan 08 '22 at 11:07

1 Answers1

2

You can use -a flags and the application name

shell_exec('open -a "Google Chrome" '.escapeshellarg($url));
Jerson
  • 1,700
  • 2
  • 10
  • 14