Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Azure通知中心-401 MissingToken:未找到授权标头_Php_Azure_Push Notification_Notifications - Fatal编程技术网

Php Azure通知中心-401 MissingToken:未找到授权标头

Php Azure通知中心-401 MissingToken:未找到授权标头,php,azure,push-notification,notifications,Php,Azure,Push Notification,Notifications,我遇到了一点问题,我正在尝试为PHP提供一个通知中心REST包装器。我按照说明插入了正确的凭据,但仍然收到一条错误消息: “致命错误:未捕获的异常”“异常”“带有消息”“错误” 发送通知:401消息: 401MissingToken:授权头 不是 发现..跟踪ID:7392381b-7e4b-4ced-9090-6f3f8beac79d_G19,时间戳:2017年2月23日 下午5:44:36'in/“PATH”/class.NotificationHub.php:127 堆栈跟踪:#0/“PA

我遇到了一点问题,我正在尝试为PHP提供一个通知中心REST包装器。我按照说明插入了正确的凭据,但仍然收到一条错误消息:

“致命错误:未捕获的异常”“异常”“带有消息”“错误” 发送通知:401消息: 401MissingToken:授权头 不是 发现..跟踪ID:7392381b-7e4b-4ced-9090-6f3f8beac79d_G19,时间戳:2017年2月23日 下午5:44:36'in/“PATH”/class.NotificationHub.php:127 堆栈跟踪:#0/“PATH”/testingOfNotification.php(21): NotificationHub->sendNotification(对象(通知),NULL)#1 {main}在第127行的/PATH“/class.NotificationHub.php中抛出

我遵循的指南是

这是我的密码:

require_once("class.NotificationHub.php");

    $hub = new NotificationHub("Endpoint=sb://XXXX.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=XXXX", "XXXX");

$message = '{"data":{"message":"Hello from PHP!"}}';

$notification = new Notification("gcm", $message);

$hub->sendNotification($notification, null);
class.Notification.php

class Notification {
    public $format;
    public $payload;
    public $headers;

    function __construct($format, $payload) {
        if (!in_array($format, ["template", "apple", "windows", "gcm", "windowsphone"])) {
            throw new Exception('Invalid format: ' . $format);
        }
        $this->format = $format;
        $this->payload = $payload;
    }
}
class.NotificationHub

include 'class.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);
        }
    }
}

public 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"; //;charset=utf-8
    } else {
        $contentType = "application/xml";
    }
    $token = $this->generateSasToken($uri);

    //printArr($token, "token"); die();

    $headers = [
        'Authorization: '.$token,
        'Content-Type: '.$contentType,
        'ServiceBusNotification-Format: '.$notification->format
    ];

    printArr($headers);

    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_CUSTOMREQUEST => 'PUT',
        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;
}
}
包括'class.Notification.php';
类通知中心{
const API_VERSION=“?API VERSION=2013-10”;
私营部门;
私人道路;;
私人$sasKeyName;
私人$sasKeyValue;
函数构造($connectionString,$hubPath){
$this->hubPath=$hubPath;
$this->parseConnectionString($connectionString);
}
私有函数parseConnectionString($connectionString){
$parts=分解(“;”,$connectionString);
如果(sizeof($parts)!=3){
抛出新异常(“分析连接字符串时出错:.$connectionString”);
}
foreach($parts作为$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);
}
}
}
公共函数generateStoken($uri){
$targetUri=strtolower(rawurlencode(strtolower($uri));
$expires=time();
$expiresInMins=60;
$expires=$expires+$expiresInMins*60;
$toSign=$targetUri。“\n”。$expires;
$signature=rawurlencode(base64_编码(hash_hmac('sha256',$toSign,$this->sasKeyValue,TRUE));
$token=“SharedAccessSignature sr=”.$targetUri.&sig=”
.$signature.&se=“.$expires.”&skn=“.$this->sasKeyName;
返回$token;
}
公共功能广播通知($notification){
$this->sendNotification($notification,“”);
}
公共函数sendNotification($notification,$tagsOrTagExpression){
echo$tagsOrTagExpression.“”;
if(是_数组($tagsOrTagExpression)){
$tagExpression=内爆(“| |,$tagsOrTagExpression”);
}否则{
$tagExpression=$tagsOrTagExpression;
}
#构建uri
$uri=$this->endpoint.$this->hubPath./messages.NotificationHub::API_版本;
echo$uri。“”;
$ch=curl_init($uri);
如果(在数组中($notification->format,[“template”,“apple”,“gcm”])){
$contentType=“application/json”;//;charset=utf-8
}否则{
$contentType=“应用程序/xml”;
}
$token=$this->generateStoken($uri);
//printArr($token,“token”);die();
$headers=[
“授权:”.$token,
“内容类型:”。$contentType,
“ServiceBusNotification格式:”。$notification->Format
];
printArr($headers);
如果(“!==$tagExpression){
$headers[]=“ServiceBusNotification标记:”。$tagExpression;
}
#为其他平台添加标题
if(是_数组($notification->headers)){
$headers=array\u merge($headers,$notification->headers);
}
curl_setopt_数组($ch,数组(
//CURLOPT_CUSTOMREQUEST=>“PUT”,
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');
}
//打印(信息);
//回音$应答;
}
}

所以我的问题是我是不是做错了?我知道从Azure通知中心到我的Android应用程序的连接正在工作,因为从Azure发送的测试通知将显示在我的设备上。

错误表明:
未找到授权标头。
。但是头已经在代码中传递了。因此,请仔细检查您发布的代码是否与服务器上运行的代码完全相同

我还在我的网站上测试了它,效果很好

以下是我的截图:

感谢您的回复:)昨天在我发布问题之前,我在Azure中创建了一个新的名称空间和通知中心,我尝试了一下,但得到了相同的结果。但现在它不知怎么起作用了,我真的不知道为什么。但是非常感谢你抽出时间-克里斯蒂安