PHP CURL DELETE请求 [英] PHP CURL DELETE request

查看:698
本文介绍了PHP CURL DELETE请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用PHP和cURL执行DELETE http请求。



我已经阅读了如何做到很多地方,但似乎没有什么工作。



这是我怎么做的:

  public function curl_req ($ path,$ json,$ req)
{
$ ch = curl_init($ this-> __ url。$ path);
$ data = json_encode($ json);
curl_setopt($ ch,CURLOPT_CUSTOMREQUEST,$ req);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ data);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ ch,CURLOPT_HTTPHEADER,array('Content-Type:application / json','Content-Length:'。strlen($ data)))
$ result = curl_exec($ ch);
$ result = json_decode($ result);
return $ result;
}

然后我继续使用我的函数:

  public function deleteUser($ extid)
{
$ path =/rest/user/\".$extid.\"/; token =。$ this-> __ token;
$ result = $ this-> curl_req($ path,,DELETE);
return $ result;

}

这给我HTTP内部服务器ERROR。
在我的其他函数使用相同的curl_req方法与GET和POST,一切顺利。



那么我做错了什么?

解决方案

这个我自己。如果任何人有这个问题,这里是我的解决方案:



我创建了一个新的方法:

  public function curl_del($ path)
{

$ url = $ this-> __ url。$ path;
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_CUSTOMREQUEST,DELETE);
$ result = curl_exec($ ch);
$ httpCode = curl_getinfo($ ch,CURLINFO_HTTP_CODE);
curl_close($ ch);

return $ result;
}

更新2 b

由于这似乎有助于一些人,这里是我的最终curl删除方法,它返回json解码对象中的HTTP响应:

  / ** 
* @desc使用cURL执行DELETE请求
*
* @param string $ path在URL fx之后的路径。 / user / login
* @param array $ json如果你需要发送一些json的请求。对我来说,删除请求总是空白
* @return Obj $ result来自REST接口的HTTP响应,JSON解码。
* /
public function curl_del($ path,$ json ='')
{
$ url = $ this-> __ url。$ path;
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_CUSTOMREQUEST,DELETE);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ json);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
$ result = curl_exec($ ch);
$ result = json_decode($ result);
curl_close($ ch);

return $ result;
}


I'm trying to do a DELETE http request using PHP and cURL.

I have read how to do it many places, but nothing seems to work for me.

This is how I do it:

public function curl_req($path,$json,$req)
{
    $ch = curl_init($this->__url.$path);
    $data = json_encode($json);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $req);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));
    $result = curl_exec($ch);
    $result = json_decode($result);
    return $result;
}

I then go ahead and use my function:

public function deleteUser($extid)
{
    $path = "/rest/user/".$extid."/;token=".$this->__token;
    $result = $this->curl_req($path,"","DELETE");
    return $result;

}

This gives me HTTP internal server ERROR. In my other functions using the same curl_req method with GET and POST, everything goes well.

So what am I doing wrong?

解决方案

I finally solved this myself. If anyone else is having this problem, here is my solution:

I created a new method:

public function curl_del($path)
{

    $url =$this->__url.$path;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
    $result = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return $result;
}

Update 2

Since this seems to help some people, here is my final curl delete method, which returns the HTTP response in json decoded object:

  /**
 * @desc    Do a DELETE request with cURL
 *
 * @param   string $path   path that goes after the URL fx. "/user/login"
 * @param   array  $json   If you need to send some json with your request. For me delete requests are always blank
 * @return  Obj    $result HTTP response from REST interface in JSON decoded.
 */
public function curl_del($path, $json='')
{
    $url =$this->__url.$path;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    $result = json_decode($result);
    curl_close($ch);

    return $result;
}

这篇关于PHP CURL DELETE请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