简体   繁体   中英

PHP to C# Conversion

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM