【问题标题】:SMS GATEWAY ISSUE in PHPPHP 中的 SMS 网关问题
【发布时间】:2017-10-09 01:55:48
【问题描述】:

我是第一次集成 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 将具有不同的人名并从数据库中获取。

请推荐。

【问题讨论】:

  • 您使用 urlencode php 函数表单消息,然后尝试发送

标签: php sms integration sms-gateway


【解决方案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

【讨论】:

    【解决方案2】:

    您还可以使用 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);
    ?>
    

    【讨论】:

      猜你喜欢
      • 2011-04-09
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      • 2016-11-06
      • 2012-09-20
      • 2018-09-11
      相关资源
      最近更新 更多