question

Tobias King avatar image
Tobias King asked Phong Vu edited

Bulk faxing app PHP

Hi,
Not sure if I'm going about this the wrong way, but I'm trying to create a pretty basic bulk faxing app using PHP - open a CSV file with a list of phone numbers and send to all etc. The basic functionality of sending a fax works fine in my sandbox, but I'm struggling to find more info on what else I need to do, specifically related to rate limits and such. I should also ask if there is already an implementation of this somewhere I can just use - didn't find anything particularly useful on the RingCentral Apps or on Github.

I know there are rate limits of 10 on my 'heavy' call. Is this all faxes, or is this because I have a certain size of attachment? Either way, when I send, do I have to keep track of the limit, and once it hits 10 in 1 60 second period tell it to sleep for 59 seconds, or does the SDK do all of that and it will just continue sending when it's able to? If I do need to keep track of it, how do I get the info out of the response? In debugging, I have been able to see the info like X-Rate-Limit-Group etc, but can't figure out how to do it with code. I've tried things like $resp->_response (but _response is not available to be queried like this), $resp->getHeaders(), $resp -> getHeader('X-Rate-Limit-Limit').
$resp->json() only gives me the _jsonAsObject which does not include the _response object, so doesn't have the headers I need.

Given


$resp = $platform->sendRequest($request);

json_encode($resp);


gives me a blank array.


What am I doing wrong? Is there anything else I need to know?


Thanks in advance.

fax
1 |3000

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

Tobias King avatar image
Tobias King answered

That's perfect, thanks very much.

1 |3000

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

Phong Vu avatar image
Phong Vu answered Phong Vu edited

Here you go. I give you two options, choose one of those.

<?php
require('vendor/autoload.php');

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

$RINGCENTRAL_USERNAME = '';
$RINGCENTRAL_PASSWORD = '';
$RINGCENTRAL_EXTENSION = '';

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

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

$listOfPhoneNumber = [
  "16501234567","16501234567","16501234567","16501234567","16501234567",
  "16501234567","16501234567","16501234567","16501234567","16501234567",
  "16501234567","16501234567","16501234567","16501234567","16501234567",
  "16501234567","16501234567","16501234567","16501234567","16501234567",
  "16501234567","16501234567","16501234567","16501234567","16501234567",
  "16501234567","16501234567","16501234567","16501234567","16501234567",
  "16501234567","16501234567","16501234567","16501234567","16501234567"
];

$index = 0;
//sendFaxRegularInterval();
function sendFaxRegularInterval() {
  global $rcsdk;
  global $index;
  global $listOfPhoneNumber;
  if ($index < count($listOfPhoneNumber)){
    $request = $rcsdk->createMultipartBuilder()
                     ->setBody(array(
                         'to' => array(array('phoneNumber' => $listOfPhoneNumber[$index])),
                         'faxResolution' => 'High',
                     ))
                     ->add(fopen('test.jpg', 'r'))
                     ->request('/account/~/extension/~/fax');

    $resp = $rcsdk->platform()->sendRequest($request);
    print_r ("FAX sent. Message status: " . $resp->json()->messageStatus."\n");
    $jsonObj = $resp->response();
    $limit = strval($jsonObj->getHeaders()['X-Rate-Limit-Limit'][0]);
    $limitRemaining = strval($jsonObj->getHeaders()['X-Rate-Limit-Remaining'][0]);
    $limitWindow = strval($jsonObj->getHeaders()['X-Rate-Limit-Window'][0]);

    $delayInterval = 0;
    if ($limitRemaining == 0){
        print_r("no remaining. panelty\n");
        $delayInterval = ($limitWindow / $limit);
    }else{
        $delayInterval = ($limitWindow / $limitRemaining);
    }
    sleep($delayInterval);
    $index++;
    sendFaxRegularInterval();
  }else{
    print_("Done\n");
  }
}

$startTime = time();
sendFaxAsFastAsPossible();
function sendFaxAsFastAsPossible() {
  global $rcsdk;
  global $startTime;
  global $index;
  global $listOfPhoneNumber;

  if ($index < count($listOfPhoneNumber)){
      $request = $rcsdk->createMultipartBuilder()
                       ->setBody(array(
                           'to' => array(array('phoneNumber' => $listOfPhoneNumber[$index])),
                           'faxResolution' => 'High',
                       ))
                       ->add(fopen('test.jpg', 'r'))
                       ->request('/account/~/extension/~/fax');

      $resp = $rcsdk->platform()->sendRequest($request);
      print_r ("FAX sent. Message status: " . $resp->json()->messageStatus."\n");
      $jsonObj = $resp->response();
      $limit = strval($jsonObj->getHeaders()['X-Rate-Limit-Limit'][0]);
      $limitRemaining = strval($jsonObj->getHeaders()['X-Rate-Limit-Remaining'][0]);
      $limitWindow = strval($jsonObj->getHeaders()['X-Rate-Limit-Window'][0]);

      $delayInterval = 0;
      if ($limitRemaining == 0){
          print_r("no remaining. wait for next window\n");
          $now = time();
          $diff = $now - $startTime;
          $delayInterval = ($limitWindow - $diff);
          $startTime = $now + $delayInterval;
      }
      sleep($delayInterval);
      $index++;
      sendFaxAsFastAsPossible();
  }else{
      print_r("Done\n");
  }
}

Hope this helps!

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