切换到PHP7 获取不到GET,file_get_contents('php://input')获取不到Curl POST的数据

今天遇到了一个file_get_contents('php://input')获取不到curl post请求的数据的问题。

代码如下:

两个php脚本

1.php

$ch = curl_init();

$data = json_encode(array('username' => 'aaa', 'password' => 'xxx','sign'=>'dasddsadxf123dsad'));

$url = 'http://test.com/test_php/file_get_contents_post/2.php';

$ch = curl_init(); //初始化curl

curl_setopt($ch, CURLOPT_URL, $url);//设置链接

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息

curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//POST数据

curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");

$response = curl_exec($ch);//接收返回信息

if(curl_errno($ch)){//出错则显示错误信息

print curl_error($ch);

}

curl_close($ch); //关闭curl链接

echo $response;//显示返回信息

2.php

$res = file_get_contents('php://input');

var_dump($res);

返回结果为空,在1.php里打印返回结果,获取不到2.php返回的内容。请问是什么原因?有人知道吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用curl库或file_get_contents函数进行POST请求时,您可以按照以下示例传递数据: 1. 使用curl库: ```php $url = "http://192.168.100.201:5678/decrypt/"; $data = ["path" => "H:/project/河南森博建筑工程有限公司开标一览表.xls", "my_directory" => "f68dd48b10364f8b9813996ffebb7361", "new_name" => "河南森博建筑工程有限公司", "pwd" => "123456"]; $curl = curl_init($url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); $response = curl_exec($curl); curl_close($curl); // 处理响应数据 ``` 2. 使用file_get_contents函数进行POST请求的方式有所不同,需要使用`http_build_query`函数将数据转换为查询字符串,并使用`stream_context_create`函数设置上下文选项: ```php $url = "http://192.168.100.201:5678/decrypt/"; $data = ["path" => "H:/project/河南森博建筑工程有限公司开标一览表.xls", "my_directory" => "f68dd48b10364f8b9813996ffebb7361", "new_name" => "河南森博建筑工程有限公司", "pwd" => "123456"]; $options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query($data) ) ); $context = stream_context_create($options); $response = file_get_contents($url, false, $context); // 处理响应数据 ``` 请注意,这是一个示例,您需要根据实际情况修改`$url`和`$data`的值以适应您的API和数据需求。另外,确保您的服务器可以访问指定的IP地址和端口,并且API端点正确响应您的请求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值