Developing a server-side push application using PHP

You can follow these steps to develop a server-side application that uses the BlackBerry push format for the push request.
  1. Construct the URL to which the push request is sent. Include either the email address or BlackBerry device PIN of the user to whom the pushed data is sent, and the port on which the BlackBerry Browser listens.
    path = "/push?DESTINATION=" . $HTTP_POST_VARS['email'] . 
      "&PORT=7874&REQUESTURI=/";
  2. Open a socket connection to the BlackBerry MDS Connection Service. In the following code sample, the user supplies the address of the BlackBerry MDS Connection Service web server and the port number in the HTML form.
    $besfs = @fsockopen($HTTP_POST_VARS['besHostname'], 
      $HTTP_POST_VARS['besPort'], $errno, $errstr,30)
  3. Define the header properties for the push request. In the following code sample, the user supplies the URL of the web page to be pushed, the push title, and the push type in the HTML form.
    fputs($besfs, "POST $path HTTP/1.0\r\n"); 
    fputs($besfs, "Host: " . $HTTP_POST_VARS['besHostname'] . "\r\n"); 
    fputs($besfs, "Content-Location: . $HTTP_POST_VARS['pushURL'] . 
      "\r\n"); 
    fputs($besfs, "X-RIM-Push-Title: " . $HTTP_POST_VARS
      ['pushTitle'] . "\r\n"); 
    fputs($besfs, "X-RIM-Push-Type: " . $HTTP_POST_VARS['pushType'] . 
      "\r\n");
  4. Send the web page to the BlackBerry device.
    $webURL = parse_url($HTTP_POST_VARS['pushURL']);
    
    if (empty($webURL['port']))
        $port = 80;
    else
        $port = $webURL['port'];
    
    //Open a socket connection to the webserver
    //hosting the page to push.
    $webfs = @fsockopen($webURL['host'], $port, $errno, $errstr, 30);
    fputs($webfs, "GET " . $webURL['path'] . " HTTP/1.1\r\n");
    fputs($webfs, "Host: " . $webURL['host'] . "\r\n");
    fputs($webfs, "Connection: Close\r\n\r\n");
    
    //Read and discard the first line from the
    //webserver (should be HTTP/1.1 200 OK).
    fgets($webfs);
    
    //Read in the page to push from the web server
    //and write it out to the MDS server.
    while (!feof($webfs))
    {
        fputs($besfs, fgets($webfs));
    }
    
    //Close the connection to the web server.
    fclose($webfs);
  5. Send the output from the BlackBerry MDS Connection Service to the client. A response code of 200 indicates a successful push.
    $buf = "";
    //Capture the BlackBerry MDS Connection Service’s reply to the 
    //push.
    while (!feof($besfs))
        $buf .= fgets($besfs,128);
  6. Close the connection.
    fclose($besfs);
    //Display the BlackBerry MDS Connection Service's reply.
    echo "Response from MDS: <br>";
    echo $buf;

Server-side sample application

