0

文件名:sms1.php 位于:http ://techmentry.com/sms1.php

sms1.php的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>

请参阅http://techmentry.com/sms1.php上的输出。它已经显示错误代码(105)(已经 - 因为它应该在用户单击发送按钮时显示错误代码)。105 错误代码表示缺少“密码”参数。但是我已经在代码中说明了密码。

请帮忙 :)

4

3 回答 3

1

您没有检查表单是否已提交,因此即使在显示初始表单时也正在运行 cURL 代码。你需要做:

将提交按钮更改为:

<input type='submit' name='submit' value='Send'>

并将顶部的 PHP 代码更改为:

<?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";
}
?>

我所做的另一项更改是使用数组而不是字符串作为CURLOPT_POSTFIELDS. 这避免了urlencode()你没有做的所有参数。

从关联数组中分配变量的正确方法是:

$mobiles = $_POST['phone'];

键应该用引号引起来,数组名本身不应该。由于字符串中变量插值的工作方式,您的方式有效,但除非您将变量嵌入更长的字符串中,否则通常不会这样做,例如:

echo "The phone number is $_POST[phone]";

许多程序员完全避免这种语法,更喜欢串联:

echo "The phone number is ". $_POST['phone'];

这是一种风格选择,无论哪种方式都没有广泛的共识。

于 2013-07-24T13:11:45.733 回答
0

“在字符串中使用 PHP 变量和换行符通常会导致问题,您可以尝试以下方法:

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>
于 2013-07-24T13:14:31.850 回答
0

限定符:我正在学习 PHP。

你不应该把你的操作包起来if(isset($_POST['submit']))吗?

在您提交任何内容之前,它正在处理中。

于 2013-07-24T13:17:19.817 回答