加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Opscode Chef REST API无效的JSON

发布时间:2020-12-13 22:52:29 所属栏目:PHP教程 来源:网络整理
导读:我想通过 PHP与Chef集成 我使用库https://github.com/dv1r/php-chef与Hosted Enterprise Chef进行通信.当我从Chef检索信息时,一切都很好.我也可以删除客户端等. 当我尝试将数据发送到服务器时,问题就开始了.我总是得到错误“无效的JSON”. 我发送的JSON根据h
我想通过 PHP与Chef集成

我使用库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,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”:[“无效的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);
 }
?>

我希望它能帮助你搞清楚..

警告:

>我是开源厨师服务器(11.0.10),也许就是这样
点(即使我怀疑它)
>我使用的略有不同
不使用json_encode的常量库的版本…请看这里:
https://github.com/jenssegers/php-chef

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读