How can I call API using PHP?
Posted: Wed Jan 18, 2017 6:58 am
Dear All,
I wanna call API to get all the critical alerts by API.
Here is what I have tried:
But I got nothing, Although I get the JSON data when I can the link from the browser after I put my credentials.
Can u help?
Best,
Dev
I wanna call API to get all the critical alerts by API.
Here is what I have tried:
Code: Select all
$user = 'myusername';
$pass = 'mypassword';
$url = "https://myserver/nagios/cgi-bin/statusjson.cgi?query=servicelist&details=true&servicestatus=critical";
$body = array('username' => $user, 'password' => $pass);
$body = json_encode($body );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab URL and pass it to the browser, retaining the reply
$reply = curl_exec($ch);
print_r($reply);
$content_json = json_decode($reply);
print_r($content_json);die;
Can u help?
Best,
Dev