I am trying to send data over HTTP protocol using the PUT request method in PHP, but it just would not work for me.. i have tried many techniques and methods like cURL()
and file_get_contents()
with a build header but it just would not work, the response just comes with a 400 and 415 error respectivly.
this is what i am trying to access: Found at: 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"
}
]
these are my data:
$url = "https://api.netbiter.net/operation/v1/rest/json/system/$sysid/live?accesskey=$accesskey&id=$par";
$json='[{"id": "'.$par.'","value": "'.$val.'"}]';
and those are some of what i tried: 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);
i took the cURL function from Curl and PHP - how can I pass a json through curl by PUT,POST,GET, seems to work there but not for me, i get a 400 : Bad request error on this.
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));
I get a 415: Unsupported Media Type error for this.
I played around with those for a long time but nothing seems to work, the returned data should be JSON data as the API in the link provided above illustrates but nothing comes back nor does the data changes.
Also checked whether the problem is in my actual data but my data is fine and should grant access with no problem.
What am I doing wrong?
EDIT
It seems i am getting a error, which - as i found out - in many cases like this is because the JSON is not valid, but when i echo the json i am trying to send i get this:
[{"id":"theRightId","value":"1"}]
The structure is OK and nothing is wrong.. this is driving me mad now..