2017-05-10 95 views
0

我整合短信網關的第一次。當有人向網站付款時,我想發送短信。我使用下面的代碼:短信網關問題PHP

<?php 
$pay="1000"; 
$msg="Arivind"; 
echo $url="http://yourdomainname.com/api/swsend.asp?username=xxxxxx&password=xxxxxx&sender=SENDERID&sendto=91XXXXXXXXX&message=Dear'$msg' Thanks for making payment of Rs '$pay'"; 
$c=curl_init(); 
curl_setopt($c,CURLOPT_RETURNTRANSFER,1); 
curl_setopt($c,CURLOPT_URL,$url); 
$contents=curl_exec($c); 
curl_close($c); 
echo "SMS Successfully sent"; 
?> 

現在,如果我在郵件的正文使用變量,則不會發送消息,但如果我使用靜態消息的消息是越來越傳遞到數目。 靜態信息犯規解決我的目的,我需要的信息被髮送到不同的人,變量即使用$味精會的人&從數據庫中獲取不同的名稱。

親切SUGGEST。

+1

您使用urlencode php函數窗體消息,然後嘗試發送 –

回答

0

您還可以使用http_build_query到您的變量轉換成格式良好的URL。單引號之間

http://www.tutorialspoint.com/php_mysql_online.php?PID=0Bw_CjBb95KQMWFpZLVJGLWZQYWM

<?php 

$fname = 'Matthew'; 
$lname = 'Douglas'; 
$amount = 1000; 
$message = "Thanks for your payment of Rs {$amount}."; 

$urlComponents = array(
    'firstName' => $fname, 
    'lastName' => $lname, 
    'message' => $message 
); 

$url = 'http://yourdomainname.com/api/swsend.asp?'; 

echo $url . http_build_query($urlComponents); 
?> 
1

使用變量'不將其轉換成動態值。同時它能夠更好地在簡單的PHP函數RESTAPI:

function CURLcall($number, $message_body){ 

     $api_params = "swsend.asp?username=xxxxxx&password=xxxxxx&sender=SENDERID&sendto=91XXXXXXXXX&message=$message_body"; 
     $smsGatewayUrl = "echo $url="http://yourdomainname.com/api/"; 
     $smsgatewaydata = $smsGatewayUrl.$api_params; 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_POST, false); 
     curl_setopt($ch, CURLOPT_URL, smsgatewaydata); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     $output = curl_exec($ch); 
     curl_close($ch); 
     // Use file get contents when CURL is not installed on server. 
     if(!$output){ 
       $output = file_get_contents($smsgatewaydata); 
     } 
    } 

致電上述功能:

$message_body = urlencode("Dear $msg Thanks for making payment of Rs $pay"); 
CURLcall('918954xxxxx',$message_body); 

請注意:用urlencode是有用的,以避免在GET方法的錯誤,因爲它轉換空間爲編碼格式http://php.net/manual/en/function.urlencode.php