【发布时间】:2018-02-27 17:17:03
【问题描述】:
在 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
我从输出中收到一个字符串。我想将此字符串传递给 php 代码以使用 json_decode 函数。我该怎么办?
我将像这样在 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
**注意:我必须在一个 Bash 脚本文件中编写所有代码。
更新 1 感谢 Felipe Valdes 的建议,我像这样更新了我的代码。但是,我得到了回报
{"message":"Bad credentials","documentation_url":"https://developer.github.com/enterprise/2.8/v3"}
我的代码:
#!/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);
?>
【问题讨论】: