Azure通知中心PHP

时间:2015-03-04 13:24:12

标签: php azure notifications

我试图从PHP更新Notification HUB注册。 这里他们展示了如何从PHP发送通知: http://azure.microsoft.com/en-us/documentation/articles/notification-hubs-php-backend-how-to/

我尝试以这种方式调整代码以使用其他API方法: https://msdn.microsoft.com/en-us/library/azure/dn223262.aspx

public function updateRegistration($registrationId) {
    # build uri
    $uri = $this->endpoint . $this->hubPath . "/registrations/" . $registrationId . NotificationHub::API_VERSION;

    $ch = curl_init($uri);

    $contentType = "application/atom+xml;type=entry;charset=utf-8";

    $token = $this->generateSasToken($uri);

    $headers = [
        'Authorization: '.$token,
        'Content-Type: '.$contentType,
        'x-ms-version: 2013-08',
        'If-Match: update'
    ];

    curl_setopt_array($ch, array(
        CURLOPT_POST => TRUE,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_SSL_VERIFYPEER => FALSE,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_POSTFIELDS =>  '<?xml version="1.0" encoding="utf-8"?>
        <entry xmlns="http://www.w3.org/2005/Atom">
            <content type="application/xml">
                <WindowsRegistrationDescription xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect">
                    <Tags>myTag, myOtherTag</Tags>
                </WindowsRegistrationDescription>
            </content>
        </entry>'
    ));

    // 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  :'. $info['http_code'] . ' msg: ' . $response);
    }

} 

但是我收到了这个错误:

&#34;错误:405 msg:405指定的HTTP谓词(POST)无效。&#34;

我做错了什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

根据文档,它仅接受PUT

所以,代码应该是:

curl_setopt_array($ch, array(
    CURLOPT_CUSTOMREQUEST => 'PUT', // add this line
    CURLOPT_POST => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_SSL_VERIFYPEER => FALSE,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_POSTFIELDS =>  '<?xml version="1.0" encoding="utf-8"?>
    <entry xmlns="http://www.w3.org/2005/Atom">
        <content type="application/xml">
            <WindowsRegistrationDescription xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect">
                <Tags>myTag, myOtherTag</Tags>
            </WindowsRegistrationDescription>
        </content>
    </entry>'
));