问题
I have a PHP script that will send a notification to an iPhone.(below) I have my website code in C#. What I want to do is pass the info from C# to the PHP script.
The PHP script
<?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);
?>
I want to pass the deviceToken and the passphrase to this script. I have everything on the same server and have them in the same location on the server.
The C# code I want to start the PHP script from is here. This code is basically getting all the devicetokens I have to send notifications. inside of that foreach loop is where I need to invoke the PHP script.
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);
}
Any help would be greatly appreciated. Thanks
回答1:
In your PHP Script use $_POST to get deviceToken and passphrase
<?php
// Put your device token here (without spaces):
$deviceToken = $_POST['deviceToken'];
// Put your private key's passphrase here:
$passphrase = $_POST['passphrase'];
?>
C# Method to Post Data to your PHP-Site
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;
}
and finally call this method
String deviceToken = HttpUtility.UrlEncode("YourDeviceToken");
String passphrase = HttpUtility.UrlEncode("YourPassphrase");
SendPost("http://yourphpsite.com/xxx.php", String.Format("deviceToken={0}&passphrase={1}", deviceToken, passphrase));
来源:https://stackoverflow.com/questions/15227604/pass-data-from-c-sharp-to-php