-1

我在通过 WhatsApp PHP 客户端发送消息时遇到问题。详情如下:

错误

由于长度,这里给出: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;
    }
4

1 回答 1

-1

要接收消息,您需要绑定 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 命令行
于 2016-04-12T21:27:05.843 回答