Create Registration Notification Hub Azure PHP

旧时模样 提交于 2019-12-13 09:24:18

问题


I'm trying to create registration using REST API NotificationHub AZURE from PHP API Microsoft

¿Anyone know how it's done?

Regards and thanks!


回答1:


Generally speaking, there are 3 main steps required to access Notification Hubs REST endpoints:

  1. Parse the connection string
  2. Generate the authorization token
  3. Perform the HTTP call

You can refer to https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-php-backend-how-to/ for more detail.

Meanwhile, you can directly use these PHP sample provided by Azure Team in your application, which can implement your requirements easily.




回答2:


# 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;
    }
}


来源:https://stackoverflow.com/questions/35941283/create-registration-notification-hub-azure-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!