【发布时间】:2021-07-27 04:23:02
【问题描述】:
我正在建立一个系统,用于使用 PHP + MYSQL 和连接 API 向 whatsapp 发送消息。
我创建了一个提交表单,我在其中定义了一些模板,例如 {{name}}、{{expiration}}、 >{{codeticket}} 我打算将这些嵌入在我的消息中的模板发送给在 MYSQL 用户表中注册的所有用户。
目标如下: 当向所有用户发送消息时,每个用户都会收到以下消息: 例如:*
你好{{name}}你好吗?以下是您的银行单据代码: {{codeticket}} 记住您需要按天付款 {{expiration}} 此致,数字支持。 *
为此,我创建了一个脚本,用于查询用户表中“活动”字段等于“0”且 ID 等于“3”的所有字段。
因此,我在 PHP 中创建了一个“while”循环以从 users 表中获取所有字段,然后将每个字段的变量放入 url 中以发送到 whatsapp。
不使用模板的代码示例如下所示:
$sql = "SELECT * FROM usuarios WHERE id_ma ='". $id_ma ."' AND ativo = '".$ativo."'";
$qr = mysqli_query($conn, $sql);
$cont = mysqli_num_rows($qr);
if ($cont > 0) {
//I make a loop with while to go through the bank and set each defaulting user.
while ($txt = mysqli_fetch_array($qr, MYSQLI_ASSOC)) {
$tags = array();
$tags['name'] = $txt['nome'];
$tags['expiration'] = $txt['data_expira'];
$tags['linkgrupo'] = $txt['link_canal'];
$tags['product'] = $txt['nome_prod'];
$tags['codebillet'] = $txt['codigo_boleto'];
$tags['link_billet'] = $txt['link_boleto'];
foreach ($tags as $key => $valor) {
$conteudo_msg = str_replace('{{'.$key.'}}', $valor, $conteudo_msg);
}
//Here I take the query data and send the msg to users via APi Whatsapp
$url = "https://send.zaprapido.com.br/api/send.php?token=".$apiwhatsapp."&no=55".$txt["celular"]."&text=".urlencode(utf8_decode($txt['nome']))."+";
//echo $url."<br />";
//Vamos chamar a msg via Json pela APi
$result = file_get_contents($url, true);
$data = json_decode(file_get_contents($url));
问题在于,当添加变量 $content_msg 以使用模板时,脚本只获取每个字段的第一行并将其发送到数据库中注册的所有号码。
【问题讨论】:
-
当您在 url 中包含
$content_msg变量时,您是否将其转义? -
你能在你的例子中显示你将在哪里添加
$content_msg吗? -
你为什么使用
utf8_decode?你知道这个函数的用途吗? -
您好,我正在使用 utf8_decode,因为我的数据库存在编码问题。当我以它注册的奇怪字符的形式输入任何内容时,utf8_decode 正是为了防止这些字符被发送给最终用户。
标签: php mysql str-replace