转自:http://blog.youkuaiyun.com/chaoping315/article/details/19013237
1.GET方式请求
- <?php
- $data=array('sParam1'=>'test1','sParam2'=>101,'isAuto'=>1);//定义参数
- $data=@http_build_query($data);//把参数转换成URL数据
- $aContext=array('http'=>array('method'=>'GET',
- 'header'=>'Content-type:application/x-www-form-urlencoded',
- 'content'=>$data));
- $cxContext=stream_context_create($aContext);
- $sUrl='http://www.mytest.com/test.php';//此处必须为完整路径
- $d=@file_get_contents($sUrl,false,$cxContext);
- print_r($d);
- ?>
2.POST方式请求
- <?php
- $data=array('sParam1'=>'test1','sParam2'=>101,'isAuto'=>1);//定义参数
- $data=@http_build_query($data);//把参数转换成URL数据
- $aContext=array('http'=>array('method'=>'POST',
- 'header'=>'Content-type:application/x-www-form-urlencoded',
- 'content'=>$data));
- $cxContext=stream_context_create($aContext);
- $sUrl='http://www.mytest.com/test.php';//此处必须为完整路径
- $d=@file_get_contents($sUrl,false,$cxContext);
- print_r($d);
- ?>
PHP GET POST 请求示例
本文介绍了使用 PHP 通过 GET 和 POST 方法发送 HTTP 请求的方法。示例代码展示了如何构造请求参数,并利用 stream_context_create 实现文件获取内容函数 file_get_contents 的自定义上下文设置。
393

被折叠的 条评论
为什么被折叠?



