我正尝试通过Chef
PHP
进行整合
我使用库https://github.com/dv1r/php-chef与Hosted Enterprise Chef
进行通信。当我从Chef
检索信息时,一切都很好。我也可以删除客户等。
当我尝试将数据发送到服务器时,问题就开始了。我总是得到错误"无效的JSON"。 我发送的JSON根据http://jsonlint.com/有效。
是否有人知道我是否需要向json_encode()
添加和编码类型才能解决此问题?
代码示例:
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());
}
图书馆的有趣部分:
// 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);
如果我错过了一些重要信息,请发表评论,我会补充一下。
谢谢。
编辑:更多调试信息 -
原始回复:
首先调用API(GET)raw_response: {"name":"name","id":"dev"}
第二次通话(PUT)raw_response: {"error":["invalid JSON"]}
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
)
答案 0 :(得分:1)
$ res-&gt; testtt将是您的主要问题。
魔法&#39;转换在某种程度上是错误的。 转换为阵列,你做的最安全
即:
try{
// Gets current data in Data-Bad `evns` Item `dev` (works)
$res = (array) $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());
}
希望它会有所帮助。
编辑工作示例
<?php
$envs = unserialize($_POST['envs']);
foreach($_POST["datas"] as $env => $items) {
ksort($items,SORT_NATURAL);
$old = (array) $chef->get("/data/livraisons/".$env);
$new = array_merge($old,$items);
ksort($new,SORT_NATURAL);
$chef->put("/data/livraisons/".$env,$new);
}
?>
我希望它能帮助你解决这个问题。
警告: