<?php
/*
** php分别模拟发送GET与POST请求
*/
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;
}