php - 如何打开 WhatsApp 聊天,其中包含来 self 网站的多行预填充消息?

标签 php android html ios whatsapp

来自 WhatsApp's FAQ section :

WhatsApp provides a custom URL scheme to interact with WhatsApp:

If you have a website and want to open a WhatsApp chat with a pre-filled message, you can use our custom URL scheme to do so. Opening whatsapp://send?text= followed by the text to send, will open WhatsApp, allow the user to choose a contact, and pre-fill the input field with the specified text.

Here is an example of how to write this on your website:

<a href="whatsapp://send?text=Hello%20World!">Hello, world!</a>

如果我想提到 pre-filled message 怎么办?像这样跨越多行:

Text on the first line
Text on the second line

Text on the third line
Text on the fourth line

我试图将文本放在 <a href=""></a> 中像这样的标签:

<a href="whatsapp://send?text=First line\r\nSecond line\r\n\r\nThird line\r\nFourth line">Share on WhatsApp</a>

但这不起作用,消息在 WhatsApp 中显示如下:

First lineSecond lineThird lineFourth line

最佳答案

PHP换行符\r\n<a href=""></a> 的 URL 中使用的标签需要编码。 urlencode() PHP 函数可用于执行此操作。该链接必须类似于以下内容才能正确打开带有跨多行的预填充消息的 WhatsApp 聊天:

<?php
    $msg = "First line\r\nSecond line\r\n\r\nThird line\r\nFourth line";
    $msg = str_replace("\r\n", urlencode("\r\n"), $msg); // note the double quotes

    echo "<a href='whatsapp://send?text=$msg'>Share on WhatsApp</a>";
?>

现在,如果有人在他的 Android 或 iOS 设备上浏览您的网站时单击该链接,WhatsApp 应用程序将打开以允许他选择联系人,并使用将跨越的指定文本预填充输入字段像这样的多行:

First line
Second line

Third line
Fourth line

请注意,必须使用双引号,即 "\r\n"不是 '\r\n' .

关于php - 如何打开 WhatsApp 聊天,其中包含来 self 网站的多行预填充消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34242442/

相关文章:

php - while 循环使用带复选框的表单 php 不根据数据库详细信息进行更新

android - OpenGL FRONT_AND_BACK

javascript - 在 PhoneGap 2.9 中使用 jQuery/JavaScript 单击按钮事件不执行任何操作

android - 谷歌地图不显示任何内容

javascript - 下拉列表在 IE 7 上打开/隐藏在另一个下拉列表后面

javascript - 根据属性显示/隐藏

php - 选择元素到 foreach Twig 模板中

php - SQLite 文件大小在删除表后仍然没有改变

javascript - Laravel Blade 模板更改为 vue 组件

html - FF 中的 Angular mat-tab 和 mat-table 滚动问题