关于POST,DELETE,GET,POST请求
get:是用来取得数据。其要传递过的信息是拼在url后面,因为其功能使然,有长度的限制
post:是用来上传数据。要上传的数据放在request的head里。没有长度限制。主要是用于增加操作
put:也是用来上传数据。但是一般是用在具体的资源上。主要用于修改操作
delete:用来删除某一具体的资源上。
以下为每种请求方式的写法,实例可用。
当然也可以将四种请求合并为一个通过的类调用,实际运用过程中,根据自己的实际需求调整
public function _curl_get($url, $data=array(), $header = array(), $timeout = 30)
{
$url = u r l . ? . h t t p b u i l d q u e r y ( url.?.http_build_query( url.?.httpbuildquery(data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 0);
i n f o = c u r l e x e c ( info = curl_exec( info=curlexec(ch);
curl_close($ch);
return $info;
}
public function _curl_post($url, $data=array(), $header=array(), $timeout=30)
{
$ch = curl_init(); //初始化
curl_setopt($ch, CURLOPT_URL, $url); //设置链接
curl_setopt($ch, CURLOPT_POST, 1); //设置post方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //设置传输数据
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //设置返回信息
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
i n f o = c u r l e x e c ( info = curl_exec( info=curlexec(ch); //接收返回信息
$errno = curl_errno( $ch ); //返回错误代码
$post_info = curl_getinfo( $ch ); //提交详情
curl_close($ch);
return $info;
}
public function _curl_put($url, $data=array(), $header=array(), $timeout=30)
{
$ch = curl_init(); //初始化
curl_setopt($ch, CURLOPT_URL, $url); //设置链接
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, “PUT”); //设置PUT方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //设置传输数据
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //设置返回信息
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
i n f o = c u r l e x e c ( info = curl_exec( info=curlexec(ch); //接收返回信息
$errno = curl_errno( $ch ); //返回错误代码
$post_info = curl_getinfo( $ch ); //提交详情
curl_close($ch);
return $info;
}
public function _curl_delete($url, $data=array(), $header=array(), $timeout=30)
{
$url = u r l . ? . h t t p b u i l d q u e r y ( url.?.http_build_query( url.?.httpbuildquery(data);
$ch = curl_init(); //初始化
curl_setopt($ch, CURLOPT_URL, $url); //设置链接
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “DELETE”); //设置DELETE方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //设置传输数据
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //设置返回信息
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
i n f o = c u r l e x e c ( info = curl_exec( info=curlexec(ch); //接收返回信息
$errno = curl_errno( $ch ); //返回错误代码
$post_info = curl_getinfo( $ch ); //提交详情
curl_close($ch);
return $info;
}
#调用时根据数据类型,可以自己定义header,也可增加其它参数,放入header数组
if($data_type==json)
{
d a t a = j s o n e n c o d e ( data=json_encode( data=jsonencode(data);
h e a d e r = a r r a y ( C o n t e n t − T y p e : a p p l i c a t i o n / j s o n , C o n t e n t − L e n g t h : . s t r l e n ( header=array(Content-Type: application/json,Content-Length:.strlen( header=array(Content−Type:application/json,Content−Length:.strlen(data),token:.$token);
}else
{
h e a d e r = a r r a y ( t o k e n : . header=array(token:. header=array(token:.token);
}