将数据从C#传递到PHP
我有一个PHP脚本,可以向iPhone发送通知。(如下)我的网站代码是C#。我要做的是将信息从C#传递到PHP脚本将数据从C#传递到PHP,c#,php,apple-push-notifications,C#,Php,Apple Push Notifications,我有一个PHP脚本,可以向iPhone发送通知。(如下)我的网站代码是C#。我要做的是将信息从C#传递到PHP脚本 private void SendAppleNotifications(List<NotificationInfo> AppleNotifications) { ApplePushNotification push = new ApplePushNotification(false, AppleCertificate, ApplePass
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);
}
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);
?>
在PHP脚本中使用$\u POST获取deviceToken和密码短语
<?php
// Put your device token here (without spaces):
$deviceToken = $_POST['deviceToken'];
// Put your private key's passphrase here:
$passphrase = $_POST['passphrase'];
?>
最后调用这个方法
String deviceToken = HttpUtility.UrlEncode("YourDeviceToken");
String passphrase = HttpUtility.UrlEncode("YourPassphrase");
SendPost("http://yourphpsite.com/xxx.php", String.Format("deviceToken={0}&passphrase={1}", deviceToken, passphrase));
使用GET或POST参数show是否执行php脚本?C#汇编和php脚本是否在同一台windows机器上运行?您是否运行IIS服务器?有一种方法可以使用interop公开您的C#类。或者您可以编写一个C#WCF web服务。我的脚本有问题,但就连接2而言,这是可行的。Thanks@makimPhp应用程序将如何显示从C#SendPost方法接收到的内容?
String deviceToken = HttpUtility.UrlEncode("YourDeviceToken");
String passphrase = HttpUtility.UrlEncode("YourPassphrase");
SendPost("http://yourphpsite.com/xxx.php", String.Format("deviceToken={0}&passphrase={1}", deviceToken, passphrase));