Send an SMS message with yii2

I started with yii2. I want to send an SMS message with yii2. Can anyone help?

I wrote this code in php and it works perfectly

<form id="form1" name="form1" method="post" action="">

<input name="to_number" type="text" id="to_number" />

<input type="submit" name="submit" id="submit" value="Submit" />

</form>

<?php

if ( isset($_POST[‘submit’]) ){

$rand=rand(1000,9999);

$to_number = array($_POST["to_number"]);

$message = array($rand);

$client = new SoapClient("parsasms.com/webservice/v2.asmx?WSDL");

$params = array(

‘username’ => ‘demo’,

‘password’ => ‘demo’,

‘senderNumbers’ => array(“30005006002651”),

‘recipientNumbers’=> $to_number,

‘messageBodies’ => $message

);

$results = $client->SendSMS( $params );

echo $rand;

}

?>

error message or problem: Nothing, just did not work

PLZ anybody can help…

you question in not clear , but you must test step by step like

echo ‘<pre>’;

print_r($results);

die();

and another thing is : you must add clear address for SoapClient I think like this \SoapClient

you need to understand how basic MVC works try to follow the guides to get an understanding then you can easily convert you app to yii, yii is php but it gives your app structure here is an example with your code

in your yii app copy the following code in views/site/index.php file.




<form id="form1" name="form1" method="post" action="">

<input name="to_number" type="text" id="to_number" />

<input type="submit" name="submit" id="submit" value="Submit" /> 

</form>



then copy the following in your SiteController in actionIndex which is located under controllers/SiteController.php, also make sure you have your SoapClient loaded.


if ( isset($_POST['submit']) ){

$rand=rand(1000,9999);

$to_number = array($_POST["to_number"]);

$message = array($rand); 


$client = new SoapClient("parsasms.com/webservice/v2.asmx?WSDL");

$params = array(

'username' => 'demo',

'password' => 'demo',

'senderNumbers' => array("30005006002651"),

'recipientNumbers'=> $to_number,

'messageBodies' => $message

);

$results = $client->SendSMS( $params );

echo $rand;	

}	



I did the things you said but did not work.

The error was:

Bad Request (#400)

Unable to verify your data submission.

The above error occurred while the Web server was processing your request.

Please contact us if you think this is a server error. Thank you.

ah that is csrf token, heck try this.


<?= Html::beginForm(['/site/index'], 'post', [

    'class' => 'sms-form'

]) ?>

<?= Html::textInput('to_number') ?>

<?= Html::submitButton('Submit') ?>

<?= Html::endForm() ?>

Problem Bad Request(#400) was solved. But it still does not work

code in views/site/index.php




<?= Html::beginForm(['/site/index'], 'post', ['class' => 'sms-form']) ?>

<?= Html::textInput('to_number') ?>

<?= Html::submitButton('Submit') ?>

<?= Html::endForm() ?>



code in controllers/SiteController.php, in actionIndex




if ( isset($_POST['Submit']) ){

echo('ok');   

}



check for post data instead of Submit, if you wanna check for Submit, you need to add a name attribute to the button.




if (!empty($_POST["to_number"])) {

echo "ok! I have a number.";

}

or add this


<?= Html::submitButton('Submit', ['name' => 'Submit']) ?>

you should learn the basics first before you jump into a framework,