简体   繁体   English

将值从Bash脚本传递到PHP

[英]Passing a value from Bash script to PHP

In Bash script, I call something like that 在Bash脚本中,我这样称呼

# !/bin/bash
output=$(curl -L -H "Authorization: token MY_TOKEN" https://MY_GITHUB_ENTERPRISE/api/v3/repos/USER_NAME/REPO/pulls/123/files)
echo $output

I receive a string from output. 我从输出中收到一个字符串。 I want to pass this string into php code to use json_decode function. 我想将此字符串传递给php代码以使用json_decode函数。 What should I do? 我该怎么办?

I'm going to write code in PHP part like this. 我将像这样在PHP部分中编写代码。 Is it ok? 可以吗 Sorry for the numb question because I'm a newbie in both Bash script and PHP 很抱歉出现麻木的问题,因为我是Bash脚本和PHP的新手

#!/usr/bin/php
$json = json_decode($DATA_FROM_CURL, true);
// Some logic in php

// continue with bash script
#!/usr/bin/bash
echo ABC

**Note: I have to write all codes in a Bash script file. **注意:我必须在Bash脚本文件中编写所有代码。

UPDATE 1 Thank for Felipe Valdes's suggestion, I updated my code like this. 更新1感谢Felipe Valdes的建议,我这样更新了我的代码。 However, I got the return 但是,我得到了回报

{"message":"Bad credentials","documentation_url":" https://developer.github.com/enterprise/2.8/v3 "} {“消息”:“错误的凭证”,“ documentation_url”:“ https://developer.github.com/enterprise/2.8/v3 “}

My code: 我的代码:

#!/usr/bin/php
<?php
// create curl resource 
$ch = curl_init();
$TOKEN = 'abclaldslsl';

$header = array();
$header[] = 'Content-length: 0';
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: token'.TOKEN;

// set url 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, "https://MY_GITHUB_ENTERPRISE/api/v3/repos/USER_NAME/REPO/pulls/123/files");

//return the transfer as a string 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// $output contains the output string 
$output = curl_exec($ch);

// close curl resource to free up system resources 
curl_close($ch);

print_r($output);
?>

In order to pass BASH variables from the environment to your PHP process, you may use ARGV and ARGC variables from PHP 为了将BASH变量从环境传递到PHP进程,可以使用PHP中的ARGV和ARGC变量

http://php.net/manual/en/reserved.variables.argv.php http://php.net/manual/zh/reserved.variables.argv.php

you may also pass values as a bash variable and fetch the data using getenv(): 您也可以将值作为bash变量传递,并使用getenv()获取数据:

http://php.net/manual/en/function.getenv.php http://php.net/manual/zh/function.getenv.php

in your case, you can also avoid the bash script all together and simply proceed to fetch the curl url contents directly from PHP using something like file_get_contents or the curl_* functions: 在您的情况下,您也可以完全避免使用bash脚本,而直接使用诸如file_get_contents或curl_ *函数之类的方法直接从PHP获取curl URL内容:

http://php.net/manual/en/curl.examples.php http://php.net/manual/zh/curl.examples.php

http://php.net/manual/en/function.file-get-contents.php http://php.net/manual/en/function.file-get-contents.php

Also, see this similar question: How to pass parameters from bash to php script? 另外,请参见以下类似问题: 如何将参数从bash传递到php脚本?

You forgot an $ before your TOKEN variable. 您在TOKEN变量之前忘记了$

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: 如果愿意,可以使用参数vector( argv )设置路径和令牌:

$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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM