• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP关于file_get_contens函数向本地文件传参数的问题

php 搞代码 3年前 (2022-01-23) 17次浏览 已收录 0个评论
文章目录[隐藏]

PHP关于file_get_contens函数向本地文件传参数的问题

源码如下:
<?php
$str_query = “userId=admin&gameId=game1&serverId=server1”;

$context= stream_context_create(array(
‘http’=> array(
‘method’=> ‘POST’,
‘timeout’ => 10,
‘header’=> ‘User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.98 Safari/534.13\r\nContent-type: application/x-www-form-urlencoded\r\n’,
‘content’=> $str_query
)
));

echo file_get_contents(“interface.php”,true, $context);

?>

本地Aparche服务器上就两个文件。一个是放以上源码的文件。一个是interface.php文件。

我在此文件上写上如上的源码。用file_get_contents向interface.php文件POST数据。结果interface.php没有接收到数据。

不知道是在哪里出错了。求高手赐教!!!!

回复讨论(解决方案)

需要有协议类型
echo file_get_contents(” http://localhost/interface.php&#8221;,true, $context);

另外参数数组中
header 项要写成
‘header’=> “User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.98 Safari/534.13\r\nContent-type: application/x-www-form-urlencoded\r\n”,

只在双引号中 \r\n 才会生效

另外参数数组中
header 项要写成
‘header’=> “Use2本文来源gao*daima.com搞@代#码&网6

搞gaodaima代码

r-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.98 Safari/534.13\r\nContent-type: application/x-www-……

哦? 就是说没有HTTP这样就肯定不行了咯!!!

这点参数直接curl get 就是了

引用 3 楼 的回复:
另外参数数组中
header 项要写成
‘header’=> “User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.98 Safari/534.13\r\nContent-type: app……

没有互联网协议,只不过是文件系统函数。你非要猴子去飞,那是办不到的。孙悟空也只是借着菩提老祖的口诀翻翻筋斗而已

为什么会file_get_contents能获取到完整的json内容,而用curl获取却是不完整的json内容呢?


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP关于file_get_contens函数向本地文件传参数的问题

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址