2013-03-05 131 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