PHP 到 C# 转换

PHP to C# Conversion

这是我的 C# 代码

public void FormEvents_Submit(object sender, SubmitEventArgs e)
        {

            var UriBuilder = new UriBuilder("http://smsgateway.me/api/v3/messages/send/");
            var parameters = HttpUtility.ParseQueryString(string.Empty);
            parameters["email"] = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:email", NamespaceManager).Value;
            parameters["password"] = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:password", NamespaceManager).Value;
            parameters["device"] = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:device", NamespaceManager).Value; ;
            parameters["number"] = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:MobileNumber", NamespaceManager).Value;
            parameters["message"] = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:SMS_TO_BE_SENT", NamespaceManager).Value; ;
            parameters["options"]= ;
            UriBuilder.Query = parameters.ToString();
            //UriBuilder.Fragment = "some_fragment";

            Uri finalUrl = UriBuilder.Uri;
            var request = WebRequest.Create(finalUrl);

这是PHP代码

<?php
include "smsGateway.php";
$smsGateway = new SmsGateway('demo@smsgateway.me', 'password');

$deviceID = 1;
$number = '+44771232343';
$message = 'Hello World!';

$options = [
'send_at' => strtotime('+10 minutes'), // Send the message in 10 minutes
'expires_at' => strtotime('+1 hour') // Cancel the message in 1 hour if the message is not yet sent
];

//Please note options is no required and can be left out
$result = $smsGateway->sendMessageToNumber($number, $message, $deviceID, $options);
?>

我想在 c# 代码 中使用PHP 代码中的选项。 选项分为两部分:发送时间和到期时间。 send_at 是 "Time to send the message in Unix Time format" expires_at 是 "Time to give up trying to send the message at in Unix Time format" 如何在我的 C# 代码中添加选项 并以 Unix 时间格式定义 send_at 和 expires_at

可能问题1:

您是运行 PHP 5.4 之前的版本并且您的数组构造有误。你可以试试:

$options = array(
'send_at' => strtotime('+10 minutes'), // Send the message in 10 minutes
'expires_at' => strtotime('+1 hour') // Cancel the message in 1 hour if the message is not yet sent
);

PHP Manual on arrays

可能问题2:

strtotime正在播放。你可以试试:

$options = [
'send_at' => time() + (10 * 60), // Send the message in 10 minutes
'expires_at' => time() + (60*60) // Cancel the message in 1 hour if the message is not yet sent
];

PHP Manual on time

不同解法:

查看 C# 示例,您似乎可以从 URL 调用此服务。为什么不设置一个 bash 脚本来调用 curl?这就是我访问 SMS 提供商的方式。我的 SMS 脚本看起来像这样:

curl "https://www.textmarketer.biz/gateway/?username=****&password=****&number=****&message=&orig=****"

其中 </code> 是脚本的参数。我从其他脚本或程序调用它:</p> <pre><code>~/send_sms.sh my+message+here

这是解决我问题的 C# 代码

public void FormEvents_Submit(object sender, SubmitEventArgs e)
{

    DateTime dateTime1 = DateTime.Now;
    var year1 = dateTime1.Year+1;
    var mon1 = dateTime1.Month;
    var day1 = dateTime1.Day;
    var hour1 = dateTime1.Hour;
    var min1 = dateTime1.Minute;
    var sec1 = dateTime1.Second;

    System.Windows.Forms.MessageBox.Show(Convert.ToString(year1));// To check
    var dateTime = new DateTime(year1, mon1, day1, hour1, min1, sec1, DateTimeKind.Local);
    var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    var unixDateTime = (dateTime.ToUniversalTime() - epoch).TotalSeconds;

    //DateTime.Parse(hour1);
    var UriBuilder = new UriBuilder("http://smsgateway.me/api/v3/messages/send/");
    var parameters = HttpUtility.ParseQueryString(string.Empty);
    parameters["email"] = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:email", NamespaceManager).Value;
    parameters["password"] = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:password", NamespaceManager).Value;
    parameters["device"] = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:device", NamespaceManager).Value; ;
    parameters["number"] = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:MobileNumber", NamespaceManager).Value;
    parameters["message"] = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:SMS_TO_BE_SENT", NamespaceManager).Value; ;
   // parameters["send_at"] = "";
    parameters["expires_at"] = Convert.ToString(unixDateTime);
    UriBuilder.Query = parameters.ToString();
    //UriBuilder.Fragment = "some_fragment";

    Uri finalUrl = UriBuilder.Uri;
    var request = WebRequest.Create(finalUrl);