php分别模拟发送GET和POST请求,非常实用的额,也可作PHP CURL入门级的理解教材的,示例代码如下:
<?php /* ** php分别模拟发送GET与POST请求 ** ** php.hhsy.cc 2012-07-28 23:02:07 */ function httpRequest($url,$method,$params=array()){ if(trim($url)==''||!in_array($method,array('get','post'))||!is_array($params)){ return false; } $curl=curl_init(); curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); curl_setopt($curl,CURLOPT_HEADER,0 ) ; switch($method){ case 'get': $str='?'; foreach($params as $k=>$v){ $str.=$k.'='.$v.'&'; } $str=substr($str,0,-1); $url.=$str;//$url=$url.$str; curl_setopt($curl,CURLOPT_URL,$url); break; case 'post': curl_setopt($curl,CURLOPT_URL,$url); curl_setopt($curl,CURLOPT_POST,1 ); curl_setopt($curl,CURLOPT_POSTFIELDS,$params); break; default: $result=''; break; } if(isset($result)){ $result=curl_exec($curl); } curl_close($curl); return $result; }