我正在使用此方法在nginx中使apache头请求工作。
if (!function_exists('apache_request_headers')) {
function apache_request_headers() {
foreach($_SERVER as $key=>$value) {
if (substr($key,0,5)=="HTTP_") {
$key=str_replace(" ","-",ucwords(strtolower(str_replace("_"," ",substr($key,5)))));
$out[$key]=$value;
}else{
$out[$key]=$value;
}
}
return $out;
}
}
我像$headers = apache_request_headers();
那样检索标题并使用数组来保存json。
$response=array()
$response["error"] = true;
$response["message"] = $headers;
下面的代码是$ response数组变量的内容:
{
error: true
message: {
CONTENT_LENGTH: "13"
CONTENT_TYPE: "application/x-www-form-urlencoded"
DOCUMENT_ROOT: "/home4/admin/public_html"
GATEWAY_INTERFACE: "CGI/1.1"
Accept: "*/*"
Accept-Encoding: "gzip, deflate"
Accept-Language: "en-US,en;q=0.8"
Connection: "close"
Cookie: "_ga=GA1.2.1266385826.1428275832"
Host: "mysite.com"
Origin: "chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo"
User-Agent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36"
X-Apikey: "bca0de3e7c10cb6623ef00021caf9450"
X-Http-Proto: "HTTP/1.1"
X-Log-7528: "107.147.160.193"
X-Real-Ip: "107.147.160.193"
PATH: "/bin:/usr/bin"
PHPRC: "/home4/admin"
QUERY_STRING: ""
REDIRECT_STATUS: "200"
REDIRECT_UNIQUE_ID: "VVlk5sD@@rgAACwVT0AAAACM"
REDIRECT_URL: "/rmapp/v1/tasks"
REMOTE_ADDR: "107.147.160.193"
REMOTE_PORT: "31527"
REQUEST_METHOD: "POST"
REQUEST_URI: "/rmapp/v1/tasks"
SCRIPT_FILENAME: "/home4/admin/public_html/rmapp/v1/index.php"
SCRIPT_NAME: "/rmapp/v1/index.php"
SERVER_ADDR: "192.185.226.161"
SERVER_ADMIN: "webmaster@mysite.com"
SERVER_NAME: "mysite.com"
SERVER_PORT: "80"
SERVER_PROTOCOL: "HTTP/1.1"
SERVER_SIGNATURE: "<address>Apache Server at mysite.com Port 80</address> "
SERVER_SOFTWARE: "Apache"
UNIQUE_ID: "VVlk5sD@@rgAACwVT0AAAACM"
PHP_SELF: "/rmapp/v1/index.php"
REQUEST_TIME: 1431921894
argv: [0]
argc: 0
}-
}
我的问题是,我需要从$ header中抓取X-ApiKey,但使用$api_key = $headers['X-ApiKey'];
不会返回任何内容,但正如您所见,X-ApiKey
中存在$header
。有人可以告诉我我在这里失踪了吗?
答案 0 :(得分:1)
string
是explode
。首先尝试$temp = explode('X-Apikey:', $response['message']);
$temp1 = explode('X-Http-Proto:', $temp[1]);
var_dump(trim(str_replace('"', '', $temp1[0])));
然后访问它 -
string(32) "bca0de3e7c10cb6623ef00021caf9450"
<强>输出强>
{{1}}
答案 1 :(得分:1)
看起来您使用的是错误的变量名称。变量名称区分大小写。
响应返回以下内容(Apikey中的小k):
X-Apikey: "bca0de3e7c10cb6623ef00021caf9450"
当您使用大写k $api_key = $headers['X-ApiKey'];
尝试以下方法:
$api_key = $headers['X-Apikey'];