// define an array where we would cache the Session Token Cookie with value
$cookieArr = array();
// Sonus SBC 1000/2000 REST login resource URL
$loginResource = "https://sbc_host_or_ipaddress/rest/login";
// set the login resource url in curl
curl_setopt($curlHandle, CURLOPT_URL, $loginResource );
// setup a callback handler for reading and processing the response header fields
// that would come in response to REST login resource POST call
curl_setopt($curlHandle, CURLOPT_HEADERFUNCTION, array($this, 'responseHeaderCallback'));
// tell cURL that we are doing a HTTP POST
curl_setopt($curlHandle, CURLOPT_POST, true);
// set the login resource POST params. The user must be of the REST access level and created from the WebUI prior to using the API.
$loginPropsArr = array('Username'=>'admin', 'Password'=>'admin');
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, http_build_query($loginPropsArr, '', '&'));
// exec the HTTP/REST request
$response = curl_exec($curlHandle);
// HTTP response header callback function which processes the header fields
function responseHeaderCallback($curlHandle, $header) {
if(!strncmp($header, "Set-Cookie:", 11)) {
$cookiestr = trim(substr($header, 11, -1));
$cookie = explode(';', $cookiestr);
$cookie = explode('=', $cookie[0]);
$cookiename = trim(array_shift($cookie));
$cookieArr[$cookiename] = trim(implode('=', $cookie));
}
return strlen($header);
}
|