How to pass parameters from bash to php script?
Call it as:
php /path/to/script/script.php -- 'id=19&url=http://bkjbezjnkelnkz.com'
Also, modify your PHP script to use parse_str():
parse_str($argv[1]);
If the index $_SERVER['REMOTE_ADDR']
isn't set.
More advanced handling may need getopt(), but parse_str() is a quick'n'dirty way to get it working.
Passing a variable from BASH to PHP
You say in a comment that you are using php5-cgi
to run the script.
php5-cgi
is the CGI version of the interpreter; its purpose is to be used by the web server.
If you want to run the script on the command line then you have to use the CLI version of the interpreter. It's name is php
(or, possibly, php5
on your system).
The CLI and CGI versions of the interpreter handle some things differently. The content of the variables $argc
and $argv
is one of these differences.
The CGI version populates them only if the register_argc_argv
option is set to On
(or 1
) in php.ini
. For performance reasons, this option is usually Off
for the CGI.
On the other hand, the CLI version populates $argc
and $argv
variables with the parameters received in the command line no matter the value of register_argc_argv
option.
As @andlrc also notes in their answer, you should enclose $1
in double quotes when you construct the command line in the shell script, to prevent the shell splitting it into words:
php /var/www/html/wave/waveform.php "$1"
If, for example, the value of $1
is foo bar
(two words), your original usage renders to php waveform.php foo bar
and print_r($argv)
displays:
Array
(
[0] => waveform.php
[1] => foo
[2] => bar
)
On the other hand, if you enclose $1
in quotes, the command line becomes php waveform.php "foo bar"
and the shell passes foo bar
as a single argument to the PHP script. A print_r($argv)
outputs:
Array
(
[0] => waveform.php
[1] => foo bar
)
As a side note, there is a missing {
in copy($mp3, "$tmpname}_0.mp3");
. It should probably read "copy($mp3, "${tmpname}_0.mp3");
Pass PHP variables to a Bash script and then launch it
Note: I didn't do all the settings, just enough I hope you get the idea.
Option 1: Passing parameters
PHP:
$file = escapeshellarg($file);
$filename = escapeshellarg($filename);
// escape the others
$output = exec("./bashscript $file $filename $tags $private $password");
Bash:
#!/bin/bash
filename=$1
file=$2
description="Test of the api with a simple model"
token_api="ff00ff"
title="Uber Glasses"
tags=$3
private=$4
password=$5
...
Option 2: Using environment variables
PHP:
putenv("FILENAME=$filename");
putenv("FILE=$file");
putenv("TAGS=$tags");
putenv("PRIVATE=$private");
putenv("PASSWORD=$PASSWORD");
$output = exec('./bash_script');
Bash:
filename=$FILENAME
file=$FILE
description="Test of the api with a simple model"
token_api="ff00ff"
title="Uber Glasses"
tags=$TAGS
private=$PRIVATE
password=$PASSWORD
...
Pass a variable to a PHP script running from the command line
The ?type=daily
argument (ending up in the $_GET
array) is only valid for web-accessed pages.
You'll need to call it like php myfile.php daily
and retrieve that argument from the $argv
array (which would be $argv[1]
, since $argv[0]
would be myfile.php
).
If the page is used as a webpage as well, there are two options you could consider. Either accessing it with a shell script and Wget, and call that from cron:
#!/bin/sh
wget http://location.to/myfile.php?type=daily
Or check in the PHP file whether it's called from the command line or not:
if (defined('STDIN')) {
$type = $argv[1];
} else {
$type = $_GET['type'];
}
(Note: You'll probably need/want to check if $argv
actually contains enough variables and such)
Passing a value from Bash script to PHP
You forgot an $
before your TOKEN
variable.
Your script could look something like this:
#!/usr/bin/php
<?php
// create curl resource
$ch = curl_init();
$host = 'https://MY_GITHUB_ENTERPRISE'; // your host
$path = "/api/v3/repos/USER_NAME/REPO/pulls/123/files"; // your path
$access_token = 'abclaldslsl'; // your access token
// set url
curl_setopt($ch, CURLOPT_URL, $host . $path);
curl_setopt($ch, CURLOPT_HTTPHEADER,
[
'Accept: application/json', // inform the server you want a json response
'Authorization: token '. $access_token, // set the access token
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// check for errors
if(!$response = curl_exec($ch))
{
die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}
print_r(json_decode($output, true));
You can use the argument vector (argv
) to set your path and token if you wish:
$path = $argv[1] ?? "/api/v3/repos/USER_NAME/REPO/pulls/123/files";
$access_token = $argv[2] ?? 'abclaldslsl';
That would allow you do override the default path and access token which would allow to user your command like this:
$ myscript.php /api/v3/repos/USER_NAME/REPO/pulls/123/files MY_ACCESS_TOKEN
Pass in a text file as an argument from Bash to php
If you want to pass the file content via STDIN, you can use the following syntax on the shell:
php /path/to/php/job.php < log.txt
Or, if log.txt is the result of some other operation, you can pipe it directly to the script:
echo "$some100linestring" | php /path/to/php/job.php
Passing Bash Arguments to PHP File with shell_exec
You can use $argv
to get the command line parameters:
$term = $argv[1];
And run:
php file.php argument
Related Topics
I18N with Gettext But Without the Locale Hassle
Can PHP Read Text from a Powerpoint File
Iterating Through a Stdclass Object in PHP
Convert Hex to Ascii Characters
Sparql Query to Get All Parent of a Node
Woocommerce - Overriding Billing State and Post Code on Existing Checkout Fields
How to Iterate by Row Through a MySQL Query in PHP
Sort Array by Value Alphabetically PHP
How to Upload Image PHP and Insert Path in MySQL
How to Check If File Is Ascii or Binary in PHP
PHP Email - How to Avoid Mail Ending Up in Spam Box
How to Download Blob Based File from MySQL Database in PHP
Saving Div Contents or Canvas as Image
How to Get Results from the Wikipedia API with PHP
How to Determine the Extension(S) Associated with a Mime Type in PHP