如何处理收到的HTTP标头 [英] How to handle received HTTP Header
问题描述
我的问题是如何使用从其他网站收到的php读取标题信息。 (我正在使用patreon webhooks)
文档页面说:
My question is how to read header information with php received from other site. (I'm using patreon webhooks) The documentation page says:
当其中一个事件发生时,我们的服务器将发送HTTP POST到您指定的URL。此HTTP POST将包含来自JSON格式的用户操作的相关数据。它还有标题
X-Patreon-Event:
< trigger>
X-Patreon-Signature:
< message signature>
其中消息签名是与您的client_secret签署的(与MD5一起)的JSON POST正文HMAC
When one of these events occurs, our servers will send an HTTP POST to a URL you specify. This HTTP POST will contain the relevant data from the user action in JSON format. It will also have headers
X-Patreon-Event:
<trigger>
X-Patreon-Signature:
<message signature>
where the message signature is the JSON POST body HMAC signed (with MD5) with your client_secret
这是我的代码:
<?php
logData("asd");
$headers = getallheaders();
$X_Patreon_Event = $headers['X-Patreon-Event'];
$X_Patreon_Signature = $headers['X-Patreon-Signature'];
logMusic(json_decode($X_Patreon_Event));
logMusic(json_decode($X_Patreon_Signature));
function logData($str){
$url = '/var/www/websitelog.txt';
$current = "$str\n";
file_put_contents($url,$current,FILE_APPEND | LOCK_EX);
}
推荐答案
getallheaders
(自PHP 5.4.0起)将所有标题作为关联数组返回...
getallheaders
(since PHP 5.4.0) will return all headers as an associative array...
$headers = getallheaders();
...然后您就可以检查以获取所需的标头值
...that you'll be then able to inspect to fetch the desired headers values
$X_Patreon_Event = $headers['X-Patreon-Event'];
$X_Patreon_Signature = $headers['X-Patreon-Signature'];
附注: getallheaders ()
函数可能不可用(例如,如果您的Web服务器是nginx)。在这种情况下,您始终可以使用一小段代码重新实现该功能:从PHP获取当前请求的http标头
Side note: getallheaders()
function may not be available (for example if your web server is nginx). In that case you can always re-implement the function with a little piece of code: Get the http headers from current request in PHP
这篇关于如何处理收到的HTTP标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!