PHP中的SMS GATEWAY ISSUE

问题描述 投票:0回答:2

我是第一次集成SMS网关。当有人付钱给网站时,我想发送短信。我使用以下代码:

<?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"; 
?>

现在,如果我在消息正文中使用变量,则不会发送消息,但如果我使用静态消息,则消息将被传递给该号码。静态消息无法解决我的目的,因为我需要将消息发送给不同的人,使用的变量即$ msg将具有不同的人名并从数据库中获取。

亲切的建议。

php sms integration sms-gateway
2个回答
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


0
投票

您还可以使用http_build_query将变量转换为格式良好的URL。

<?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);
?>
© www.soinside.com 2019 - 2024. All rights reserved.