open-wa / wa-automate-docker

💬 🤖 The easiest way to turn your WhatsApp into an API. Be sure to 🌟 this repository for updates!
106 stars 31 forks source link

Need PHP Example on /sendText #17

Closed honggianto closed 3 years ago

honggianto commented 3 years ago

Hi all,

I tried to /sendText using PHP script below :

<?php
$curl = curl_init();
$data = [
    'to' => '000000000000@c.us',
    'content' => 'Hello World!',
];

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_URL, "http://localhost:8002/sendText");
$result = curl_exec($curl);
curl_close($curl);
print_r($result);
?>

and the result always :

{"success":true,"response":"Message missing"}

What am i missed here ? Help me please... Thanks before.

smashah commented 3 years ago

hi, try this

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://localhost:8002/sendText",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
    "args": {
        "to": "00000000000@c.us or 00000000000-111111111@g.us",
        "content": "Hello World!"
    }
}',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
honggianto commented 3 years ago

Awesome! It works! Thank you so much @smashah ...

Here's another script that worked too in PHP version 8 :

<?php
$url = 'http://localhost:8002/sendText';
$data = array("args" => array ("to" => "000000000@c.us" ,"content" => "Wonderful!" ) );

$postdata = json_encode($data);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
print_r ($result);