我具有集成的SMS网关,并使用curl功能发送SMS。在我发送的文本中,我有一个'@' symbol
,因为卷曲功能失败。
我的文字是
$smsbody = "A new user has logged in\nEmail: testemail@gmail.com";
下面是我的代码:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{
\"Text\": \"$smsbody\",
\"Number\": \"$tomobile\",
\"SenderId\": \"Example\",
\"DRNotifyUrl\": \"https://www.example.com\",
\"DRNotifyHttpMethod\": \"POST\",
\"Tool\": \"API\"
}");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Authorization: ".$smsauthor
));
$response = curl_exec($ch);
curl_close($ch);
如果我删除'@'符号,则可以成功发送消息。对于带有“ @”符号的消息,消息失败。 如何对符号“ @”进行编码,或者可以替代此问题?
答案 0 :(得分:2)
使用 curl_escape(资源$ ch,字符串$ str)。 Docs
此功能URL根据»RFC 3986对给定的字符串进行编码。
使用:
<?php
// Create a curl handle
$ch = curl_init();
// Escape a string used as a GET parameter
$location = curl_escape($ch, 'Hofbräuhaus / München');
// Result: Hofbr%C3%A4uhaus%20%2F%20M%C3%BCnchen
// Compose an URL with the escaped string
$url = "http://example.com/add_location.php?location={$location}";
// Result: http://example.com/add_location.php?location=Hofbr%C3%A4uhaus%20%2F%20M%C3%BCnchen
// Send HTTP request and close the handle
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl
_close($ch);
?>