gpt4 book ai didi

php - Opscode Chef REST API JSON 无效

转载 作者:可可西里 更新时间:2023-10-31 23:40:34 25 4
gpt4 key购买 nike

我正在尝试通过 PHPChef 集成

我使用了库 https://github.com/dv1r/php-chefHosted Enterprise Chef 通信。当我从 Chef 检索信息时,一切正常。我还可以删除客户端等。

当我尝试向服务器发送数据时,问题就出现了。我总是收到错误“无效的 JSON”。根据 http://jsonlint.com/,我发送的 JSON 是有效的.

有人知道我是否需要向 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
)

最佳答案

$res->testtt 将是您的主要问题。

“魔法”转换在某种程度上是错误的。转换到数组,你将最安全地完成所做的事情

即:

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);
}
?>

希望能帮到你..

警告:

  1. 我在一个开源 Chef 服务器 (11.0.10) 上,也许这就是点(即使我对此表示怀疑)
  2. 我使用的是稍微不同的不使用 json_encode 常量的库版本...请参见此处: https://github.com/jenssegers/php-chef

关于php - Opscode Chef REST API JSON 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23456790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com