使用PHP Curl返回短信值 [英] Returning SMS values using PHP Curl
问题描述
使用PHP Curl返回SMS值。下面的代码用于发送短信,但我需要能够为每个操作返回值
例如如果sms是OK返回值成功,如果值是2904返回SMS发送失败等。
Returning SMS values using PHP Curl.The code below was used to send sms but i need to be able to return values for every action for instance if sms is ok return value successful, if value is 2904 return SMS sending failed etc.
任何帮助将不胜感激。
SMS网关API的返回值如下:
Any help will be appreciated. The return values of SMS gateway API are as follows:
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);
?>
推荐答案
edit:如何返回您列出的值?我改变了下面的代码,但它仍然可能不工作,因为我猜在索引的名称,它返回响应代码:
edits: How does it return the values you list? I changed the code below, but it still may not work because I am guessing at the name of the index that it returns the response code:
您正在寻找 curl_getinfo($ ch)
命令。它返回一个数组,你将需要查看该数组来找到存储返回值的数组键。我猜这是'http_code'
。
You are looking for the curl_getinfo($ch)
command. It returns an array, and you will need to look at that array to find the array key where the return value is stored. I will guess that it is '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>';
}
这篇关于使用PHP Curl返回短信值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!