2011-12-12 98 views
2

我在BlackBerry开发者支持论坛上使用来自http://supportforums.blackberry.com/t5/BlackBerry-Push-Development/Simplified-BIS-Push-client-sample/ta-p/693857的客户端代码。服务器端问题:PHP推送消息给黑莓

我能够从BlackBerry推送服务器获得注册响应,但是我无法从BlackBerry推送服务器获取任何推送消息。服务器端代码使用PHP构建。当我按一下按钮发送,我得到这个错误

---------------error--------------------------------------------- 
    An XML parser error has occured 
    Invalid document end 
    Response 
---------------PHP code------------------------------------------------------------------ 
    <?php 
    if ($_POST) { 
     // APP ID provided by RIM 
     $appid = 'xxxxxxxxxx'; 
     // Password provided by RIM 
     $password = 'xxxxxxxxxxxxxxxx'; 

     //Deliver before timestamp 
     $deliverbefore = gmdate('Y-m-d\TH:i:s\Z', strtotime('+5 minutes')); 

     //An array of address must be in PIN format or "push_all" 
     $addresstosendto[] = 'push_all'; 

     $addresses = ''; 
     foreach ($addresstosendto as $value) { 
      $addresses .= '<address address-value="' . $value . '"/>'; 
     } 

     // create a new cURL resource 
     $err = false; 
     $ch = curl_init(); 
     $messageid = microtime(true); 

     $data = '--mPsbVQo0a68eIL3OAxnm'. "\r\n" . 
     'Content-Type: application/xml; charset=UTF-8' . "\r\n\r\n" . 
     '<?xml version="1.0"?> 
     <!DOCTYPE pap PUBLIC "-//WAPFORUM//DTD PAP 2.1//EN" "http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd"> 
     <pap> 
     <push-message push-id="' . $messageid . '" deliver-before-timestamp="' . $deliverbefore . '" source-reference="' . $appid . '">' 
     . $addresses . 
     '<quality-of-service delivery-method="unconfirmed"/> 
     </push-message> 
     </pap>' . "\r\n" . 
     '--mPsbVQo0a68eIL3OAxnm' . "\r\n" . 
     'Content-Type: text/plain' . "\r\n" . 
     'Push-Message-ID: ' . $messageid . "\r\n\r\n" . 
     stripslashes($_POST['message']) . "\r\n" . 
     '--mPsbVQo0a68eIL3OAxnm--' . "\n\r"; 

     // set URL and other appropriate options 
     curl_setopt($ch, CURLOPT_URL, "https://pushapi.eval.blackberry.com/mss/PD_pushRequest"); 
     curl_setopt($ch, CURLOPT_HEADER, false); 
     curl_setopt($ch, CURLOPT_USERAGENT, "Hallgren Networks BB Push Server/1.0"); 
     curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 
     curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
     curl_setopt($ch, CURLOPT_USERPWD, $appid . ':' . $password); 
     curl_setopt($ch, CURLOPT_POST, 1); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/related; boundary=mPsbVQo0a68eIL3OAxnm; type=application/xml", "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2", "Connection: keep-alive")); 

     // grab URL and pass it to the browser 
     $xmldata = curl_exec($ch); 

     // close cURL resource, and free up system resources 
     curl_close($ch); 

     //Start parsing response into XML data that we can read and output 
     $p = xml_parser_create(); 
     xml_parse_into_struct($p, $xmldata, $vals); 
     $errorcode = xml_get_error_code($p); 
     if ($errorcode > 0) { 
      $err = true; 
     } 
     xml_parser_free($p); 

     echo 'Our PUSH-ID: ' . $messageid . "<br \>\n"; 
     if (!$err && $vals[1]['tag'] == 'PUSH-RESPONSE') { 
      echo 'PUSH-ID: ' . $vals[1]['attributes']['PUSH-ID'] . "<br \>\n"; 
      echo 'REPLY-TIME: ' . $vals[1]['attributes']['REPLY-TIME'] . "<br \>\n"; 
      echo 'Response CODE: ' . $vals[2]['attributes']['CODE'] . "<br \>\n"; 
      echo 'Response DESC: ' . $vals[2]['attributes']['DESC'] . "<br \> \n"; 
     } elseif ($err) { 
      echo '<p>An XML parser error has occured</p>' . "\n"; 
      echo '<pre>' . xml_error_string($errorcode) ."</pre>\n"; 
      echo '<p>Response</p>' . "\n"; 
      echo '<pre>' . $xmldata . '</pre>' . "\n" 
     } else { 
      echo '<p>An error has occured</p>' . "\n"; 
      echo 'Error CODE: ' . $vals[1]['attributes']['CODE'] . "<br \>\n"; 
      echo 'Error DESC: ' . $vals[1]['attributes']['DESC'] . "<br \>\n"; 
     } 
    } else { 
     showhtml(); 
    } 

    function showhtml() { 
     ?> 
    <html> 
    <head><title>Blackberry PUSH Bishes!!!</title></head> 
    <body> 
    <form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>"> 
    <p>Message to push:</p> 
    <textarea name="message" rows="30" cols="50"></textarea> 
    <p></p><input type="submit" value="Push Data"></p> 
    </form> 
    </body> 
    </html> 
    <?php 
    } 
    ?> 
