question

Nate Breeden avatar image
Nate Breeden asked Phong Vu answered

PHP SDK Issue - Uncaught Error: Call to a member function post() on null

Probably a dumb issue, I'm used to C# and Python but not PHP, trying to move an automation I have over to PHP.


Not sure what I'm doing wrong here, but every time I run the below code, then test it, I receive "Uncaught Error: Call to a member function post() on null in /path/to/myapp.php"


<?php
require('vendor/autoload.php');
use RingCentral\SDK\Subscription\Events\NotificationEvent;
use RingCentral\SDK\Subscription\Subscription;

$RINGCENTRAL_CLIENTID = 'sensored';
$RINGCENTRAL_CLIENTSECRET = 'sensored';
$RINGCENTRAL_SERVER = 'https://platform.ringcentral.com';

$RINGCENTRAL_USERNAME = "sensored";
$RINGCENTRAL_PASSWORD = 'sensored';
$RINGCENTRAL_EXTENSION = "sensored";

$rcsdk = new RingCentral\SDK\SDK($RINGCENTRAL_CLIENTID, $RINGCENTRAL_CLIENTSECRET, $RINGCENTRAL_SERVER);

$platform = $rcsdk->platform();
$platform->login($RINGCENTRAL_USERNAME, $RINGCENTRAL_EXTENSION, $RINGCENTRAL_PASSWORD);

$subscription = $rcsdk->createSubscription();
$subscription->addEvents(array('/restapi/v1.0/account/~/telephony/sessions'));
$subscription->addListener(Subscription::EVENT_NOTIFICATION, function (NotificationEvent $e) {
    if(($e->payload()['body']["parties"][0]['to']['phoneNumber'] == 'myextension') or ($e->payload()['body']["parties"][0]['to']['phoneNumber'] == 'myDID') and ($e->payload()['body']['parties'][0]['status']['code'] == 'Setup'))
    {
        $accountId = '~';
        $telephonySessionId = $e->payload()['body']['telephonySessionId'];
        $partyId = $e->payload()['body']['parties'][0]['id'];
        $body = array(
            'extensionNumber' => 'sensored'
        );

        $r = $platform->post("/restapi/v1.0/account/{$accountId}/telephony/sessions/{$telephonySessionId}/parties/{$partyId}/forward", $body);
        echo json_decode($r);
    }
});
$subscription->setKeepPolling(true);
$subscription->register();
?>


I've verified that the variables are all good, URL is pulled properly, and that I'm sending the required params as seen here: https://developers.ringcentral.com/api-reference/Call-Control/transferCallParty


Is it because I'm calling this from inside of the event notification?


Any help would be appreciated, especially on the proper way of doing this


Full error:

PHP Warning:  Undefined variable $platform in myapp.php on line 31
PHP Fatal error:  Uncaught Error: Call to a member function post() on null in myapp.php:31
Stack trace:
#0 /sensored/vendor/symfony/event-dispatcher/EventDispatcher.php(184): {closure}()
#1 /sensored/vendor/symfony/event-dispatcher/EventDispatcher.php(46): Symfony\Component\EventDispatcher\EventDispatcher->doDispatch()
#2 /sensored/vendor/ringcentral/ringcentral-php/src/Subscription/Subscription.php(276): Symfony\Component\EventDispatcher\EventDispatcher->dispatch()
#3 /sensored/vendor/pubnub/pubnub/composer/lib/Pubnub/Pubnub.php(565): RingCentral\SDK\Subscription\Subscription->notify()
#4 /sensored/vendor/pubnub/pubnub/composer/lib/Pubnub/Pubnub.php(375): Pubnub\Pubnub->_subscribe()
#5 /sensored/vendor/ringcentral/ringcentral-php/src/Subscription/Subscription.php(252): Pubnub\Pubnub->subscribe()
#6 /sensored/vendor/ringcentral/ringcentral-php/src/Subscription/Subscription.php(129): RingCentral\SDK\Subscription\Subscription->subscribeAtPubnub()
#7 /sensored/vendor/ringcentral/ringcentral-php/src/Subscription/Subscription.php(86): RingCentral\SDK\Subscription\Subscription->subscribe()
#8 /sensored/myapp.php(36): RingCentral\SDK\Subscription\Subscription->register()
#9 {main}
  thrown in /sensored/myapp.php on line 31

(31 is:)

$r = $platform->post("/restapi/v1.0/account/{$accountId}/telephony/sessions/{$telephonySessionId}/parties/{$partyId}/forward", $body);
sdk
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

Phong Vu avatar image
Phong Vu answered

If you want to use a global variable inside a PHP function, you have to redeclare it with the global keyword inside the function before using it. So try this:

$subscription->addListener(Subscription::EVENT_NOTIFICATION, function (NotificationEvent $e) {
    global $platform;
    if(($e->payload()['body']["parties"][0]['to']['phoneNumber'] == 'myextension') or ($e->payload()['body']["parties"][0]['to']['phoneNumber'] == 'myDID') and ($e->payload()['body']['parties'][0]['status']['code'] == 'Setup'))
    {
        $accountId = '~';
        $telephonySessionId = $e->payload()['body']['telephonySessionId'];
        $partyId = $e->payload()['body']['parties'][0]['id'];
        $body = array(
            'extensionNumber' => 'sensored'
        );
 
        $r = $platform->post("/restapi/v1.0/account/{$accountId}/telephony/sessions/{$telephonySessionId}/parties/{$partyId}/forward", $body);
        echo json_decode($r);
    }
});
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Developer sandbox tools

Using the RingCentral Phone for Desktop, you can dial or receive test calls, send and receive test SMS or Fax messages in your sandbox environment.

Download RingCentral Phone for Desktop:

Tip: switch to the "sandbox mode" before logging in the app:

  • On MacOS: press "fn + command + f2" keys
  • On Windows: press "Ctrl + F2" keys