PHP http(file_get_content) GET与POST请求方式

来源:互联网 发布:国家卫生部数据查询 编辑:程序博客网 时间:2024/04/29 15:38

转自:http://blog.csdn.net/chaoping315/article/details/19013237

1.GET方式请求

[php] view plaincopy
  1. <?php  
  2.   
  3.        $data = array('sParam1'=>'test1','sParam2'=>101,'isAuto'=>1);  //定义参数  
  4.   
  5.        $data = @http_build_query($data);  //把参数转换成URL数据  
  6.   
  7.        $aContext = array('http' => array('method' => 'GET',  
  8.   
  9.                                                                  'header'  => 'Content-type: application/x-www-form-urlencoded',  
  10.   
  11.                                                                  'content' => $data ));  
  12.   
  13.        $cxContext  = stream_context_create($aContext);  
  14.   
  15.        $sUrl = 'http://www.mytest.com/test.php'; //此处必须为完整路径  
  16.   
  17.        $d = @file_get_contents($sUrl,false,$cxContext);  
  18.   
  19.        print_r($d);  
  20.   
  21. ?>  


2.POST方式请求

[python] view plaincopy
  1. <?php  
  2.   
  3.        $data = array('sParam1'=>'test1','sParam2'=>101,'isAuto'=>1);  //定义参数  
  4.   
  5.        $data = @http_build_query($data);  //把参数转换成URL数据  
  6.   
  7.        $aContext = array('http' => array('method' => 'POST',  
  8.   
  9.                                                                  'header'  => 'Content-type: application/x-www-form-urlencoded',  
  10.   
  11.                                                                  'content' => $data ));  
  12.   
  13.        $cxContext  = stream_context_create($aContext);  
  14.   
  15.        $sUrl = 'http://www.mytest.com/test.php'; //此处必须为完整路径  
  16.   
  17.        $d = @file_get_contents($sUrl,false,$cxContext);  
  18.   
  19.        print_r($d);  
  20.   
  21. ?>  

0 0
原创粉丝点击