POST 请求一切正常,但找不到使用 PUT 或 DELETE 的方法?
NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] init];
[mutableRequest setURL:[NSURL URLWithString:[NSString
stringWithFormat:@"http://abc.com/update/27"]]];
[mutableRequest setHTTPMethod:@"PUT"];
[mutableRequest addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[mutableRequest addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[mutableRequest addValue:@"PUT" forHTTPHeaderField:@"X-HTTP-Method-Override"];
[mutableRequest addValue:sessionId forHTTPHeaderField:@"ZURMO_SESSION_ID"];
[mutableRequest addValue:token forHTTPHeaderField:@"ZURMO_TOKEN"];
[mutableRequest addValue:@"REST" forHTTPHeaderField:@"ZURMO_API_REQUEST_TYPE"];
NSString *postLength = [NSString stringWithFormat:@"%d",[postdata3 length]];
[mutableRequest addValue:postLength forHTTPHeaderField:@"Content-Length"];
[mutableRequest setHTTPBody:postdata3]
这就是服务器端 (PHP) 使用 PUT、GET、POST 和 DELETE 处理 api 调用的方式。任何帮助将不胜感激谢谢
public static function createApiCall($url, $method, $headers, $data = array())
{
if ($method == 'PUT')
{
$headers[] = 'X-HTTP-Method-Override: PUT'; //also tried this one to add header
}
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
switch($method)
{
case 'GET':
break;
case 'POST':
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($data));
break;
case 'PUT':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($data));
break;
case 'DELETE':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
}
$response = curl_exec($handle);
return $response;
}