I am trying to integrate with Chef
via PHP
I used the library https://github.com/dv1r/php-chef to communicate with the Hosted Enterprise Chef
. When I retrieve information from Chef
, everything is fine. I can also delete clients and such.
The problem starts when I try to send data to the server. I always get the error "Invalid JSON". The JSON that I am sending is valid according to http://jsonlint.com/ .
Does anybody know if i need to add and encoding type to the json_encode()
inorder to fix this?
Code example:
try{
// Gets current data in Data-Bad `evns` Item `dev` (works)
$res = $this->chef->get('/data/envs/dev');
} catch (Exception $e){
echo("Exception: ".$e->getMessage());
}
// Alter Data
$res->testtt = "testess";
try{
// Set's new data to Data-bag `envs` Item `dev` (FAILS)
$ret = $this->chef->put("/data/envs/dev",$res);
} catch (Exception $e){
die("Exception: <br>".$e->getMessage());
}
The interesting part of the library:
// json encode data
if ($data && !is_string($data)) {
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
$this->debug("data encoded to json: {$data}");
}
// sign the request
$this->sign($endpoint, $method, $data, $header);
$this->debug("request URL: {$url}");
// initiate curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
// most people are using self-signed certs for chef, so its easiest to just
// disable ssl verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
// add data to post and put requests
if ($method == 'POST' || $method == 'PUT')
{
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
// execute
$raw_response = curl_exec($ch);
If I missed some critical information, please comment and I will add.
Thank you.
EDIT: More debug info -
Raw Responses:
First call to the API (GET) raw_response: {"name":"name","id":"dev"}
Second call (PUT) raw_response: {"error":["invalid JSON"]}
Output of curl_getinfo($ch) [PUT]:
Array
(
[url] => https://api.opscode.com/organizations/MY_ORG/data/envs/dev
[content_type] => text/html
[http_code] => 400
[header_size] => 426
[request_size] => 1665
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 1
[total_time] => 0.175739
[namelookup_time] => 2.0E-5
[connect_time] => 0.02709
[pretransfer_time] => 0.093261
[size_upload] => 0
[size_download] => 26
[speed_download] => 147
[speed_upload] => 0
[download_content_length] => 26
[upload_content_length] => 0
[starttransfer_time] => 0.126115
[redirect_time] => 0.049605
[certinfo] => Array()
[primary_ip] => 184.106.28.81
[primary_port] => 443
[local_ip] => xxx.xxx.xxx.50
[local_port] => 33329
[redirect_url] =>
[request_header] => PUT /organizations/MY_ORG/data/envs/dev HTTP/1.1
Host: api.opscode.com
Accept: application/json
Content-Type: application/json
X-Chef-Version: 11.8.2
X-Ops-Sign: algorithm=sha1;version=1.0
X-Ops-UserId: USER
X-Ops-Timestamp: 2014-05-07T13:39:55Z
X-Ops-Content-Hash: qk8fSIReFrOMJ+Wk2y8yoe3EAgk=
X-Ops-Authorization-1: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
X-Ops-Authorization-2: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
X-Ops-Authorization-3: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
X-Ops-Authorization-4: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
X-Ops-Authorization-5: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
X-Ops-Authorization-6: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)