【问题标题】:How to open a WhatsApp chat with a pre-filled message that spans on multiple lines from my website?如何使用跨越我网站多行的预填充消息打开 WhatsApp 聊天?
【发布时间】:2016-03-18 11:34:22
【问题描述】:

来自WhatsApp's FAQ section

WhatsApp 提供了一个自定义 URL 方案来与 WhatsApp 交互:

如果您有一个网站并想与 预填充消息,您可以使用我们的自定义 URL 方案来执行此操作。 打开 whatsapp://send?text= 后跟要发送的文本,将打开 WhatsApp,允许用户选择联系人,并预先填写输入 具有指定文本的字段。

以下是如何在您的网站上编写此内容的示例:

<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 android html ios whatsapp


    【解决方案1】:

    虽然urlencode($msg) 适用于大多数移动浏览器,但它在 android 设备上的 firefox 中使用+ 对空格进行编码,因此您的用户可能会看到类似Text+on+the+first+line 的文本。更好的解决方法是使用rawurlencode($msg) 以使其与所有浏览器兼容,因为它会强制根据 RFC 3986 格式对文本进行编码。

    【讨论】:

      【解决方案2】:

      &lt;a href=""&gt;&lt;/a&gt; 标签中的 URL 中使用的 PHP 换行符 \r\n 需要编码。 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'

      【讨论】:

        猜你喜欢
        • 2015-06-23
        • 2019-03-08
        • 1970-01-01
        • 1970-01-01
        • 2019-09-14
        • 1970-01-01
        • 2017-09-23
        • 1970-01-01
        • 2015-03-20
        相关资源
        最近更新 更多