# Chef-server REST api in php

**URL:** <https://discourse.chef.io/t/chef-server-rest-api-in-php/1465>\
**Category:** Chef Infra (archive)\
**Created:** [July 2, 2010, 11:19am UTC](https://discourse.chef.io/t/chef-server-rest-api-in-php/1465 "2010-07-02T11:19:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vasiliy\_Tolstov](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/vasiliy_tolstov/32/103_2.png) [@Vasiliy\_Tolstov](https://discourse.chef.io/u/Vasiliy_Tolstov)\
**Post date:** [July 2, 2010, 11:19am UTC](https://discourse.chef.io/t/chef-server-rest-api-in-php/1465/1 "2010-07-02T11:19:12Z")

</div>

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](mailto:v.tolstov@selfip.ru)  
[Selfip.Ru](http://Selfip.Ru)

---

<div class="post-metadata">

**Author:** ![Vasiliy\_Tolstov](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/vasiliy_tolstov/32/103_2.png) [@Vasiliy\_Tolstov](https://discourse.chef.io/u/Vasiliy_Tolstov)\
**Post date:** [July 2, 2010, 12:37pm UTC](https://discourse.chef.io/t/chef-server-rest-api-in-php/1465/2 "2010-07-02T12:37:50Z")

</div>

В Птн, 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](mailto:v.tolstov@selfip.ru)  
[Selfip.Ru](http://Selfip.Ru)
