Discussion:
POSTing JSON API information via cURL
ZenLlama
2012-10-28 16:54:09 UTC
Permalink
ZenLlama [http://community.zenoss.org/people/ZenLlama] created the discussion

"POSTing JSON API information via cURL"

To view the discussion, visit: http://community.zenoss.org/message/69596#69596

--------------------------------------------------------------
Hi,

I'm +attempting+ to start performing some rather basic JSON API calls via PHP's cURL implementation. Being quite new to Zenoss, PHP, and JSON alike it's been a bit of a slog. I've had decent luck fetching and posting data via the Python API, but JSON has for me thus far been stubborn.

If anyone has a moment, can you provide an example of any proper basic HTTP request for the JSON information for a call in the API? An example would be fetching all devices in device class Server...I'm a bit unclear as to what the base URL should be and what format the POST data should take. Perhaps cURL isn't the best way of doing this?

Unfortunately implementing in PHP and doing so from a remote host are both necessary, as I'll be incorporating this as an extension to an existing PHP-based application.

Thanks in advance, and obviously should I solve my own problem first I'll post back. I've been through the forums, API examples, etc with precious little luck.
--------------------------------------------------------------

Reply to this message by replying to this email -or- go to the discussion on Zenoss Community
[http://community.zenoss.org/message/69596#69596]

Start a new discussion in zenoss-users by email
[discussions-community-forums-zenoss--***@community.zenoss.org] -or- at Zenoss Community
[http://community.zenoss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2003]
ZenLlama
2012-10-28 17:01:54 UTC
Permalink
ZenLlama [http://community.zenoss.org/people/ZenLlama] created the discussion

"Re: POSTing JSON API information via cURL"

To view the discussion, visit: http://community.zenoss.org/message/69597#69597

--------------------------------------------------------------
Ah, of course this is not entirely helpful if I don't note the product version. We're currently using 4.1.1 Enterprise, though an upgrade to 4.2 is planned.
--------------------------------------------------------------

Reply to this message by replying to this email -or- go to the discussion on Zenoss Community
[http://community.zenoss.org/message/69597#69597]

Start a new discussion in zenoss-users by email
[discussions-community-forums-zenoss--***@community.zenoss.org] -or- at Zenoss Community
[http://community.zenoss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2003]
ZenLlama
2012-10-28 20:39:27 UTC
Permalink
ZenLlama [http://community.zenoss.org/people/ZenLlama] created the discussion

"Re: POSTing JSON API information via cURL"

To view the discussion, visit: http://community.zenoss.org/message/69598#69598

--------------------------------------------------------------
I suppose it would be further helpful if I posted what I had so far, particularly since I've finally realized I need to POST JSON data to get any back. Anything specific (hosts, users, passwords) in this setup is redacted with <>

<?php

$cookie = "/tmp/cookie.txt";
$ch = curl_init(" https:// https://<host>/zport/acl_users/cookieAuthHelper/login");

<login to retrieve cookie>

$headers2[] = "Accept: */*";
$headers2[] = "Connection: Keep-Alive";
$headers2[] = "Content-type: application/json";

$json_data='{"uid": "/zport/dmd/Devices/Server", "limit": 100, "params": {}}';
$json_string='{"action":"DeviceRouter":"method":"getDevices":"data":"$json_data"}';

curl_setopt($ch, CURLOPT_URL, " https:// https://<host>/zport/dmd/Devices/Server");
curl_setopt($ch, CURLOPT_POSTFIELDS, "$json_string");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers2);
$devices = curl_exec($ch);
echo $devices;

curl_close($ch);
?>

I'm now getting what appears to be JSON data...but it's not really what I'm looking for. Likely missing something quite stupid.

Thanks!
--------------------------------------------------------------

Reply to this message by replying to this email -or- go to the discussion on Zenoss Community
[http://community.zenoss.org/message/69598#69598]

Start a new discussion in zenoss-users by email
[discussions-community-forums-zenoss--***@community.zenoss.org] -or- at Zenoss Community
[http://community.zenoss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2003]
Loading...