Using Cisco NAC API with PHP (cURL)

The Cisco NAC Guest Server provides an API for other applications to interrogate the NAC.
I didn’t use cURL before, but now it was the best option to use since I didn’t have Zend Framework or anything else.

Off course, the first attempts always returned “false”. It never works from the first time. After some searching, this piece of code did it:

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_USERAGENT, "Dummy text");
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 
  $result = curl_exec($ch);
  $error = curl_error($ch);
 
  curl_close($ch);
 
  if ($result) {
      return $result;
  } else {
      return $error;
  }

I needed to parse the response, so the transfer must be returned as a string; therefor CURLOPT_RETURNTRANSFER is set to “true”.

The NAC server expected a User Agent, so I needed to add that option: CURLOPT_USERAGENT. It doesn’t matter what value it has, but it needs something.

Because I didn’t needed to configure any certificates clientside (in cURL) I could set CURLOPT_SSL_VERIFYPEER is set to false.

The last problem I had, was that everytime something didn’t work, I only got “false” as answer. To get an error message from cURL it is necessary to read it out from the connection with “curl_error()”. This function returns the latest error message.

To simplify things, my function returns the result as a string, or the error message.