1

I am developing an integration class in PHP to access SCOM data using the new REST API interface. I cannot find additional documentation to connect to the API using PHP. What I have so far is this:

$url = "http://scomserver/OperationsManager/authenticate";
$username = 'myuser';
$password = 'myPassw0rd';
$request = "";
$cookie = tempnam("/tmp", "CURLCOOKIE");
$requestHeaders = array('Content-Type, application/json; charset=utf-8');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, implode("\r\n", $requestHeaders));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, count($request));
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, -1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); 
curl_setopt($ch, CURLOPT_TIMEOUT, 400);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

$buffer = curl_exec($ch);
$response = curl_getinfo( $ch );
curl_close($ch);

With this code I am getting the following error:

{"errorMessage":"Passed parameter cannot be null","errorTrace":""}

And IIS logs report 400 error.

How can I fix this? Is there something I am missing?

Thanks,
Enzo

1 Answers1

1

You're missing request body. When use NTLM authentication, request body shoud notify SCOM API that you're doing that. For this, submit "Windows" as base-64 encoded JSON body, which is empty in your code. Refer to this example (it's in PowerShell, but should be readable): https://community.squaredup.com/answers/question/scom-1801-rest-api/.

In other words, make not changes to your PHP code, but:

$request = "Windows";
$request = utf8_encode($request);
$request = "\"".base64_encode($request)."\""; // should give you ["V2luZG93cw=="]
Max
  • 751
  • 6
  • 10
  • Thank you for your response, Max. I've tried several changes to this code. If I remove NTLM authentication and pass it in the body, I receive a 401 error: unauthorized, wrong username and password. I've also tried with several different data in the body, but no luck. Always the same error. The PowerShell code that you mentioned works just fine. But I suppose that there must be something else that must be done for PHP and CURL. – Enzo Medina Mar 08 '19 at 17:06
  • Hi thanks for testing. Actually, I gave it another careful read, and my bad... Edited the answer to fix the mistake. – Max Mar 09 '19 at 21:58
  • Great! It worked! Just a little change: replace plus sign with dot to concatenate strings. Thank you very much. – Enzo Medina Mar 10 '19 at 00:24
  • Thanks, edited the answer for the correct concatenation operator. P.S. I don't know PHP at all )) – Max Mar 10 '19 at 20:18