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);
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); } });
A new Community is coming to RingCentral!
Posts are currently read-only as we transition into our new platform.We thank you for your patience
during this downtime.
Try Workflow Builder
Did you know you can easily automate tasks like responding to SMS, team messages, and more? Plus it's included with RingCentral Video and RingEX plans!Try RingCentral Workflow Builder