欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2024-01-04 12:48:40
...
PHP关于file_get_contens函数向本地文件传参数的问题

源码如下:
$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",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'=> "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-……



哦? 就是说没有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内容呢?

上一篇:

下一篇: