0

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

网络服务网址:http ://stageapi.myprepaid.co.za/api/ConsumerRegisterRequest/cac52674-1711-e311-b4a8-00155d4905d3

市政府= 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);

?>
4

2 回答 2

0

我复制了您的代码,但对其进行了更改,使其指向我本地主机上的一个非常基本的 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. :)

于 2013-08-30T04:34:48.440 回答
0

尝试:

curl_setopt($ch, CURLOPT_PUT, true);
于 2013-08-30T04:33:31.887 回答