+0

只是说明未来求解时,' “\ n \ r”'行不出现成为问题。 – sarnold

回答

5

我能够通过添加以下行之前curl_exec将邮件推到我的黑莓。

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

下面是完整的代码:

<?php 
if ($_POST) { 
    // APP ID provided by RIM 

    $appid = 'xxxxxxxxxx'; 
    // Password provided by RIM 
    $password = 'xxxxxxxxxxxxxxxx'; 

    //Deliver before timestamp 
    $deliverbefore = gmdate('Y-m-d\TH:i:s\Z', strtotime('+5 minutes')); 

    //An array of address must be in PIN format or "push_all" 
    $addresstosendto[] = 'push_all'; 

    $addresses = ''; 
    foreach ($addresstosendto as $value) { 
     $addresses .= '<address address-value="' . $value . '"/>'; 
    } 

    // create a new cURL resource 
    $err = false; 
    $ch = curl_init(); 
    $messageid = microtime(true); 

    $data = '--mPsbVQo0a68eIL3OAxnm'. "\r\n" . 
    'Content-Type: application/xml; charset=UTF-8' . "\r\n\r\n" . 
    '<?xml version="1.0"?> 
    <!DOCTYPE pap PUBLIC "-//WAPFORUM//DTD PAP 2.1//EN" "http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd"> 
    <pap> 
    <push-message push-id="' . $messageid . '" deliver-before-timestamp="' . $deliverbefore . '" source-reference="' . $appid . '">' 
    . $addresses . 
    '<quality-of-service delivery-method="unconfirmed"/> 
    </push-message> 
    </pap>' . "\r\n" . 
    '--mPsbVQo0a68eIL3OAxnm' . "\r\n" . 
    'Content-Type: text/plain' . "\r\n" . 
    'Push-Message-ID: ' . $messageid . "\r\n\r\n" . 
    stripslashes($_POST['message']) . "\r\n" . 
    '--mPsbVQo0a68eIL3OAxnm--' . "\n\r"; 

    // set URL and other appropriate options 
    curl_setopt($ch, CURLOPT_URL, "https://pushapi.eval.blackberry.com/mss/PD_pushRequest"); 
    curl_setopt($ch, CURLOPT_HEADER, false); 
    curl_setopt($ch, CURLOPT_USERAGENT, "Hallgren Networks BB Push Server/1.0"); 
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
    curl_setopt($ch, CURLOPT_USERPWD, $appid . ':' . $password); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/related; boundary=mPsbVQo0a68eIL3OAxnm; type=application/xml", "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2", "Connection: keep-alive")); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

    // grab URL and pass it to the browser 
    $xmldata = curl_exec($ch); 

    // close cURL resource, and free up system resources 
    curl_close($ch); 

    //Start parsing response into XML data that we can read and output 
    $p = xml_parser_create(); 
    xml_parse_into_struct($p, $xmldata, $vals); 
    $errorcode = xml_get_error_code($p); 
    if ($errorcode > 0) { 
     $err = true; 
    } 
    xml_parser_free($p); 

    echo 'Our PUSH-ID: ' . $messageid . "<br \>\n"; 
    if (!$err && $vals[1]['tag'] == 'PUSH-RESPONSE') { 
     echo 'PUSH-ID: ' . $vals[1]['attributes']['PUSH-ID'] . "<br \>\n"; 
     echo 'REPLY-TIME: ' . $vals[1]['attributes']['REPLY-TIME'] . "<br \>\n"; 
     echo 'Response CODE: ' . $vals[2]['attributes']['CODE'] . "<br \>\n"; 
     echo 'Response DESC: ' . $vals[2]['attributes']['DESC'] . "<br \> \n"; 
    } elseif ($err) { 
     echo '<p>An XML parser error has occured</p>' . "\n"; 
     echo '<pre>' . xml_error_string($errorcode) ."</pre>\n"; 
     echo '<p>Response</p>' . "\n"; 
     echo '<pre>' . $xmldata . '</pre>' . "\n"; 
    } else { 
     echo '<p>An error has occured</p>' . "\n"; 
     echo 'Error CODE: ' . $vals[1]['attributes']['CODE'] . "<br \>\n"; 
     echo 'Error DESC: ' . $vals[1]['attributes']['DESC'] . "<br \>\n"; 
    } 
} else { 
    showhtml(); 
} 

