Chef-server REST api in php


#1

I’m try to send and recive json data from chef-server REST api.
In ruby all simple, but i need to use php language (our developers loves
it).

How can i generate X-Ops-Authorization-{1…} and X-Ops-Content-Hash for
my request?

I’m already create api key with knife. I’m try to read mixlib auth, but
can’t fully understand what i need in php to sing request. (i’m try to
send GET /nodes/NODE HTTP/1.1 …


Vasiliy G Tolstov v.tolstov@selfip.ru
Selfip.Ru


#2

В Птн, 02/07/2010 в 15:19 +0400, Vasiliy G Tolstov пишет:

I’m try to send and recive json data from chef-server REST api.
In ruby all simple, but i need to use php language (our developers loves
it).

How can i generate X-Ops-Authorization-{1…} and X-Ops-Content-Hash for
my request?

I’m already create api key with knife. I’m try to read mixlib auth, but
can’t fully understand what i need in php to sing request. (i’m try to
send GET /nodes/NODE HTTP/1.1 …

Now i have this code:

<?php class chef { private $con; private $header; public function __construct($url) { $this->con = curl_init(); curl_setopt($this->con, CURLOPT_URL, $url); curl_setopt($this->con, CURLOPT_HEADER, 0); curl_setopt($this->con, CURLOPT_RETURNTRANSFER, 1); $this->header = "Accept: application/json\n"; $this->header .= "X-Ops-Userid: php\n"; $this->header .= "Connection: close\n"; $this->header .= "X-Ops-Sign: version=1.0\n"; $this->header .= "Host: chef-server:4000\n"; $this->header .= "X-Ops-Timestamp: 2010-07-02T12:13:25Z\n"; $this->header .= "X-Chef-Version: 0.9.4\n"; } public function send($string) { curl_setopt($this->con, CURLOPT_HTTPGET, 1); $crypt = ""; $key = openssl_get_privatekey("file://./key.pem"); openssl_private_encrypt(json_encode($string), $crypt, $key); $sigs = split("\n", chunk_split(base64_encode($crypt), 61)); for ($i=1; $iheader .= "X-Ops-Authorization-".$i.": ".$sigs[$i-1]."\n";} var_dump($this->header); // curl_setopt($this->con, CURLOPT_POSTFIELDS, $this->header); return (object) json_decode(curl_exec($this->con)); } } $api = new chef("http://chef-server:4000"); var_dump($api->send(array("format" => 0, "action" => "show", "id" => "work.selfip.ru", "controller" => "nodes"))); ?>

Do i need encode to json my query? What is the X-Ops-Content-Hash ?


Vasiliy G Tolstov v.tolstov@selfip.ru
Selfip.Ru