PHP用CURL转发抖音的https请求为http到其他服务器

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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值