My own class request with wsse authentication
class Request {
protected $_url;
protected $_username;
protected $_apiKey;
public function __construct($url, $username, $apiUserKey) {
$this->_url = $url;
$this->_username = $username;
$this->_apiKey = $apiUserKey;
}
public function getHeader() {
$nonce = uniqid();
$created = date('c');
$digest = base64_encode(sha1(base64_decode($nonce) . $created . $this->_apiKey, true));
$wsseHeader = "Authorization: WSSE profile=\"UsernameToken\"\n";
$wsseHeader .= sprintf(
'X-WSSE: UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"', $this->_username, $digest, $nonce, $created
);
return $wsseHeader;
}
public function curl_req($path, $verb=NULL, $data=array()) {
$wsseHeader[] = "Accept: application/vnd.api+json";
$wsseHeader[] = $this->getHeader();
$options = array(
CURLOPT_URL => $this->_url . $path,
CURLOPT_HTTPHEADER => $wsseHeader,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false
);
if( !empty($data) ) {
$options += array(
CURLOPT_POSTFIELDS => $data,
CURLOPT_SAFE_UPLOAD => true
);
}
if( isset($verb) ) {
$options += array(CURLOPT_CUSTOMREQUEST => $verb);
}
$ch = curl_init();
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
if(false === $result ) {
echo curl_error($ch);
}
curl_close($ch);
return $result;
}
}