News & Announcements User Community Developer Community

Welcome to the RingCentral Community

Please note the community is currently under maintenance and is read-only.

Search
Make sure to review our Terms of Use and Community Guidelines.
  Please note the community is currently under maintenance and is read-only.
Home » Developers
Bulk faxing app PHP
Tags: fax
Nov 6, 2019 at 9:00am   •   2 replies  •  0 likes
Tobias King

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.

2 Answers
answered on Nov 7, 2019 at 11:41am  

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!


 0
answered on Nov 8, 2019 at 7:09am  

That's perfect, thanks very much.


 0



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

PRODUCTS
RingEX
Message
Video
Phone
OPEN ECOSYSTEM
Developer Platform
APIs
Integrated Apps
App Gallery
Developer support
Games and rewards

RESOURCES
Resource center
Blog
Product Releases
Accessibility
QUICK LINKS
App Download
RingCentral App login
Admin Portal Login
Contact Sales
© 1999-2024 RingCentral, Inc. All rights reserved. Legal Privacy Notice Site Map Contact Us