如何使用PUT方法通过cURL / PHP将JSON数据发送到API [英] how to send JSON data to an API using PUT method via cURL/PHP

查看:114
本文介绍了如何使用PUT方法通过cURL / PHP将JSON数据发送到API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用cURL / PHP连接到API。



我需要在发送JSON数据时向此API中插入一个方法。



这是我的参数
$ data = array('__ type'=>'urn:inin.com:connection:workstationSettings');



这是我如何做cURL调用

 私有函数_makeCall($ method,$ uri,$ data = false,$ header = NULL,& $ httpRespond = array())
{
$ ch = curl_init
$ url = $ this-> _baseURL。 $ uri;

if(
($ method =='POST'|| $ method =='PUT')
& $ data
){
$ jsonString = json_encode($ data);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ jsonString);
}

if($ method =='POST'){
curl_setopt($ ch,CURLOPT_POST,true);
} elseif($ method =='PUT'){
curl_setopt($ ch,CURLOPT_PUT,true);
} else {
if($ data){
$ url = sprintf(%s?%s,$ url,http_build_query($ data));
}
}

//禁用缓存连接的使用
curl_setopt($ ch,CURLOPT_FRESH_CONNECT,true);

//从API返回响应
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);

//从头返回HEADER响应
curl_setopt($ ch,CURLOPT_HEADER,true);

//添加任何标题
if(!empty($ header)){
curl_setopt($ ch,CURLOPT_HTTPHEADER,$ header);
}

//设置URL
curl_setopt($ ch,CURLOPT_URL,$ url);

//使cURL调用
$ respond = curl_exec($ ch);

// throw cURL exception
if($ respond === false){
$ errorNo = curl_errno($ ch);
$ errorMessage = curl_error($ ch);

throw new ApiException($ errorMessage,$ errorNo);
}

list($ header,$ body)= explode(\r\\\
\r\\\
,$ respond,2);

$ httpRespond = $ this-> _http_parse_headers($ header);

$ result = json_decode($ body,true);

//抛出API异常
if($ this-> _hasAPIError($ result)){
$ errorCode = 0;
if(isset($ result ['errorCode'])){
$ errorCode = $ result ['errorCode'];
}
throw new ApiException($ result ['message'],$ errorCode);
}

return $ result;问题是,每次API接收到我的PUT请求时,它都会抱怨说,那里有一个PUT请求。是我在 $ data 数组中传递的一个缺少的参数



如何输入 $ jsonString 正确?

解决方案

表现得如你所愿。请改为尝试:

  ... 
if($ method =='POST'){
curl_setopt($ ch,CURLOPT_POST,true);
} elseif($ method =='PUT'){
curl_setopt($ ch,CURLOPT_CUSTOMREQUEST,'PUT');
...

参考:在PHP中处理PUT / DELETE参数


I am trying to connect to an API using cURL/PHP.

I need to PUT a method to this API while sending JSON data.

Here is my parameter $data = array('__type' => 'urn:inin.com:connection:workstationSettings');

Here is how I am doing the cURL call

private function _makeCall($method, $uri, $data = false, $header = NULL, &$httpRespond = array())
{
    $ch = curl_init();
    $url = $this->_baseURL . $uri;

    if( 
           ($method == 'POST' || $method == 'PUT') 
        && $data
    ){
        $jsonString = json_encode( $data );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $jsonString );
    }

    if($method == 'POST'){
        curl_setopt($ch, CURLOPT_POST, true);
    } elseif( $method == 'PUT'){
        curl_setopt($ch, CURLOPT_PUT, true);
    } else {
        if ($data){
            $url = sprintf("%s?%s", $url, http_build_query($data));
        }
    }  

    //disable the use of cached connection
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);

    //return the respond from the API
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    //return the HEADER respond from the API
    curl_setopt($ch, CURLOPT_HEADER, true);

    //add any headers
    if(!empty($header)){
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }

    //set the URL
    curl_setopt($ch, CURLOPT_URL, $url);

    //make the cURL call
    $respond = curl_exec($ch);

    //throw cURL exception
    if($respond === false){
        $errorNo = curl_errno($ch);
        $errorMessage = curl_error($ch);

        throw new ApiException($errorMessage, $errorNo);
    }   

    list($header, $body) = explode("\r\n\r\n", $respond, 2);

    $httpRespond = $this->_http_parse_headers($header);

    $result = json_decode($body, true);

    //throw API exception
    if(  $this->_hasAPIError($result) ){
        $errorCode = 0;
        if(isset($result['errorCode'])){
            $errorCode = $result['errorCode'];
        }
        throw new ApiException($result['message'], $errorCode);
    }

    return $result;
}

The issue is that every time the API receive my PUT request it complains that there is a missing parameter which I am passing in my $data array

How can I PUT the $jsonString correctly?

解决方案

From what I understand, using PUT this way doesn't behave the way you would expect. Try this instead:

...
if($method == 'POST'){
    curl_setopt($ch, CURLOPT_POST, true);
} elseif( $method == 'PUT'){
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
...

Reference: Handling PUT/DELETE arguments in PHP

这篇关于如何使用PUT方法通过cURL / PHP将JSON数据发送到API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