messagebird / php-rest-api

This repository contains the open source PHP client for MessageBird's REST API.
https://developers.messagebird.com/
BSD 2-Clause "Simplified" License
158 stars 95 forks source link

HSM Start: Invalid JSON #129

Closed pboschma closed 1 year ago

pboschma commented 4 years ago

Hello.

We've tried using the SDK to send a WhatsApp HSL message. To do this we've used the create-hsm example from the example folder. However, it will throw an exception JSON is not a valid format. Then I got looking and found out that this issue is already known for the conversation API. We're using start and not create (Which was the possible fix in the other issue) #95

When we use create instead of start the error gets even funnier. Got an invalid JSON response from the server. I have a feeling that the SDK is not running things correctly, but it can be our stupidity too. I've attached the code.

       $param = new \MessageBird\Objects\Conversation\HSM\Params();
        $param->default = $this->body;

        $language = new \MessageBird\Objects\Conversation\HSM\Language();
        $language->policy = \MessageBird\Objects\Conversation\HSM\Language::DETERMINISTIC_POLICY;
        $language->code = 'EN';

        $hsm = new \MessageBird\Objects\Conversation\HSM\Message();
        $hsm->templateName = $this->template;
        $hsm->namespace = $_ENV['WA_NAMESPACE'];
        $hsm->params = array($param);
        $hsm->language = $language;

        $content = new \MessageBird\Objects\Conversation\Content();
        $content->hsm = $hsm;

        $message = new \MessageBird\Objects\Conversation\Message();
        $message->channelId = $this->channel;
        $message->content = $content;
        $message->to = $this->recipients[0];
        $message->type = 'hsm';

        $conversation = $this->client->conversations->start($message);
stgoos commented 4 years ago

I also got the "Got an invalid JSON response from the server" error. I'm trying to replace an old solution we have in place in the company, which is still running on the old legacy API (on PHP 5.6.40), with a new version of the solution based on the REST API but so far I'm hitting this blocker...

The new version I'm trying to set up is running on a Windows 2012 R2 x64 server with IIS 8.5 with PHP 7.4.10 on it. Because of the company network I need to define the proxy to channel the traffic directly outside, just like with the old legacy API. That all seems to work well because without that I run into timeout issues.

My code to check the credit balance, to see if the REST API is working fine, is the following:

<?php 

require_once('vendor/autoload.php'); 

define('API_KEY', 'the-api-key-goes-here');
define('COMPANY_PROXY', '10.20.30.40:8080');

$httpClient = new \MessageBird\Common\HttpClient( \MessageBird\Client::ENDPOINT, 10, 10 );
$httpClient->addHttpOption( CURLOPT_PROXY, COMPANY_PROXY );
$client = new \MessageBird\Client( API_KEY, $httpClient );

/**
 * balance-view
 */
try {
    $balance = $client->balance->read();
    echo "Balance = ".$balance; 
} catch( \MessageBird\Exceptions\AuthenticateException $e ) {
    // That means that your accessKey is unknown
    echo 'Wrong Login'; 
} catch( \Exception $e ) {
    echo "<pre><code>". var_export( $e, true ) ."</code></pre>";
}

The full dump of \Exception $e is:

MessageBird\Exceptions\ServerException::__set_state(array(
   'message' => 'Got an invalid JSON response from the server.',
   'string' => '',
   'code' => 0,
   'file' => 'C:\www\test\vendor\messagebird\php-rest-api\src\MessageBird\Resources\Base.php',
   'line' => 162,
   'trace' => 
  array (
    0 => 
    array (
      'file' => 'C:\www\test\vendor\messagebird\php-rest-api\src\MessageBird\Resources\Base.php',
      'line' => 126,
      'function' => 'processRequest',
      'class' => 'MessageBird\Resources\Base',
      'type' => '->',
    ),
    1 => 
    array (
      'file' => 'C:\www\test\balance-view.php',
      'line' => 16,
      'function' => 'read',
      'class' => 'MessageBird\Resources\Base',
      'type' => '->',
    ),
  ),
   'previous' => NULL,
))

Line 16: $balance = $client->balance->read();

Like you I'm stuck at the moment as I also don't see other hints in the PHP logs on the server.

P.S. If these 2 topics are unrelated then I'm happy to create a new topic for it.

reandimo commented 3 years ago

Hello.

We've tried using the SDK to send a WhatsApp HSL message. To do this we've used the create-hsm example from the example folder. However, it will throw an exception JSON is not a valid format. Then I got looking and found out that this issue is already known for the conversation API. We're using start and not create (Which was the possible fix in the other issue) #95

When we use create instead of start the error gets even funnier. Got an invalid JSON response from the server. I have a feeling that the SDK is not running things correctly, but it can be our stupidity too. I've attached the code.

       $param = new \MessageBird\Objects\Conversation\HSM\Params();
        $param->default = $this->body;

        $language = new \MessageBird\Objects\Conversation\HSM\Language();
        $language->policy = \MessageBird\Objects\Conversation\HSM\Language::DETERMINISTIC_POLICY;
        $language->code = 'EN';

        $hsm = new \MessageBird\Objects\Conversation\HSM\Message();
        $hsm->templateName = $this->template;
        $hsm->namespace = $_ENV['WA_NAMESPACE'];
        $hsm->params = array($param);
        $hsm->language = $language;

        $content = new \MessageBird\Objects\Conversation\Content();
        $content->hsm = $hsm;

        $message = new \MessageBird\Objects\Conversation\Message();
        $message->channelId = $this->channel;
        $message->content = $content;
        $message->to = $this->recipients[0];
        $message->type = 'hsm';

        $conversation = $this->client->conversations->start($message);

Maybe is a little late, but could help someone.

Check the parameters $hsm->params you're sending to template are all type string. That worked for my case.

dennisvdvliet commented 1 year ago

Closing this as a part of issue cleanup.