Skip to main content
The First Step
November 6, 2019
Solved

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.

The First Step
November 8, 2019

That's perfect, thanks very much.

2 replies

PhongVu
Community Manager
Community Manager
November 7, 2019

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 RingCentralSDKSDK($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."
");
    $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
");
        $delayInterval = ($limitWindow / $limit);
    }else{
        $delayInterval = ($limitWindow / $limitRemaining);
    }
    sleep($delayInterval);
    $index++;
    sendFaxRegularInterval();
  }else{
    print_("Done
");
  }
}

$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."
");
      $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
");
          $now = time();
          $diff = $now - $startTime;
          $delayInterval = ($limitWindow - $diff);
          $startTime = $now + $delayInterval;
      }
      sleep($delayInterval);
      $index++;
      sendFaxAsFastAsPossible();
  }else{
      print_r("Done
");
  }
}

Hope this helps!

Tobias62323551004The First StepAuthorAnswer
The First Step
November 8, 2019

That's perfect, thanks very much.