如何在PHP中使用CURL执行PUT操作


How to perform a PUT operation using CURL in PHP?

我想使用CURL对Web服务执行PUT操作。让我们假设:

Web服务url:http://stageapi.myprepaid.co.za/api/ConsumerRegisterRequest/cac52674-1711-e311-b4a8-00155d4905d3

直辖市=NMBMsgc=12345

我已经写了下面的代码,但它输出了以下错误消息:"ExceptionMessage":"对象引用没有设置为对象的实例。"。如果有任何帮助,我们将不胜感激。谢谢

<?php
function sendJSONRequest($url, $data)
{             
    $data_string = json_encode($data);
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);               
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);  
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                       
        'Content-Type: application/json',
        'Accept: application/json',
        'X-MP-Version: 10072013')                      
    );
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    ob_start();
    $result = curl_exec($ch);
    $info = curl_getinfo($ch);
    if ($result === false || $info['http_code'] == 400) {
      return $result;
    } else {
      return $result;
    }
    ob_end_clean();
    curl_close($ch);
}
$mun = $_GET['municipality'];
$sgc = $_GET['sgc'];
$req = $_GET['req']; //cac52674-1711-e311-b4a8-00155d4905d3
//myPrepaid PUT URL
echo $mpurl = "http://stageapi.myprepaid.co.za/api/ConsumerRegisterRequest/$req";
// Set Variables
$data = array("Municipality" => "$mun", "SGC" => "$sgc");
//Get Response
echo $response = sendJSONRequest($mpurl, $data);
?>

我复制了您的代码,但对其进行了更改,使其指向本地主机上的一个非常基本的HTTP服务器。您的代码工作正常,并发出以下请求:

PUT /api/ConsumerRegisterRequest/cac52674-1711-e311-b4a8-00155d4905d3 HTTP/1.1
Host: localhost:9420
Content-Type: application/json
Accept: application/json
X-MP-Version: 10072013
Content-Length: 37
{"Municipality":"NMBM","SGC":"12345"}

您收到的错误消息来自stageapi.myprepaid.co.za服务器。当我把它指向他们时,这是完整的回应:

HTTP/1.1 500 Internal Server Error
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 30 Aug 2013 04:30:41 GMT
Connection: close
Content-Length: 867
{"Message":"An error has occurred.","ExceptionMessage":"Object reference not set to an instance of an object.","ExceptionType":"System.NullReferenceException","StackTrace":"   at MyPrepaidApi.Controllers.ConsumerRegisterRequestController.Put(CrmRegisterRequest value) in c:''Workspace''MyPrepaid''Prepaid Vending System''PrepaidCloud''WebApi''Controllers''ConsumerRegisterRequestController.cs:line 190'r'n   at lambda_method(Closure , Object , Object[] )'r'n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object instance, Object[] methodParameters)'r'n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)'r'n   at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)"}

您可能需要查看API,以确保您向他们传递了正确的信息。如果你是,问题可能就在他们这边。

虽然我意识到这不是你问题的一部分,而且它正在开发中,但请记住清除$_GET中的任何数据。:)

尝试使用:

curl_setopt($ch, CURLOPT_PUT, true);