【问题标题】:php- whats the error?php - 什么错误?
【发布时间】:2013-07-23 23:36:47
【问题描述】:

文件名: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 错误代码表示缺少“密码”参数。但是我已经在代码中说明了密码。

请帮忙:)

【问题讨论】:

  • 一方面,您的$mobiles = "$_POST[phone]"; 应读作$mobiles = $_POST['phone'];(减去双引号)并在括号内使用单引号。对其他人做同样的事情,试试看。
  • @Fred 至少告诉他正确的语法...$mobiles = $_POST['phone'];
  • @Rufinus 哎呀!!!如此真实。很好,将编辑,谢谢。 (我正在喝第一杯咖啡),那位女服务员呢?女服务员?!请加一份美式咖啡加一杯浓缩咖啡。
  • @user2604855 告诉我你什么时候修复它,这样我就可以使用你的脚本通过你的脚本发送我所有的短信,并为我节省一些钱。 :)
  • 我在 $mobiles = $_POST[phone] 中添加了 ';现在它的 $mobiles = $_POST['phone'];现在出现错误:解析错误:语法错误,意外的 T_ENCAPSED_AND_WHITESPACE,在第 6 行的 /home/ash/public_html/sms1.php 中期待 T_STRING 或 T_VARIABLE 或 T_NUM_STRING 当我删除 ' 时,错误也会消失!请帮忙

标签: php html api curl


【解决方案1】:

限定符:我正在学习 PHP。

您不应该将您的操作封装在if(isset($_POST['submit'])) 中吗?

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

【讨论】:

  • @Rufinus 很高兴这不是我的“编辑”。你今天早上真的很“忙”!呵呵
  • @Fred 我正在进行代码审查.. 我的同事有几十个班级.. 所以我的“内部语法解析器”目前训练有素:D
  • @Rufinus 你有一个“敏锐的眼睛”;-)
  • @Rufinus 谢谢先生!
【解决方案2】:

“在字符串中使用 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>

【讨论】:

    【解决方案3】:

    您没有检查表单是否已提交,因此即使在显示初始表单时您也在运行 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'];
    

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

    【讨论】:

    • 这正是我对 OP 的建议,但是你留下的 $user = "hidden";$password = "hidden"; 给人的印象是 OP 没有太多经验,并且怀疑他/她会效仿你的例子。我认为你的答案会起作用,用正确的语法替换 "hidden" 值。
    • 你也忘了在$sender = $_POST['sender']"; 中取出" ;-)
    • 我做到了。问题消失了,新的出现了大声笑。当我单击发送按钮时,它只是刷新页面。它应该返回消息 ID 或错误代码:/
    • 啊,你的提交按钮没有name="submit"
    • 终于工作了!!谢谢 :) 它返回消息 ID 51efd93046923e8920a6c*** 你能告诉我如何在用户单击发送按钮时将消息 ID 替换为任何文本吗?
    猜你喜欢
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 2013-11-24
    • 2012-02-18
    相关资源
    最近更新 更多