Azure通知中心REST API与PHP
我正在尝试用PHP为Azure通知中心API创建SharedAccessSignature 我不断收到错误“无效授权令牌签名” 有人举过在PHP5.4+中创建SAS的例子吗? 上有一些API文档,但有人说实现与文档不同 这是我失败的实现:Azure通知中心REST API与PHP,php,azure,push-notification,Php,Azure,Push Notification,我正在尝试用PHP为Azure通知中心API创建SharedAccessSignature 我不断收到错误“无效授权令牌签名” 有人举过在PHP5.4+中创建SAS的例子吗? 上有一些API文档,但有人说实现与文档不同 这是我失败的实现: private static function get_authentication_header() { $uri = "https://x.servicebus.windows.net/x"; $expiry = t
private static function get_authentication_header()
{
$uri = "https://x.servicebus.windows.net/x";
$expiry = time() + (60*60);
$string = utf8_encode(urlencode($uri) . "\n" . $expiry);
$keyname = "DefaultFullSharedAccessSignature";
$key = base64_decode(static::HUB_KEY);
$signature = base64_encode(hash_hmac('sha256', $string, $key));
$sas = 'SharedAccessSignature sig=' . $signature . '&se=' . $expiry . '&skn=' . $keyname . '&sr=' . urlencode($uri);
return $sas;
}
下面是一个简单的包装器,用于使用PHP向通知中心发送通知
<?php
include 'Notification.php';
class NotificationHub {
const API_VERSION = "?api-version=2013-10";
private $endpoint;
private $hubPath;
private $sasKeyName;
private $sasKeyValue;
function __construct($connectionString, $hubPath) {
$this->hubPath = $hubPath;
$this->parseConnectionString($connectionString);
}
private function parseConnectionString($connectionString) {
$parts = explode(";", $connectionString);
if (sizeof($parts) != 3) {
throw new Exception("Error parsing connection string: " . $connectionString);
}
foreach ($parts as $part) {
if (strpos($part, "Endpoint") === 0) {
$this->endpoint = "https" . substr($part, 11);
} else if (strpos($part, "SharedAccessKeyName") === 0) {
$this->sasKeyName = substr($part, 20);
} else if (strpos($part, "SharedAccessKey") === 0) {
$this->sasKeyValue = substr($part, 16);
}
}
}
private function generateSasToken($uri) {
$targetUri = strtolower(rawurlencode(strtolower($uri)));
$expires = time();
$expiresInMins = 60;
$expires = $expires + $expiresInMins * 60;
$toSign = $targetUri . "\n" . $expires;
$signature = rawurlencode(base64_encode(hash_hmac('sha256', $toSign, $this->sasKeyValue, TRUE)));
$token = "SharedAccessSignature sr=" . $targetUri . "&sig="
. $signature . "&se=" . $expires . "&skn=" . $this->sasKeyName;
return $token;
}
public function broadcastNotification($notification) {
$this->sendNotification($notification, "");
}
public function sendNotification($notification, $tagsOrTagExpression) {
echo $tagsOrTagExpression."<p>";
if (is_array($tagsOrTagExpression)) {
$tagExpression = implode(" || ", $tagsOrTagExpression);
} else {
$tagExpression = $tagsOrTagExpression;
}
# build uri
$uri = $this->endpoint . $this->hubPath . "/messages" . NotificationHub::API_VERSION;
echo $uri."<p>";
$ch = curl_init($uri);
if (in_array($notification->format, ["template", "apple", "gcm"])) {
$contentType = "application/json";
} else {
$contentType = "application/xml";
}
$token = $this->generateSasToken($uri);
$headers = [
'Authorization: '.$token,
'Content-Type: '.$contentType,
'ServiceBusNotification-Format: '.$notification->format
];
if ("" !== $tagExpression) {
$headers[] = 'ServiceBusNotification-Tags: '.$tagExpression;
}
# add headers for other platforms
if (is_array($notification->headers)) {
$headers = array_merge($headers, $notification->headers);
}
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $notification->payload
));
// Send the request
$response = curl_exec($ch);
// Check for errors
if($response === FALSE){
throw new Exception(curl_error($ch));
}
$info = curl_getinfo($ch);
if ($info['http_code'] <> 201) {
throw new Exception('Error sending notificaiton: '. $info['http_code'] . ' msg: ' . $response);
}
//print_r($info);
//echo $response;
}
}
?>
generateSasToken($uri);
$headers=[
“授权:”.$token,
“内容类型:”。$contentType,
“ServiceBusNotification格式:”。$notification->Format
];
如果(“!==$tagExpression){
$headers[]=“ServiceBusNotification标记:”。$tagExpression;
}
#为其他平台添加标题
if(是_数组($notification->headers)){
$headers=array\u merge($headers,$notification->headers);
}
curl_setopt_数组($ch,数组(
CURLOPT_POST=>TRUE,
CURLOPT_RETURNTRANSFER=>TRUE,
CURLOPT_SSL_VERIFYPEER=>FALSE,
CURLOPT_HTTPHEADER=>$headers,
CURLOPT_POSTFIELDS=>$notification->payload
));
//发送请求
$response=curl\u exec($ch);
//检查错误
如果($response==FALSE){
抛出新异常(curl_error($ch));
}
$info=curl\u getinfo($ch);
如果($info['http_code']201){
抛出新异常('发送通知时出错:'.$info['http_代码'.'msg:'.$response');
}
//打印(信息);
//回音$应答;
}
}
?>
我不知道你是想发送推送还是做注册管理。修改上述内容以进行注册管理并不困难,如NHRESTAPI所示(记住xml文档中的顺序!):
如果你仍然遇到问题,请告诉我
ElioHi Elio,使用您的代码,我能够成功连接到通知中心API,在提交推送通知时得到201响应。但是我的所有推送通知都不会发送,而是记录在azure控制面板中的“所有通道错误”指标下。是否也可以包含notification.php文件?可能是我在特定的头上犯了一个错误。嗨,Elio,我在通知中心API上仍然有问题。获取201响应没有问题,但不会发送任何通知。使用调试面板发送通知可以正常工作。所以这一定是我的通知负载或标题的问题。到今天为止,所有的东西似乎都使用相同的代码库,Azure通知中心出现了一些内部问题?推动iOS和Windows Phone,现在做得很好。