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

如何在PHP中使用CURL执行PUT操作?,php,web-services,rest,curl,put,Php,Web Services,Rest,Curl,Put,我想使用CURL在web服务上执行PUT操作。让我们假设: Web服务url: 市政当局=NMBM sgc=12345 我已经编写了下面的代码,但它输出了以下错误消息:“ExceptionMessage”:“对象引用未设置为对象的实例”。任何帮助都将不胜感激。谢谢 <?php function sendJSONRequest($url, $data) { $data_string = json_encode($data); $ch = curl_i

我想使用CURL在web服务上执行PUT操作。让我们假设:

Web服务url:

市政当局=NMBM sgc=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);

?>

试试:

curl_setopt($ch, CURLOPT_PUT, true);

我复制了您的代码,但对其进行了更改,使其指向本地主机上非常基本的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.mypreadmid.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)"}
HTTP/1.1500内部服务器错误
缓存控制:没有缓存
Pragma:没有缓存
内容类型:application/json;字符集=utf-8
过期:-1
服务器:Microsoft IIS/8.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
日期:2013年8月30日星期五04:30:41 GMT
连接:关闭
内容长度:867
{“Message”:“出现错误。”,“ExceptionMessage”:“对象引用未设置为对象的实例。”,“ExceptionType”:“System.NullReferenceException”,“StackTrace”:“位于MyPrepaidApi.Controller.ConsumerRegisterRequestController.Put(CRMRRegisterRequest值)在c:\\Workspace\\My预付款\\预付费自动售货系统\\PrepaidCloud\\WebApi\\Controllers\\ConsumerRegisterRequestController.cs:lambda_方法(闭包,对象,对象[])第190行\r\n\r\n在System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.c_u显示类别13.b_uC(对象实例,对象[]方法参数)\r\n位于System.Web.Http.Controller.ReflectedHttpActionDescriptor.ActionExecutor.Execute(对象实例,对象[]参数)\r\n位于System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 Func,CancellationToken CancellationToken)}
您可能需要检查API,以确保向他们传递了正确的信息。如果是的话,问题可能就在他们这边


虽然我意识到这不是你问题的一部分,而且这还在开发中,但请记住清理
$\u GET
:)中的任何数据

可能是重复的我已经尝试过使用这个,但仍然没有工作。无论如何,谢谢。:)谢谢我确实认为代码工作正常。我尝试了一种不同的编码,它仍然给我上述错误信息。也许网络服务真的出了问题。