Reputation: 9
File Name: sms1.php Located at: http://techmentry.com/sms1.php
PHP code of sms1.php:
<?php
//Variables to POST
$user = "hidden";
$password = "hidden";
$mobiles = "$_POST[phone]";
$message = "$_POST[msg]";
$sender = "$_POST[sender]";
//Initialize CURL data to send via POST to the API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.hidden/sendhttp.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"user=$user&
password=$password&
mobiles=$mobiles&
message=$message&
sender=$sender"
);
//Execute CURL command and return into variable $result
$result = curl_exec($ch);
//Do stuff
echo "$result";
?>
<br><br><br>
<form name='sms' action='' method='post'>
Phone number<br/><input type='text' name='phone' value='' maxlength=12/>
<br/>
Sender ID (from) <br/><input type='text' name='sender' value='' maxlength=15/>
<br/>
Message : <br/><textarea rows=5 cols=30 name='msg'></textarea>
<br/>
<input type='submit' value='Send'>
</form>
Please see the output on http://techmentry.com/sms1.php . It is already displaying an error code (105) (already - because it should show the error code when user click send button). 105 error code means missing 'password' parameter. But I have already stated the password in the code.
Please help :)
Upvotes: 0
Views: 87
Reputation: 1
Qualifier: I'm learning PHP.
Shouldn't you wrap your operations in if(isset($_POST['submit']))
?
It is processing before you submit anything.
Upvotes: 0
Reputation: 874
"Using PHP variables and newlines within strings often results in problems, could you try the following:
if ($_SERVER['REQUEST_METHOD'] == "POST")){
//this prevents code being executed on page load
$user = "hidden";
$password = "hidden";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.hidden/sendhttp.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
//removed newlines and variables in string
curl_setopt($ch, CURLOPT_POSTFIELDS,"user=".$user.
"&password=".$password.
"&mobiles=". $_POST['phone'].
"&message=". $_POST['msg'] .
"&sender=".$_POST['sender']);
//Execute CURL command and return into variable $result
$result = curl_exec($ch);
//Do stuff
echo "$result";
}
?>
<br><br><br>
<form name="sms" action="" method="post">
Phone number<br/><input type="text" name="phone" value="" maxlength="12"/>
<br/>
Sender ID (from) <br/><input type="text" name="sender" value="" maxlength="15"/>
<br/>
Message : <br/><textarea rows="5" cols="30" name="msg"></textarea>
<br/>
<input type="submit" value="Send">
</form>
Upvotes: 0
Reputation: 780879
You're not checking whether the form was submitted, so you're running the cURL code even when displaying the initial form. You need to do:
Change the submit button to:
<input type='submit' name='submit' value='Send'>
and change the PHP code at the top to:
<?php
if (isset($_POST['submit'])) {
//Variables to POST
$user = "hidden";
$password = "hidden";
$mobiles = $_POST['phone'];
$message = $_POST['msg'];
$sender = $_POST['sender'];
//Initialize CURL data to send via POST to the API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.hidden/sendhttp.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
array('user' => $user,
'password' => $password,
'mobiles' => $mobiles,
'message' => $message,
'sender' => $sender)
);
//Execute CURL command and return into variable $result
$result = curl_exec($ch);
//Do stuff
echo "$result";
}
?>
The other change I made was to use an array instead of a string as the parameter for CURLOPT_POSTFIELDS
. This avoids having to urlencode()
all the parameters, which you weren't doing.
And the proper way to assign variables from an associative array is like:
$mobiles = $_POST['phone'];
The key should be in quotes, the array name itself should not. Your way worked because of the way variable interpolation in strings works, but it's not generally done that way unless you're embedding the variable in a longer string, like:
echo "The phone number is $_POST[phone]";
Many programmers avoid this syntax entirely, preferring concatenation:
echo "The phone number is ". $_POST['phone'];
That's a stylistic choice, there's no widespread concensus either way.
Upvotes: 1