使用PHP Curl返回SMS值。下面的代码用于发送短信但我需要能够为每个动作返回值 例如,如果sms是ok,则返回值成功,如果值为2904,则返回SMS发送失败等。
任何帮助将不胜感激。 SMS网关API的返回值如下:
OK=Successful
2904=SMS Sending Failed
2905=Invalid username/password combination
2906=Credit exhausted
<?php
$data = array(
'username' => $_GET['username'],
'password' => $_GET['password'],
'sender' => $_GET['sender'],
// 'sender' => $_GET['uname'],
'recipient' => $_GET['recipient'],
'message' => $_GET['message']
);
// Send the POST request with cURL
$ch = curl_init('http://example.com/components/com_spc/smsapi.php');
curl_setopt($ch, CURLOPT_POST, true);
$header[ ] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[ ] = "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[ ] = "Cache-Control: max-age=0";
$header[ ] = "Connection: keep-alive";
$header[ ] = "Keep-Alive: 300";
$header[ ] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[ ] = "Accept-Language: en-us,en;q=0.5";
$header[ ] = "Pragma: "; // browsers keep this blank.
// also tried $header[] = "Accept: text/html";
curl_setopt ($ch, CURLOPT_HTTPHEADER, $header);
//curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/4.0");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch); //This is the result from Textlocal
curl_close($ch);
if($result === false) {
//if(curl_exec($ch) === false) {//
echo '<font color=red>Message sending failed</font>';
} else {
echo '<font color=green>SMS Message also successfully Sent.</font>';
}
print($result);
?>
答案 0 :(得分:0)
编辑:它如何返回您列出的值?我更改了下面的代码,但它仍然可能无法正常工作,因为我猜测它返回响应代码的索引名称:
您正在寻找curl_getinfo($ch)
命令。它返回一个数组,您需要查看该数组以查找存储返回值的数组键。我猜这是'http_code'
。
具体做法是:
if($result === false) {
echo '<font color=red>Message sending failed</font>';
echo '<br>More info:';
$curl_info = curl_getinfo($ch);
if ($curl_info['http_code']===2906) {
echo 'Credit exhausted';
} elseif ($curl_info['http_code']===2905) {
echo 'invalid username/password';
} elseif ($curl_info['http_code']===2904) {
echo 'SMS failed';
}
} else {
echo '<font color=green>SMS Message also successfully Sent.</font>';
}