首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在PHP中使用CURL执行PUT操作?

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

Stack Overflow用户
提问于 2013-08-30 04:04:30
回答 2查看 1.5K关注 0票数 0

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

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

municipality= NMBM sgc= 12345

我编写了下面的代码,但它输出了以下错误消息:“ExceptionMessage”:“对象引用没有设置为对象的实例”。任何帮助都是非常感谢的。谢谢!

代码语言:javascript
复制
<?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);

?>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-30 04:34:48

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

代码语言:javascript
复制
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服务器。当我把它指向他们时,这就是全部的回应:

代码语言:javascript
复制
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的任何数据进行消毒。:)

票数 0
EN

Stack Overflow用户

发布于 2013-08-30 04:33:31

试着:

代码语言:javascript
复制
curl_setopt($ch, CURLOPT_PUT, true);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18524700

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档