我无法从PHP脚本发送POST请求。所以我的PHP脚本是
<?php
if (isset($_POST))
{
// form validation vars
$formok = true;
$errors = array();
// sumbission data
$ip1 = $_SERVER['REMOTE_ADDR'];
$ip2 = $_SERVER['HTTP_X_FORWARDED_FOR'];
$ip3 = $_SERVER['HTTP_FORWARDED'];
$ua = $_SERVER['HTTP_USERAGENT'];
$date = date('d/m/Y');
$time = date('H:i:s');
// form data
$action = "sendsms";
$user = "user";
$password = "pass";
$from = $_POST['sender'];
$to = $_POST['to'];
$text = $_POST['msg'];
// validation of recepient
if (empty($to))
{
$formok = false;
$errors[] = "I don't know where to deliver the message :(";
}
elseif (!filter_var($to, FILTER_VALIDATE_INT))
{
$formok = false;
$errors[] = "Sorry, but I am unaware of that recepient :(";
}
// validation of sender ID
if (empty($from))
{
$formok = false;
$errors[] = "I don't know the address from where to deliver the message :(";
}
elseif (strlen($from) < 13)
{
$formok = false;
$errors[] = "Sorry, I couldn't handle the sender ID, it's too long :(";
}
// validation of message
if (empty($message))
{
$formok = false;
$errors[] = "I don't know what message to deliver :(";
}
elseif (strlen($message) < 160)
{
$formok = false;
$errors[] = "Sorry, I couldn't handle that message, it's too long :( ";
}
// Send SMS if all is ok
if ($formok)
{
$postData = array(
'action' => $action,
'user' => $user,
'password' => $password,
'from' => $from,
'to' => $to,
'text' => $text,
);
// create post body
$post_body = '';
foreach($postData as $key => $value)
{
$post_body.= urlencode($key) . '=' . urlencode($value) . '&';
}
$post_body = rtrim($post_body, '&');
// Initialize CURL data to send via POST to the API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/http-api.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_body);
// Execute CURL command and return into variable ch
$result = curl_exec($ch);
mail("reports@domain.com", "SMS Report", "===SMS DETAILS=== \n Recepient: $to \n Sender: $from \n Message: $text \n Status: $result \n \n ===USER'S DETAILS=== \n IP adddress: $ip1, $ip2 and $ip3 . \n User Agent: $ua \n \n All the available information has been written");
}
// What we need to return back to our form
$returndata = array(
'posted_form_data' => array(
'to' => $to,
'from' => $from,
'text' => $text
) ,
'form_ok' => $formok,
'errors' => $errors
);
// If this is not an AJAX request
if (empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest')
{
// set session variables
session_start();
$_SESSION['cf_returndata'] = $returndata;
// redirect back to form
header('location: ' . $_SERVER['HTTP_REFERER']);
}
}
它要求用户提供接收者,发件人和文本,但在提交表单时始终显示此错误:
“哎呀......有一些问题: - 抱歉,我无法处理发件人ID,这太长了:( - 我不知道要传递什么信息:(“
任何人都可以说出错了吗?任何帮助将不胜感激。
答案 0 :(得分:2)
elseif (strlen($message) < 160)
{
$formok = false;
$errors[] = "Sorry, I couldn't handle that message, it's too long :( ";
}
为什么消息太长,如果 160下?与发件人id $相同。 $ message甚至没有设置。所以当然如果你用empty()检查它会返回true。
你去:
$message = $_POST['msg'];
elseif (strlen($message) > 160)
elseif (strlen($from) > 13)
答案 1 :(得分:1)
如果它比<13> ,你确定要说的话太长了吗?
elseif (strlen($from) < 13)
{
$formok = false;
$errors[] = "Sorry, I couldn't handle the sender ID, it's too long :(";
}
我认为你的意思是这样做:
elseif (strlen($from) > 13)
而且,这个:
elseif (strlen($message) > 160)
希望这会有所帮助;)
答案 2 :(得分:1)
$ _ POST。
if (isset($_POST))
将始终返回true。
答案 3 :(得分:0)
$ _ POST [&#39;发件人&#39;];似乎接收了少于13个字符串的输入,这是您的代码所不允许的。
if(empty($ message))检查$ message是否为空,即是。我无法找到你将anythig分配给这个变量的地方。