2013-03-05 123 views
3

我有一個PHP腳本,它會向iPhone發送通知(下面)我在C#中使用了我的網站代碼。我想要做的是將來自C#的信息傳遞給PHP腳本。將數據從C#傳遞到PHP

PHP腳本

<?php 

// Put your device token here (without spaces): 
$deviceToken = ''; //Get from C# 

// Put your private key's passphrase here: 
$passphrase = ''; //Get from C# 

// Put your alert message here: 
$message = 'New Message'; 

//////////////////////////////////////////////////////////////////////////////// 

$ctx = stream_context_create(); 
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); 
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 

// Open a connection to the APNS server 
$fp = stream_socket_client(
          'ssl://gateway.push.apple.com:2195', $err, 
          $errstr, 30, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); 

if (!$fp) 
exit("Failed to connect: $err $errstr" . PHP_EOL); 

echo 'Connected to APNS' . PHP_EOL; 

// Create the payload body 
$body['aps'] = array(
        'alert' => $message, 
        'sound' => 'default' 
        ); 

// Encode the payload as JSON 
$payload = json_encode($body); 

// Build the binary notification 
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; 

// Send it to the server 
$result = fwrite($fp, $msg, strlen($msg)); 

if (!$result) 
echo 'Message not delivered' . PHP_EOL; 
else 
echo 'Message successfully delivered' . PHP_EOL; 

// Close the connection to the server 
fclose($fp); 

?> 

我想通過deviceToken和密碼給該腳本。我擁有同一臺服務器上的所有內容,並將它們放在服務器上的相同位置。

我想從這裏啓動PHP腳本的C#代碼。這個代碼基本上是獲得我必須發送通知的所有devicetokens。在那個foreach循環裏面我需要調用PHP腳本。

private void SendAppleNotifications(List<NotificationInfo> AppleNotifications) 
    { 
     ApplePushNotification push = new ApplePushNotification(false, AppleCertificate, ApplePassword); 

     List<NotificationPayload> notificationList = new List<NotificationPayload>(); 


     List<string> returnStrings = new List<string>(); 

     foreach (NotificationInfo ni in AppleNotifications) 
     {      
     } 

     returnStrings = push.SendToApple(notificationList); 
    } 

任何幫助將不勝感激。由於

+2

使用GET或POST參數 – Tearsdontfalls 2013-03-05 15:29:33

+0

如何執行php腳本? – user1950929 2013-03-05 15:30:20

+0

是C#程序集和你的php腳本在同一臺Windows機器上運行?你有IIS服務器運行嗎? – 2013-03-05 15:33:15

回答

3

在你的PHP腳本中使用$ _POST獲得deviceToken和密碼

<?php 

// Put your device token here (without spaces): 
$deviceToken = $_POST['deviceToken']; 
// Put your private key's passphrase here: 
$passphrase = $_POST['passphrase']; 

?> 

C#方法到POST數據到PHP站點

public string SendPost(string url, string postData) 
{ 
    string webpageContent = string.Empty; 

    try 
    { 
     byte[] byteArray = Encoding.UTF8.GetBytes(postData); 

     HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); 
     webRequest.Method = "POST"; 
     webRequest.ContentType = "application/x-www-form-urlencoded"; 
     webRequest.ContentLength = byteArray.Length; 

     using (Stream webpageStream = webRequest.GetRequestStream()) 
     { 
      webpageStream.Write(byteArray, 0, byteArray.Length); 
     } 

     using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) 
     { 
      using (StreamReader reader = new StreamReader(webResponse.GetResponseStream())) 
      { 
       webpageContent = reader.ReadToEnd(); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     //throw or return an appropriate response/exception 
    } 

    return webpageContent; 
} 

最後調用此方法

String deviceToken = HttpUtility.UrlEncode("YourDeviceToken"); 
String passphrase = HttpUtility.UrlEncode("YourPassphrase"); 

SendPost("http://yourphpsite.com/xxx.php", String.Format("deviceToken={0}&passphrase={1}", deviceToken, passphrase)); 
+0

我的腳本存在問題,但只要連接2這個作品。謝謝 – BigT 2013-03-05 18:00:20