curl 不适用于 #


curl doesn't work with #

所以我有这个发送短信的函数,这个函数效果很好,但问题是当我发送#或换行符时,函数没有按预期工作!

function sendSMS($phoneNumber,$message){
$ch = curl_init();
$urll = "http://www....com/api/sendsms.php?username=username&password=pass&message=$message&numbers=$phoneNumber&sender=sender&unicode=E&return=full";
  $url = str_replace(' ','%20',$urll);
// set url
curl_setopt($ch, CURLOPT_URL, $url);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
}

如果我像这样使用它,例如:

sendSMS("phone number","#hashtag");

它不发送消息,而当我像这样使用它时:

sendSMS("phone number","some text message");

它将毫无问题地发送消息!

你必须urlencode()(docs)你在URL中使用的每个字符串;特别是你的消息:

$url = "http://www...com/api/sendsms.php?...&message=".urlencode($message)."&numbers=".urlencode($phoneNumber)."&...";

这会将"#"替换为 "%23" 。您已经通过将其替换为 "%20" 来清理代码中的空格,但这不是 url 字符串中唯一的特殊字符。因此,不要尝试手动执行此操作,urlencode()为您完成这项工作。

相关文章: