PHP WhatsApp Client — Невозможно получить сообщения

У меня проблема с отправкой сообщений через PHP-клиент WhatsApp. Подробности приведены ниже:

ошибка

Из-за длины это дано здесь: http://pastie.org/10794465
« `

Журнал отладки

http://pastie.org/10794474

Код

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
set_time_limit(10);
var_dump(extension_loaded('curve25519'));
var_dump( extension_loaded('protobuf'));
//require_once __DIR__.'../vendor/whatsapp/autoload.php';
date_default_timezone_set('Europe/Madrid');
//require_once __DIR__.'../vendor/whatsapp/chat-api/src/whatsprot.class.php';
require_once 'vendor/whatsapp/chat-api/src/whatsprot.class.php';
require_once 'vendor/whatsapp/chat-api/src/events/MyEvents.php';
//require_once __DIR__.'/../src//events/MyEvents.php';

$username = '92xxxxxxxxx';                      // Telephone number including the country code without '+' or '00'.
$password = 't7+YzhqpUd8P7LgeU9NdttaIpc4=';     // Use registerTool.php or exampleRegister.php to obtain your password
$nickname = 'ADD Agent';                          // This is the username (or nickname) displayed by WhatsApp clients.
$target = "92xxxxxxxxx";                   // Destination telephone number including the country code without '+' or '00'.
$target = "92xxxxxxxxx";                   // Destination telephone number including the country code without '+' or '00'.
$debug = true;                                           // Set this to true, to see debug mode.

echo "[] Logging in as '$nickname' ($username)\n";
//Create the whatsapp object and setup a connection.
$w = new WhatsProt($username, $nickname, $debug,true,__DIR__.'/wadata/');
$events = new MyEvents($w);
$events->setEventsToListenFor($events->activeEvents);

$w->connect();
// Now loginWithPassword function sends Nickname and (Available) Presence
$w->loginWithPassword($password);
$w->sendMessage($target, 'Salam kia haal hain?!');
echo "<b>Message Sent to $target</b>";

echo "<br>Getting message<br>";
$w->pollMessage();
$msgs = $w->GetMessages();
foreach ($msgs as $m) {
var_dump($m);
}

В MyEvents.php

public function onGetMessage( $mynumber, $from, $id, $type, $time, $name, $body )
{
echo "<br>Message Got from $name:\n$body\n\n<br>"; // NOT being fired.
exit;
}

0

Решение

Чтобы получить сообщение, вам нужно связать onGetMessage и вызвать pollMessage в цикле

while (1) {
$w->pollMessage();
}

Проверьте этот пример завершения:

<?php

//set_time_limit(10);
require_once __DIR__.'/../src/whatsprot.class.php';
require_once __DIR__.'/../src//events/MyEvents.php';

//Change to your time zone
date_default_timezone_set('Europe/Madrid');

//######### DO NOT COMMIT THIS FILE WITH YOUR CREDENTIALS ###########
///////////////////////CONFIGURATION///////////////////////
//////////////////////////////////////////////////////////
$username = '*************';                      // Telephone number including the country code without '+' or '00'.
$password = '*************';     // Use registerTool.php or exampleRegister.php to obtain your password
$nickname = 'LuisN';                          // This is the username (or nickname) displayed by WhatsApp clients.
$target = "************";                   // Destination telephone number including the country code without '+' or '00'.
$debug = false;                                           // Set this to true, to see debug mode.
///////////////////////////////////////////////////////////

function onPresenceAvailable($username, $from)
{
$dFrom = str_replace(['@s.whatsapp.net', '@g.us'], '', $from);
echo "<$dFrom is online>\n\n";
}

function onPresenceUnavailable($username, $from, $last)
{
$dFrom = str_replace(['@s.whatsapp.net', '@g.us'], '', $from);
echo "<$dFrom is offline> Last seen: $last seconds\n\n";
}
function onGetMessage($mynumber, $from, $id, $type, $time, $name, $body){
echo sprintf("Message from %s: [%s]\r\n",$from,$body);
}
echo "[] Logging in as '$nickname' ($username)\n";
// Create the whatsapp object and setup a connection.
$w = new WhatsProt($username, $nickname, $debug);
$w->connect();

// Now loginWithPassword function sends Nickname and (Available) Presence
$w->loginWithPassword($password);
$w->sendGetServerProperties();
$w->sendGetGroups();
$w->sendGetBroadcastLists();
// Set the profile picture
//$w->sendSetProfilePicture(Constants::PICTURES_FOLDER . '/314484_300x300.jpg');
$w->sendStatusUpdate("La vida es un carnaval \xF0\x9F\x8E\xB6");
// Synchronizes contacts with the server, very important to avoid bans
$w->sendSync([$target]);
// Print when the user goes online/offline (you need to bind a function to the event onPressence
// so the script knows what to do)
$w->eventManager()->bind('onPresenceAvailable', 'onPresenceAvailable');
$w->eventManager()->bind('onPresenceUnavailable', 'onPresenceUnavailable');
// Receives and processes messages, this includes decrypted
$w->eventManager()->bind('onGetMessage','onGetMessage');
echo "[*] Connected to WhatsApp\n\n";

$w->sendMessage($target, 'Guess the number :)');
$w->sendMessage($target, 'Sent from WhatsApi at '.date('H:i'));

while (1) {
$w->pollMessage();
}

PD: я проверил этот метод в 3 средах

  • php 5.5 Cli NTS VC11 для Windows 10
  • php 5.5 Cli NTS VC11 для Windows 7
  • PHP 5.5.9-1ubuntu4.14 Cli
-1

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]