【问题标题】:Send Images using Whatsapp Cloud Api使用 Whatsapp Cloud Api 发送图像
【发布时间】:2022-06-14 09:56:44
【问题描述】:

我正在尝试使用Whatsapp Cloud API 发送图像。使用PHP,我可以成功发送普通短信。

浏览文档时,“MEDIA_OBJECT_ID”是什么意思?举个例子就好了。

curl -X  POST \
 'https://graph.facebook.com/v13.0/FROM_PHONE_NUMBER_ID/messages' \
 -H 'Authorization: Bearer ACCESS_TOKEN' \
 -d '{
  "messaging_product": "whatsapp",
  "recipient_type": "individual",
  "to": "PHONE_NUMBER",
  "type": "image",
  "image": {
    "id" : "MEDIA_OBJECT_ID"
  }
}'

谢谢

【问题讨论】:

标签: php whatsapp


【解决方案1】:

您需要将媒体上传到https://graph.facebook.com/v13.0/FROM_PHONE_NUMBER_ID/media

响应将为您提供“MEDIA_OBJECT_ID”

改用图片链接

curl -X  POST \
 'https://graph.facebook.com/v13.0/FROM_PHONE_NUMBER_ID/messages' \
 -H 'Authorization: Bearer ACCESS_TOKEN' \
 -d '{
  "messaging_product": "whatsapp",
  "recipient_type": "individual",
  "to": "PHONE_NUMBER",
  "type": "image",
  "image": {
    "link" : "Image URL"
  }
}'

【讨论】:

    【解决方案2】:

    我建议您从 Developer Dashboard 下载 WhatsApp Cloud API Postman 合集,以备日后疑虑:

    1. 转到https://developers.facebook.com/apps/
    2. 选择您的 WhatsApp 应用程序 在左侧边栏中,转到 WhatsApp -> 入门 点击“在 Postman 中运行”按钮以打开完整的 API 示例集合

    顺便说一句,我建议您为 WhatsApp Cloud API 安装 PHP SDK

    compose require netflie/whatsapp-cloud-api
    

    【讨论】:

      【解决方案3】:

      首先,您需要将媒体文件上传到 WhatsApp 服务器,然后 WhatsApp 会回复 MEDIA_OBJECT_ID

      $target="/home/rishabh/uploads/myImage.png";
      $mime=mime_content_type('myImage.png')
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v13.0/FROM_PHONE_NUMBER_ID/media');
      
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($target));
      $headers = array();
      $headers[] = "Authorization:Bearer $YOUR_WHATSAPP_ACCESS_TOKEN";
      $headers[] = "Content-Type: $mime";
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
      $result = json_decode(curl_exec($ch), true);
      $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
      
      $MEDIA_OBJECT_ID = $result['media'][0]['id']; //MEDIA OBJECT ID
      

      现在,您将获得媒体对象 ID。您需要从 API 发送媒体

      $FileName="Caption Name or Image Name";
      $messageBody = [
                      "recipient_type" => "individual",
                      "to" => "$to_number",
                      "type" => "image",
                      "image" => [
                          "id" => $MEDIA_OBJECT_ID, // MEDIA OBJECT ID
                          "caption" => $FileName,
                      ]
                  ];
      
      $curl = curl_init();
          curl_setopt_array($curl, array(
              CURLOPT_URL => 'https://graph.facebook.com/v13.0/FROM_PHONE_NUMBER_ID/messages',
              CURLOPT_RETURNTRANSFER => true,
              CURLOPT_ENCODING => '',
              CURLOPT_MAXREDIRS => 10,
              CURLOPT_TIMEOUT => 0,
              CURLOPT_FOLLOWLOCATION => true,
              CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
              CURLOPT_CUSTOMREQUEST => 'POST',
              CURLOPT_POSTFIELDS => json_encode($messageBody),
              CURLOPT_HTTPHEADER => array(
                  "Authorization:Bearer $YOUR_WHATSAPP_ACCESS_TOKEN",
                  'Content-Type: application/json'
              ),
          ));
          $response = json_decode(curl_exec($curl), true);
          $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
          curl_close($curl);
      

      这是不同类型的媒体 JSON

       // for mp3
           $messageBody = [
                          "recipient_type" => "individual",
                          "to" => "$to_number",
                          "type" => "audio",
                          "audio" => [
                              "id" => $MEDIA_OBJECT_ID, // MEDIA OBJECT ID
                          ]
                      ];
          
          // for pdf, doc, apk etc
            $messageBody = [
                          "recipient_type" => "individual",
                          "to" => "$to_number",
                          "type" => "document",
                          "document" => [
                              "id" => $MEDIA_OBJECT_ID, // MEDIA OBJECT ID,
                              "caption" => $fileName,
                              "filename" => $fileName,
                          ]
                      ];
          
           // for mp4
           $messageBody = [
                          "recipient_type" => "individual",
                          "to" => "$to_number",
                          "type" => "video",
                          "video" => [
                             "id" => $MEDIA_OBJECT_ID, // MEDIA OBJECT ID,
                              "caption" => $media['fileName'],
                          ]
                      ];
          
          $RequestJSON = json_encode($messageBody)
      

      【讨论】:

        【解决方案4】:

        我试图点击上传媒体来获取媒体对象 ID,但我得到一个异常代码 1 和 500 系列的状态代码。

        解决办法是什么?

        【讨论】:

          猜你喜欢
          • 2015-09-27
          • 2022-06-21
          • 2022-12-29
          • 1970-01-01
          • 2015-11-02
          • 1970-01-01
          • 2022-07-13
          • 1970-01-01
          • 2020-01-04
          相关资源
          最近更新 更多