找到你要的答案

Q:PHP to C# Conversion

Q:PHP C #转换

Here is My c# Code

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

Here is PHP Code

<?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);
?>

I want to use Options in PHP code in c# code. Options is divided into 2 parts : send at and expires at. send_at is "Time to send the message in Unix Time format" expires_at is "Time to give up trying to send the message at in Unix Time format" How Can I add options in my c# code and define send_at and expires_at in Unix Time format

这是我#代码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);
?>

I want to use Options in PHP code in c# code. Options is divided into 2 parts : send at and expires at. send_at is "Time to send the message in Unix Time format" expires_at is "Time to give up trying to send the message at in Unix Time format" How Can I add options in my c# code and define send_at and expires_at in Unix Time format

answer1: 回答1:

Possible problem 1:

You are running PHP version before 5.4 and your array construct is wrong. You could try:

$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

Possible problem 2:

strtotime is playing up. You could try:

$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

Different solution:

Looking at the C# example, it appears you can call this service from a URL. Why not just set up a bash script to call curl? It's how I access my SMS provider. Rather than mess around in a programming language, my SMS script looks like:

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

where $1 is the parameter to the script. I call it from other scripts or programs with:

~/send_sms.sh my+message+here

可能的问题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手册

可能的问题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手册上的时间

不同的解决方案:

看着C #例子,看来你可以从URL调用此服务。为什么不设立一个bash脚本调用卷曲?这是我如何访问我的短信提供商。而不是混乱的编程语言,我的短信脚本看起来像:

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

其中1美元是脚本的参数。我叫它从其他脚本或程序:

~/send_sms.sh my+message+here
answer2: 回答2:

Here is C# Code which solved my problem

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

这是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);
c#  php  unix-timestamp