PHP用CURL转发抖音的https请求为http到其他服务器,用如下header和body作为例子阐述:
Method | POST | ||
参数形式 | 参数名 | 类型 | 说明 |
Header | x-nonce-str | string | 签名用的随机字符串 |
x-timestamp | int64 | 发送消息的毫秒级时间戳 | |
x-signature | string | 请求签名,业务方接收后需要计算和校验签名,防伪造和篡改 | |
x-roomid | string | 房间id | |
x-msg-type | string | 消息类型 live_comment: 直播间评论 live_gift: 直播间送礼 live_like: 直播间点赞 | |
content-type | string | 固定值:application/json | |
body | ~ | string | 具体类型消息payload对象的json序列化字符串 |
代码如下:
$curl = curl_init();
$x_nonce_str="";
$x_timestamp="";
$x_signature="";
$x_roomid="";
$x_msg_type="";
foreach ($_SERVER as $name => $value) {
if (strpos($name, 'HTTP_X_NONCE_STR') === 0) {
$x_nonce_str = $value;
continue;
}
if (strpos($name, 'HTTP_X_TIMESTAMP') === 0) {
$x_timestamp = $value;
continue;
}
if (strpos($name, 'HTTP_X_SIGNATURE') === 0) {
$x_signature = $value;
continue;
}
if (strpos($name, 'HTTP_X_ROOMID') === 0) {
$x_roomid = $value;
continue;
}
if (strpos($name, 'HTTP_X_MSG_TYPE') === 0) {
$x_msg_type = $value;
continue;
}
}
$body = file_get_contents('php://input');
$myheaders = array(
'x-nonce-str: '.$x_nonce_str,
'x-timestamp: '.$x_timestamp,
'x-signature: '.$x_signature,
'x-roomid: '.$x_roomid,
'x-msg-type: '.$x_msg_type,
'User-Agent: Apifox/1.0.0 (https://apifox.com)',
'content-type: application/json',
'Accept: */*',
'Host: #你的ip地址#:#你的端口#',
'Connection: keep-alive'
);
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://#你的ip地址#:#你的端口#',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => $myheaders,
));
$response = curl_exec($curl);
curl_close($curl);
//输出日志
echo $response;
//用文件保存日志
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = time() . " ip:" . getIP ();
fwrite($myfile, $txt . "\n");
fwrite($myfile, var_dump($myheaders) . "\n");
fwrite($myfile, $body . "\n");
fwrite($myfile, "response:" . var_dump($response));
fclose($myfile);