2013-07-24 29 views
0

文件名:sms1.php 坐落在:sms1.php的http://techmentry.com/sms1.phpphp-最新的錯誤?

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錯誤代碼意味着缺少'密碼'參數。但是我已經在代碼中說明了密碼。

請幫助:)

+0

一件事,你的'$手機= 「$ _ POST [手機]」。',應讀爲$手機= $ _ POST [ 'phone'];'(減去雙引號)並在括號內使用單引號。對其他人也一樣,嘗試一下。 –

+1

@Fred至少告訴他正確的語法......'$ mobiles = $ _POST ['phone'];' – Rufinus

+1

@Rufinus oopss !!!如此真實。好的趕上,會編輯,謝謝。 (我正在喝第一杯咖啡),那女服務員在哪裏?服務員?雙美式咖啡請帶一杯意式濃縮咖啡。 –

回答

1

你不檢查表單是否被提交,所以你顯示初始表單時甚至跑步捲曲代碼。你需要做的:

更改提交按鈕:

<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']; 

這是一個文體選擇,沒有廣泛的共識,無論哪種方式。

+0

這正是我提出的OP,但是用'user =「hidden」;'和'password =「hidden」;'你留在那裏,我覺得OP沒有太多經驗,懷疑他/她會效仿你的榜樣。我認爲你的答案會起作用,用正確的語法來替換隱藏的值。 –

+0

你也忘了在'$ sender = $ _POST ['sender']「中取出'''''';-) –

+0

我這樣做了。問題消失了,新出現了LOL。當我點擊發送按鈕,它只是刷新頁面。它應該返回一個消息ID或錯誤代碼:/ – user2604855

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> 
0

預選賽:我在學習PHP

你是不是應該換你的操作中?if(isset($_POST['submit']))

你提交任何東西之前,是處理

+0

想念a)D – Rufinus

+0

@Rufinus很高興這不是我的「編輯」。今天早上你真的「在球上」! hehehe –

+0

@Fred我正在進行代碼審查。我的同事們有幾十個類。所以我的「內部語法分析器」現在訓練有素:D – Rufinus