function showhtml() { 
    ?> 
<html> 
<head><title>Blackberry PUSH Bishes!!!</title></head> 
<body> 
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>"> 
<p>Message to push:</p> 
<textarea name="message" rows="30" cols="50"></textarea> 
<p></p><input type="submit" value="Push Data"></p> 
</form> 
</body> 
</html> 
<?php 
} 
?> 
+0

我没有得到相同的错误。但是我没有收到我的设备上的任何通知。我在运行代码时获得此答案我们的PUSH-ID:xxxxxxxxxxx PUSH-ID:xxxxxxxxxx REPLY-TIME:2011-12-20T07:32:28Z 响应代码:1001 响应DESC:请求已被接受处理。 – mobileDeveloper

+0

您的样本推送启用应用程序当时是否在运行? – Hari

+0

不,我注册“请求注册失败。由于java.io.IOExcetion:网络操作[Subscribe]失败导致请求异常请确保Content Provider URL可用” – mobileDeveloper

1

试试这个代码

<?php 
ini_set('display_errors','1'); 
error_reporting(E_ALL); 

// APP ID provided by RIM 
$appid = ''; 
// Password provided by RIM 
$password = ''; 

//Deliver before timestamp 
$deliverbefore = gmdate('Y-m-d\TH:i:s\Z', strtotime('+15 minutes')); 

//An array of address must be in PIN format or "push_all" 
$addresstosendto[] = 'push_all'; 

$addresses = ''; 
foreach ($addresstosendto as $value) { 
$addresses .= '<address address-value="' . $value . '"/>'; 
} 

// create a new cURL resource 
$err = false; 
$ch = curl_init(); 
$messageid = microtime(true); 

$data = '--mPsbVQo0a68eIL3OAxnm'. "\r\n" . 
'Content-Type: application/xml; charset=UTF-8' . "\r\n\r\n" . 
'<?xml version="1.0"?> 
<!DOCTYPE pap PUBLIC "-//WAPFORUM//DTD PAP 2.1//EN" "http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd"> 
<pap> 
<push-message push-id="' . $messageid . '" deliver-before-timestamp="' . $deliverbefore . '" source-reference="' . $appid . '">' 
. $addresses . 
'<quality-of-service delivery-method="confirmed"/> 
</push-message> 
</pap>' . "\r\n" . 
'--mPsbVQo0a68eIL3OAxnm' . "\r\n" . 
'Content-Type: text/plain' . "\r\n" . 
'Push-Message-ID: ' . $messageid . "\r\n\r\n" . 
stripslashes('This is my message') . "\r\n" . 
'--mPsbVQo0a68eIL3OAxnm--' . "\n\r"; 

// set URL and other appropriate options 
curl_setopt($ch, CURLOPT_URL, "https://pushapi.eval.blackberry.com/mss/PD_pushRequest"); 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_USERAGENT, "Hallgren Networks BB Push Server/1.0"); 
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
curl_setopt($ch, CURLOPT_USERPWD, $appid . ':' . $password); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/related; boundary=mPsbVQo0a68eIL3OAxnm; type=application/xml", "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2", "Connection: keep-alive")); 

// grab URL and pass it to the browser 
echo $xmldata = curl_exec($ch); 

// close cURL resource, and free up system resources 
curl_close($ch); 

//Start parsing response into XML data that we can read and output 
$p = xml_parser_create(); 
xml_parse_into_struct($p, $xmldata, $vals); 
$errorcode = xml_get_error_code($p); 
if ($errorcode > 0) { 
echo xml_error_string($errorcode); 
$err = true; 
} 
xml_parser_free($p); 

echo 'Our PUSH-ID: ' . $messageid . "<br \>\n"; 
if (!$err && $vals[1]['tag'] == 'PUSH-RESPONSE') { 
echo 'PUSH-ID: ' . $vals[1]['attributes']['PUSH-ID'] . "<br \>\n"; 
echo 'REPLY-TIME: ' . $vals[1]['attributes']['REPLY-TIME'] . "<br \>\n"; 
echo 'Response CODE: ' . $vals[2]['attributes']['CODE'] . "<br \>\n"; 
echo 'Response DESC: ' . $vals[2]['attributes']['DESC'] . "<br \> \n"; 
} else { 
echo '<p>An error has occured</p>' . "\n"; 
echo 'Error CODE: ' . $vals[1]['attributes']['CODE'] . "<br \>\n"; 
echo 'Error DESC: ' . $vals[1]['attributes']['DESC'] . "<br \>\n"; 
} 


?>