php – Opscode Chef REST API无效的JSON

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Opscode Chef REST API无效的JSON脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我想通过 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

脚本宝典总结

以上是脚本宝典为你收集整理的php – Opscode Chef REST API无效的JSON全部内容,希望文章能够帮你解决php – Opscode Chef REST API无效的JSON所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。