1

I want to build a WhatsApp bot, for that, we are using Gupshup WhatsApp bot API, for integration they asked to give a callback URL, so created index.php in cPanel of one domain(https://sample_url/WhatsappBot/index.php), and gave the URL (https://sample_url/WhatsappBot/). As per their API documentation, they will pass a response to that URL, so I want to fetch that. Here is the remaining part of API documentation API documentation2, API documentation3, API documentation4. So I created one curl.php named file, that code is given below.

<?php
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'https://sample_url/WhatsappBot/');
  // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_POST, 1);

  $headers = array();
  $headers[] = 'Content-Type: application/json';

  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

  $result = curl_exec($ch);

  if (curl_errno($ch)) 
   {
      echo 'Error:' . curl_error($ch) ."\n";
   }
  else
   {
      $result_value = json_decode($result,true,JSON_PRETTY_PRINT);
      echo $result_value;
      // var_dump($result_value);   
   }
 curl_close($ch);
?>

they provided a collection of API for reference, but I am getting the wrong result. In their collection API have the result, Collection API result

but I am getting this, myresult

What is the wrong in this code? Can anyone please help me...

Anisha Krishnan
  • 21
  • 1
  • 10
  • Why are you trying to make a request to `https://sample_url/WhatsappBot/` here? That is not how a callback URL works - _their_ servers will make a request to that URL. What you need to do, inside that script, is take the received POST data, and do something with it. – CBroe May 02 '22 at 07:55

1 Answers1

3

Your Callback URL should contain a program that receive a POST data as JSON, Go ahead to decode the JSON data, using the data received, proceed with what ever logic you plan to execute.

//this should be in your call back URL index.php

$post_data_expected = file_get_contents("php://input");

$decoded_data = json_decode($post_data_expected, true);

You can your POSTMAN to always test your callback URL to see it behaves the way you expects.

  • Added this code in index.php. `` and tested in postman but nothing displayed. I just put this URL "https://sample_url/WhatsappBot/" and POST request. Is there anything additional I want to add in postman header or in code?? @Apollos Geofrey – Anisha Krishnan May 03 '22 at 17:25
  • take note: this line shouldn't be, $post_data_expected = file_get_contents("https://sample_url/WhatsappBot/"); instead : $post_data_expected = file_get_contents("php://input"); – Apollos Geofrey May 04 '22 at 12:08
  • did you install postman on your machine or trying this online? – Apollos Geofrey May 04 '22 at 12:10
  • edited the code like you said, ``. now also no result. I tested in postman, which I already installed in my computer. – Anisha Krishnan May 04 '22 at 15:49
  • well, i don't know how u are doing this. but instead of using use or – Apollos Geofrey May 05 '22 at 10:22
  • while using postman, make sure you are using the POST method, data should be posted as JSON as well as a valid URL (could be that of your local-host server as well eg. {http://localhost/index.php}). it should provide a feedback response which should be either an error or expected result. – Apollos Geofrey May 05 '22 at 10:27