php curl发送delte,php Curl如何发起POST DELETE GET POST请求

关于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(ContentType:application/json,ContentLength:.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);

}

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值