<html>
  <title>Push Results</title>
  <body>
    <?
		
   //Ensure all required variables have been filled in.
   if (!@empty($HTTP_POST_VARS['besHostname']) || 
             !@empty($HTTP_POST_VARS['email']) || 
             !@empty($HTTP_POST_VARS['pushURL']) || 
             !@empty($HTTP_POST_VARS['pushType']) || 
             !@empty($HTTP_POST_VARS['pushTitle']))
   {
		
     //Ensure that the MDS Push port is numeric.
			  if(!is_numeric($HTTP_POST_VARS['besPort']))
    	{
    			echo "Error! MDS Push Port must be numeric!";
     }
     else
     {
		
     		//Create the push URL.
       $path = "/push?DESTINATION=" . $HTTP_POST_VARS['email'] . 
                    "&PORT=7874&REQUESTURI=/";
    		 
       //Open a socket connection to the MDS Server.
       if ($besfs = @fsockopen($HTTP_POST_VARS['besHostname'], 
                    $HTTP_POST_VARS['besPort'], 
                    $errno, $errstr, 30))
       {
         fputs($besfs, "POST $path HTTP/1.0\r\n");
         fputs($besfs, "Host: " . $HTTP_POST_VARS['besHostname'] . "\r\n");
         fputs($besfs, "Content-Location: " . 
                      $HTTP_POST_VARS['pushURL'] . "\r\n");
         fputs($besfs, "X-RIM-Push-Title: " . 
                      $HTTP_POST_VARS['pushTitle'] . "\r\n");
         fputs($besfs, "X-RIM-Push-Type: " . 
                      $HTTP_POST_VARS['pushType'] . "\r\n");
    			
         //Send the push URL if we are using a browser channel 
         //or browser channel delete.
         if ($HTTP_POST_VARS['pushType'] == "Browser-Channel" ||
																		    $HTTP_POST_VARS['pushType'] == "Browser-Channel-Delete")
         {
           fputs($besfs, "X-RIM-Push-Channel-ID: " . 
                         $HTTP_POST_VARS['pushURL'] . "\r\n");
    		
           //Send the push icon URLs if we are using a browser channel push.
           if($HTTP_POST_VARS['pushType'] == "Browser-Channel")
           {
             fputs($besfs, "X-RIM-Push-UnRead-Icon-URL: " . 
                            $HTTP_POST_VARS['unreadIconURL'] . "\r\n");
             fputs($besfs, "X-RIM-Push-Read-Icon-URL: " . 
                            $HTTP_POST_VARS['readIconURL'] . "\r\n");
           }
         }
     		
    	    $keepGoing = true;
    		
         {
           $webURL = parse_url($HTTP_POST_VARS['pushURL']);
    		
           //Set the port to 80 if not otherwise specified in the push URL.
           if (empty($webURL['port']))
             $port = 80;
           else
            	$port = $webURL['port'];
      					
    		     //Open a socket connection to the web server hosting the page to push.
           if ($webfs = @fsockopen($webURL['host'], $port, $errno, $errstr, 30))
           {
             fputs($webfs, "GET " . $webURL['path'] . " HTTP/1.1\r\n");
             fputs($webfs, "Host: " . $webURL['host'] . "\r\n");
             fputs($webfs, "Connection: Close\r\n\r\n");
    		
             //Read and discard the first line from the web 
             //server (should be HTTP/1.1 200 OK).
    	        fgets($webfs);
		
             //Read in the page to push from the web server and write it 
             //out to the BlackBerry MDS Connection Service.
             while (!feof($webfs))
             {
               fputs($besfs, fgets($webfs));
             }
     		
    	        //Close the connection to the web server.
             fclose($webfs);
           }
           else
           {
             echo "Failed to connect to " . $HTTP_POST_VARS['pushURL'] . "<br>";
             echo "Error: " . $errno . " " .$errstr;
   		
	            $keepGoing = false;
           }
         }
         else
         
          //If we didn't fail connecting to the web server, lets keep going!
          if ($keepGoing)
          {
            $buf = "";
 		
            //Capture the BlackBerry MDS Connection Service's reply to the push.
            while (!feof($besfs))
              $buf .= fgets($besfs,128);
            fclose($besfs);
		
            //Display the BlackBerry MDS Connection Service's reply.
            echo "Response from MDS: <br>";
            echo $buf;
          }
          else
          {
        		
        	   //Close the connection to the BlackBerry MDS Connection Service.
            fclose($besfs);
          }
        }
        else 
        {
         	echo "Failed to connect to " . $HTTP_POST_VARS['besHostname'] . 
                    " using port " . $HTTP_POST_VARS['besPort'] . "<br>";
          echo "Error: " . $errno . " " .$errstr;
        }
      }
    }
    else
    {
      echo "Error! Required fields were empty. Please try again.";
    }
    ?>
  </body>
</html>

Creating an HTML form used to initiate the push request

The following HTML form is used to collect information used to populate the headers of the push request. This includes BlackBerry MDS Connection Service information, as well as information used by the BlackBerry Browser, such as the push request type. The push request is initiated when the form is submitted.

<html>
  <title>Test Push Application</title>
  <body>
    <form name="phpPush" method="post" action="phpPush.php">
      <h2>Sample php Browser Push</h2>
      <table border="0">
        <tr>
          <td>BlackBerry Enterprise Server Name/IP Address:</td>
          <td><input type="text" name="besHostname" size="27"></td>
        </tr>
        <tr>
          <td>BlackBerry MDS Connection Service Push Port:</td>
          <td><input type="text" name="besPort" size="50"></td>
        </tr>
        <tr>
          <td>Push Recipient's Email Address:</td>
          <td><input type="text" name="email" size="50"></td>
        </tr>
        <tr>
          <td>Push URL (file to be pushed):</td>
          <td><input type="text" name="pushURL" size="50" 
              value="http://localhost/phpPush/testpage/
                    sample.html"></td>
        </tr>
        <tr>
          <td>Push Type:</td>
          <td>
            <select name="pushType">
              <option>Browser-Message</option>
              <option>Browser-Content</option>
            </select>
          </td>
        </tr>
        <tr>
          <td>Push Title:</td>
          <td><input type="text" name="pushTitle" size="50" 
              value="Test Push"></td>
        </tr>
        <tr>
          <td>Unread Icon URL (optional):</td>
          <td><input type="text" name="unreadIconURL" size="50" 
              value="http://localhost/phpPush/testpage/
                    smile_unread.png"></td>
        </tr>
        <tr>
          <td>Read Icon URL (optional):</td>
          <td><input type="text" name="readIconURL" size="50" 
              value="http://localhost/phpPush/testpage/smile.png"></td>
        </tr>
        <tr>
          <td colspan="2">&nbsp;</td>
		</tr>
		<tr>
		  <td></td>
          <td align="right"><input type="submit" name="submit" 
             value="Send Push!"></td>
        </tr>
      </table>
    </form>
  </body>
</html>