alexandr-mironov / php8-smpp

SMPP Client (v 3.4) on PHP8
7 stars 8 forks source link

Error "Failed to read reply to command: 0x4" #8

Closed sm-unipesa closed 1 year ago

sm-unipesa commented 1 year ago

Hi,

Thank you for your fantastic work with this library! I started using PHP8 with php8-smpp and received the error "Failed to read a reply to command: 0x4" to send SMS regardless of the value $smsNullTerminateOctetstrings.

Do you have any idea how to fix that?

Many thanks in advance.

alexandr-mironov commented 1 year ago

Hi, can you describe when you get this error, more details or code example, please?

sm-unipesa commented 1 year ago

I use the code from your documentation. Receiving this error when sending SMS: $response = $this->smppClient->sendSMS($this->from, $this->to, $message, null, Smpp::DATA_CODING_UCS2);

alexandr-mironov commented 1 year ago

try to reproduce your problem, but cannot get same error, which mode you use? in my example used tranciever mode, but your SMPP gate may not support this mode

sm-unipesa commented 1 year ago

I tried both - bindTransmitter() and bindTransceiver(). Result is the same - "Failed to read reply to command: 0x4"

alexandr-mironov commented 1 year ago

Can you provide debug output and SMPP gate which you use to i try to reproduce your error?

sm-unipesa commented 1 year ago

Sorry for the late reply. Can you help me to inject the logger into your library? I use Laravel with Monolog in my app.

alexandr-mironov commented 1 year ago

Hello! looks like you should add it to socket constructor for example:

<?php

declare(strict_types=1);

namespace app\components\Sms;

use Exception;
use smpp\{Address, Client as SmppClient, Smpp, transport\Socket};

class SmsBuilder
{
    /** @var string 11 chars limit */
    public const DEFAULT_SENDER = 'example';

    protected Socket $transport;

    protected SmppClient $smppClient;

    protected bool $debug = false;

    protected Address $from;

    protected Address $to;

    protected string $login;

    protected string $password;

    /**
     * SmsBuilder constructor.
     *
     * @param string $address SMSC IP
     * @param int $port SMSC port
     * @param string $login
     * @param string $password
     * @param int $timeout timeout of reading PDU in milliseconds
     * @param bool $debug - debug flag when true output additional info
     */
    public function __construct(
        string $address,
        int $port,
        string $login,
        string $password,
        int $timeout = 10000,
        bool $debug = false, 
        LoggerInterface $logger
    ) {
       // place to add your logger to Socket constructor
        $this->transport = new Socket([$address], $port, false, $logger);
        // Activate binary hex-output of server interaction
        $this->transport->debug = $debug;
        $this->transport->setRecvTimeout($timeout);
        $this->smppClient = new SmppClient($this->transport);

        $this->login = $login;
        $this->password = $password;

        $this->from = new Address(self::DEFAULT_SENDER, SMPP::TON_ALPHANUMERIC);
    }

    /**
     * @param string $sender
     * @param int $ton
     *
     * @return $this
     * @throws Exception
     */
    public function setSender(string $sender, int $ton): SmsBuilder
    {
        return $this->setAddress($sender, 'from', $ton);
    }

    /**
     * @param string $address
     * @param string $type
     * @param int $ton
     * @param int $npi
     *
     * @return $this
     * @throws Exception
     */
    protected function setAddress(
        string $address,
        string $type,
        int $ton = SMPP::TON_UNKNOWN,
        int $npi = SMPP::NPI_UNKNOWN
    ): SmsBuilder {
        // some example of data preparation
        if ($ton === SMPP::TON_INTERNATIONAL) {
            $npi = SMPP::NPI_E164;
        }
        $this->$type = new Address($address, $ton, $npi);

        return $this;
    }

    /**
     * @param string $address
     * @param int $ton
     *
     * @return $this
     * @throws Exception
     */
    public function setRecipient(string $address, int $ton): SmsBuilder
    {
        return $this->setAddress($address, 'to', $ton);
    }

    /**
     * @param string $message
     *
     * @throws Exception
     */
    public function sendMessage(string $message): void
    {
        $this->transport->open();
        $this->smppClient->bindTransceiver($this->login, $this->password);
        // strongly recommend use SMPP::DATA_CODING_UCS2 as default encoding in project to prevent problems with non latin symbols
        $this->smppClient->sendSMS($this->from, $this->to, $message, null, SMPP::DATA_CODING_UCS2);
        $this->smppClient->close();
    }
}

don't forget to set $debug to true)

alexandr-mironov commented 1 year ago
<?php
// some your client code
// true after 10000 its a debug, $logger is your Monolog instance
(new your_namespace\SmsBuilder('192.168.1.1', '2776', 'your_login', 'your_password', 10000, true, $logger))
    ->setRecipient('79000000000', \smpp\SMPP::TON_INTERNATIONAL) //msisdn of recipient
    ->sendMessage('Тестовое сообщение на русском and @noth3r$Ymb0ls');