PHP PUT JSON请求

时间:2017-09-17 20:09:06

标签: php json curl

我正在尝试使用PHP中的PUT请求方法通过HTTP协议发送数据,但它对我不起作用..我尝试了许多技术和方法,如cURL()file_get_contents()一个构建标题,但它只是不起作用,响应只是分别有400和415错误。

这是我想要访问的内容: 发现于:http://apidocs.netbiter.net/?page=methods&show=writeSystemValues

PUT https://api.netbiter.net/operation/v1/rest/json/system/003011FB1234/live?accesskey=1234567890ABCDEFCGHIJ
Content-type: application/json
Data:
[
    {
        "id": "967.0.1111",
        "value": "32"
    },
    {
        "id": "967.0.1112",
        "value": "0"
    }
]

这些是我的数据:

$url = "https://api.netbiter.net/operation/v1/rest/json/system/$sysid/live?accesskey=$accesskey&id=$par";
$json='[{"id": "'.$par.'","value": "'.$val.'"}]';

这些是我尝试过的一些内容: cURL:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($json)));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS,$json);
curl_setopt($ch, CURLOPT_PUT, 1 );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo $response = curl_exec($ch);
curl_close($ch);

我从Curl and PHP - how can I pass a json through curl by PUT,POST,GET获取了cURL函数,似乎在那里工作但不适合我,我得到400:错误请求错误。

的file_get_contents:

$opts = array('http' =>
        array(
            'method'  => 'PUT',
            'header'  => array(
                    'Content-type: application/json',
                    'Content-length: ' . strlen($json) . "\r\n"
                    ),
            'Accept'  => 'application/json',
            'content' => $json
        )
    );  
$result = file_get_contents($url, false, stream_context_create($opts));

我收到415:不支持的媒体类型错误。

我玩了很长时间但似乎什么都没有用,返回的数据应该是JSON数据,因为上面提供的链接中的API说明但没有任何回复,数据也没有变化。

还检查问题是否在我的实际数据中,但我的数据很好,并且应该准许访问没有问题。

我做错了什么?

修改

我似乎得到了一个错误 - 正如我发现的那样 - 在很多情况下这样做是因为JSON无效,但是当我回应json我试图发送时我得到了这个:

[{"id":"theRightId","value":"1"}]

结构还可以,没有什么不对。现在这让我很生气..

0 个答案:

没有答案
相关问题