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/