创建注册通知中心Azure PHP
我正在尝试使用PHP中的RESTAPI NotificationHub AZURE创建注册 有人知道是怎么做的吗创建注册通知中心Azure PHP,php,azure,push-notification,azure-notificationhub,Php,Azure,Push Notification,Azure Notificationhub,我正在尝试使用PHP中的RESTAPI NotificationHub AZURE创建注册 有人知道是怎么做的吗 问候和感谢 一般来说,访问通知中心REST端点需要三个主要步骤: 解析连接字符串 生成授权令牌 执行HTTP调用 有关更多详细信息,请参阅 同时,您可以在应用程序中直接使用Azure团队提供的,它可以轻松实现您的需求 #构建uri # build uri $uri = $this->endpoint . $this->hubPath . "/registratio
问候和感谢 一般来说,访问通知中心REST端点需要三个主要步骤:
#构建uri
# build uri
$uri = $this->endpoint . $this->hubPath . "/registrations" . NotificationHub::API_NEW_VERSION;
$ch = curl_init();
$token = $this->generateSasToken($uri);
$headers = [
'Authorization: '. $token,
'Content-Type: application/xml',
'x-ms-version: 2015-01'
];
$request_body = self::requestBodyRegistration($device_type, $tagsOrTagExpression, $device_code );
if( is_null( $request_body ) )
{
return null;
}
curl_setopt_array($ch, array(
CURLOPT_URL => $uri,
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $request_body
));
// Send the request
$response = curl_exec($ch);
// Check for errors
if($response === FALSE){
throw new Exception(curl_error($ch));
}
$info = curl_getinfo($ch);
curl_close($ch);
private function requestBodyRegistration($device_type, $tagsOrTagExpression, $device_code )
{
switch ($device_type) {
case 'apple':
return '<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom">
<content type="application/xml">
<AppleRegistrationDescription xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect">
<Tags>'. $tagsOrTagExpression .'</Tags>
<DeviceToken>'. $device_code .'</DeviceToken>
</AppleRegistrationDescription>
</content>
</entry>';
case 'gcm':
return '<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom">
<content type="application/xml">
<GcmRegistrationDescription xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect">
<Tags>'. $tagsOrTagExpression .'</Tags>
<GcmRegistrationId>'. $device_code .'</GcmRegistrationId>
</GcmRegistrationDescription>
</content>
</entry>';
default:
return null;
}
}
$uri=$this->endpoint$这是一条小路。“/注册”。NotificationHub::API_新版本;
$ch=curl_init();
$token=$this->generateStoken($uri);
$headers=[
“授权:”.$token,
'内容类型:应用程序/xml',
“x-ms-version:2015-01”
];
$request\u body=self::requestBodyRegistration($device\u type,$tagsOrTagExpression,$device\u code);
if(为空($request\u body))
{
返回null;
}
curl_setopt_数组($ch,数组(
CURLOPT_URL=>$uri,
CURLOPT_POST=>TRUE,
CURLOPT_RETURNTRANSFER=>TRUE,
CURLOPT_SSL_VERIFYPEER=>FALSE,
CURLOPT_HTTPHEADER=>$headers,
CURLOPT_POSTFIELDS=>$request_body
));
//发送请求
$response=curl\u exec($ch);
//检查错误
如果($response==FALSE){
抛出新异常(curl_error($ch));
}
$info=curl\u getinfo($ch);
卷曲关闭($ch);
私有函数requestBodyRegistration($device\u type、$tagsOrTagExpression、$device\u code)
{
交换机(设备类型){
“苹果”一案:
返回'
“.$tagsOrTagExpression”
“.$device_代码。”
';
案例“gcm”:
返回'
“.$tagsOrTagExpression”
“.$device_代码。”
';
违约:
返回null;
}
}