将数据从C#传递到PHP [英] Pass data from C# to PHP

查看:352
本文介绍了将数据从C#传递到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP脚本,它将向iPhone发送通知.(下面)我的网站代码是C#.我要做的是将信息从C#传递到PHP脚本.

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.

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和密码短语传递给此脚本.我将所有东西都放在同一台服务器上,并将它们放在服务器上的同一位置.

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.

我要从中启动PHP脚本的C#代码在这里.这段代码基本上是获取我必须发送通知的所有设备令牌.在该foreach循环的内部,是我需要调用PHP脚本的地方.

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

推荐答案

在您的PHP脚本中,使用$ _POST来获取deviceToken和密码

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

?>

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

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;
}

最后调用此方法

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

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

这篇关于将数据从C#传递到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