简体   繁体   中英

SMS API in PHP doesn't work if there is space in message

From Angular I am trying to hit a PHP API URL to send SMS.

Everything works fine if there is no space is the message, but if I add a space I am not receiving any messages.

Below is my PHP code:

<?php
header('Access-Control-Allow-Headers:Content-Type,x-prototype-version,x-requested-with');
header('Cache-Control:max-age=900');
include_once("conn.php");
$phno=$_POST['phno'];
$msg=$_POST['msg'];
$final = array();
$msg='Thank you.';
sms($phno,$msg);

$final['status']='success';
$final['message']="Success";



    echo json_encode($final);
function sms($phonesms,$msg)
    {
        //echo 'hello';
        $url = 'http://sms.domain.com/Api.aspx?usr=abcapi&pwd=pass123&smstype=TextSMS&to='.$phonesms.'&rout=Transactional&from=SMSACT&msg='.$msg; 
echo $url;      
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_POST, false);
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         $response = curl_exec($ch);
         $err = curl_error($ch);
         curl_close($ch);
    }
?>

So if $msg='Thank you.'; SMS will not go, and if $msg='Thankyou.'; SMS goes.

I am sure there is something more I need to do here - $url = 'http://sms.domain.com/Api.aspx?usr=abcapi&pwd=pass123&smstype=TextSMS&to='.$phonesms.'&rout=Transactional&from=SMSACT&msg='.$msg;

String concatenation is not an ideal way to build a URL. If you do it this way make sure you apply the proper escaping for all the parameters.

$url = 'http://sms.domain.com/Api.aspx?usr=abcapi&pwd=pass123&smstype=TextSMS&to='
    . urlencode($phonesms)
    . '&rout=Transactional&from=SMSACT&msg='
    . urlencode($msg); 

Note: many characters are not legal or have special meaning inside a URL, like spaces; this will convert your spaces to "+" characters, which is their "escaped" version.

See https://www.php.net/manual/en/function.urlencode.php for more info.

使用 urlencode($msg)

$url = 'http://sms.domain.com/Api.aspx?usr=abcapi&pwd=pass123&smstype=TextSMS&to='.$phonesms.'&rout=Transactional&from=SMSACT&msg='.urlencode($msg);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM