Question

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

  • 26 April 2023
  • 1 reply
  • 222 views

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 RingCentralSDKSubscriptionEventsNotificationEvent;
use RingCentralSDKSubscriptionSubscription;

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

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

$rcsdk = new RingCentralSDKSDK($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): SymfonyComponentEventDispatcherEventDispatcher->doDispatch()
#2 /sensored/vendor/ringcentral/ringcentral-php/src/Subscription/Subscription.php(276): SymfonyComponentEventDispatcherEventDispatcher->dispatch()
#3 /sensored/vendor/pubnub/pubnub/composer/lib/Pubnub/Pubnub.php(565): RingCentralSDKSubscriptionSubscription->notify()
#4 /sensored/vendor/pubnub/pubnub/composer/lib/Pubnub/Pubnub.php(375): PubnubPubnub->_subscribe()
#5 /sensored/vendor/ringcentral/ringcentral-php/src/Subscription/Subscription.php(252): PubnubPubnub->subscribe()
#6 /sensored/vendor/ringcentral/ringcentral-php/src/Subscription/Subscription.php(129): RingCentralSDKSubscriptionSubscription->subscribeAtPubnub()
#7 /sensored/vendor/ringcentral/ringcentral-php/src/Subscription/Subscription.php(86): RingCentralSDKSubscriptionSubscription->subscribe()
#8 /sensored/myapp.php(36): RingCentralSDKSubscriptionSubscription->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);

1 reply

Userlevel 1

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);
    }
});

Reply