4

我有一个 PHP 脚本,它将向 iPhone 发送通知。(下)我的网站代码是用 C# 编写的。我想要做的是将信息从 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# 代码在这里。这段代码基本上是获取我必须发送通知的所有设备令牌。在 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);
    }

任何帮助将不胜感激。谢谢

4

1 回答 1

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'];

?>

将数据发布到您的 PHP 站点的 C# 方法

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));
于 2013-03-05T15:42:11.597 回答