0

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..

bakz
  • 79
  • 15
  • Try using json_encode on an actual php-array, rather than trying to setup your json as a string: `$json='[{"id": "'.$par.'","value": "'.$val.'"}]';` could be `$json = json_encode( array('id'=>$par,'value'=>$val) );` – admcfajn Sep 17 '17 at 20:14
  • @admcfajn I did try that, and just to be sure i just tried it again, still not working.,. – bakz Sep 17 '17 at 20:17
  • Possible duplicate of [Curl and PHP - how can I pass a json through curl by PUT,POST,GET](https://stackoverflow.com/questions/21271140/curl-and-php-how-can-i-pass-a-json-through-curl-by-put-post-get) – Anis Alibegić Sep 17 '17 at 20:42
  • @Spectarion Mate i took the cURL call from there, it is still not working with me.. I get a 400:bad request error for the cURL and a 415:Unsupported Media Type error for the file_get_contents(), I even mentioned that page in my post. i rechecked my data numerous times but seems fine! – bakz Sep 17 '17 at 21:05
  • Please check [405 error](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405) – kip Sep 17 '17 at 21:16
  • @kip I no longer get that, i just get 400 for the cURL and 415 for the file_get_contents()... it just does not make since.. I don't know if the problem is with my setup or the environment, i am using php 5.2.6 – bakz Sep 17 '17 at 23:06

0 Answers0