我试图做使用PHP和卷曲一个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内部服务器错误。 在我与使用GET和POST相同curl_req方法等功能,一切顺利。
所以我在做什么错?
我终于解决了这个自己。 如果其他人是否有这个问题,这里是我的解决方案:
我创建了一个新的方法:
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
由于这似乎帮助一些人,这是我最后的卷曲DELETE方法,它返回JSON解码对象的HTTP响应:
/**
* @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;
}
要调用GET,POST,DELETE,把所有样的要求,我已创建了一个共同的功能
function CallAPI($method, $api, $data) {
$url = "http://localhost:82/slimdemo/RESTAPI/" . $api;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
switch ($method) {
case "GET":
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
break;
case "POST":
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
break;
case "PUT":
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
break;
case "DELETE":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
break;
}
$response = curl_exec($curl);
$data = json_decode($response);
/* Check for 404 (file not found). */
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Check the HTTP Status code
switch ($httpCode) {
case 200:
$error_status = "200: Success";
return ($data);
break;
case 404:
$error_status = "404: API Not found";
break;
case 500:
$error_status = "500: servers replied with an error.";
break;
case 502:
$error_status = "502: servers may be down or being upgraded. Hopefully they'll be OK soon!";
break;
case 503:
$error_status = "503: service unavailable. Hopefully they'll be OK soon!";
break;
default:
$error_status = "Undocumented error: " . $httpCode . " : " . curl_error($curl);
break;
}
curl_close($curl);
echo $error_status;
die;
}
调用delete方法
$data = array('id'=>$_GET['did']);
$result = CallAPI('DELETE', "DeleteCategory", $data);
CALL Post方法
$data = array('title'=>$_POST['txtcategory'],'description'=>$_POST['txtdesc']);
$result = CallAPI('POST', "InsertCategory", $data);
调用get方法
$data = array('id'=>$_GET['eid']);
$result = CallAPI('GET', "GetCategoryById", $data);
呼叫被置于方法
$data = array('id'=>$_REQUEST['eid'],m'title'=>$_REQUEST['txtcategory'],'description'=>$_REQUEST['txtdesc']);
$result = CallAPI('POST', "UpdateCategory", $data);
与WSSE身份验证我自己的类要求
class Request {
protected $_url;
protected $_username;
protected $_apiKey;
public function __construct($url, $username, $apiUserKey) {
$this->_url = $url;
$this->_username = $username;
$this->_apiKey = $apiUserKey;
}
public function getHeader() {
$nonce = uniqid();
$created = date('c');
$digest = base64_encode(sha1(base64_decode($nonce) . $created . $this->_apiKey, true));
$wsseHeader = "Authorization: WSSE profile=\"UsernameToken\"\n";
$wsseHeader .= sprintf(
'X-WSSE: UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"', $this->_username, $digest, $nonce, $created
);
return $wsseHeader;
}
public function curl_req($path, $verb=NULL, $data=array()) {
$wsseHeader[] = "Accept: application/vnd.api+json";
$wsseHeader[] = $this->getHeader();
$options = array(
CURLOPT_URL => $this->_url . $path,
CURLOPT_HTTPHEADER => $wsseHeader,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false
);
if( !empty($data) ) {
$options += array(
CURLOPT_POSTFIELDS => $data,
CURLOPT_SAFE_UPLOAD => true
);
}
if( isset($verb) ) {
$options += array(CURLOPT_CUSTOMREQUEST => $verb);
}
$ch = curl_init();
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
if(false === $result ) {
echo curl_error($ch);
}
curl_close($ch);
return $result;
}
}
$json empty
public function deleteUser($extid)
{
$path = "/rest/user/".$extid."/;token=".$this->__token;
$result = $this->curl_req($path,"**$json**","DELETE");
return $result;
}