That was excellent advice and pointed me in a direction to a solution.
It turns out the the 405 error is indicating some error in the parameters.
Apparently in my case the new user password was only 7 characters not 8, simple mistake but a lot of grief. would have been nice to get the same verbose error message from curl’ing within terminal.
The other big problem was I had forgotten to set up the curl SSL certificate correctly on the local server. Hence the 401 errors.
My php routine just for completeness and anyone else passing by:
echo CallMAIB_API('GET', 'users');
// or echo CallMAIB_API('POST', 'users/add', "email=new_user@anonemail.email&password=passwordofatleast8chars");
// or echo CallMAIB_API('POST', 'users/remove', "email=new_user@anonemail.email");
//
// Note: MAIL_URL = https://example.com/mail/admin/mail/ MAIL_USER = an admin user MAIL_KEY = admin user's password
function CallMAIB_API($method='POST', $path='', $postdata='', $headers=array()) {
$qs = (!empty($postdata) && 'GET' === $method) ? '?' . ltrim($postdata, '?') : '';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, MAIL_URL . $path . $qs);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_USERPWD, MAIL_USER . ":" . MAIL_KEY);
if (!empty($postdata) && in_array($method, array('POST', 'PUT'))) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
}
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36');
// we don't want any HTTPS errors so a certificate should be installed from: http://curl.haxx.se/docs/caextract.html (and regularly updated)
// placed in the \php\ directory and the php.ini file updated in the [curl] options with "curl.cainfo=c:\php\cacert.pem"
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
// check the HTTP Status code
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (200 !== $httpCode) {
$response = "$httpCode: ";
switch ($httpCode) {
case 200: break;
case 404:
$response .= "Mail API Not found";
break;
case 401:
case 405:
$response .= "Mail API Error: Check Authentication (password length > 8?) and SSL certificate";
break;
case 500:
$response .= "Our servers replied with an error.";
break;
case 502:
$response .= "Our servers may be down or being upgraded. Hopefully they will be OK soon!";
break;
case 503:
$response .= "Mail service ; unavailable. Hopefully it will be OK soon!";
break;
default:
$response = 'Undocumented error: ' . $httpCode . ' : ' . curl_error($ch);
break;
}
}
curl_close($ch);
return $response;
}