Problem with Fax API service

  • 1
  • Question
  • Updated 3 months ago
  • Answered
Hello, guys! I'm trying to send fax via Fax API and I've permanently got some strange error. I'm using PHP.
This is my code:

define('RING_CENTRAL_USERNAME', 'xxxxx');
define('RING_CENTRAL_PASSWORD', 'xxxxx');

function SendFax($recipient_number, $recipient_name='Recipient', $resolution='High', $sendtime=false, $attachment=false){

      if($resolution!='High' || $resolution!='Low') $resolution = 'High';
      if(!$sendtime) $sendtime = gmdate('d:m:y H:i');
      if(!$recipient_name) $recipient_name = 'Recipient';
      
      if(!RING_CENTRAL_USERNAME) return 'NO_RING_CENTRAL_USERNAME';
      if(!RING_CENTRAL_PASSWORD) return 'NO_RING_CENTRAL_PASSWORD';

      $data = array(
               'Username' => RING_CENTRAL_USERNAME,
               'Password' => RING_CENTRAL_PASSWORD,
               'Recipient' => $recipient_number.'|'.$recipient_name,
               'Resolution' => $resolution,
               'Sendtime' => $sendtime,
   'Coverpage' => '0');

      if($attachment) $data['Attachment'] = '@'.realpath($attachment);

  
      $ch = curl_init();
         
      curl_setopt($ch, CURLOPT_URL, 'https://service.ringcentral.com/faxapi.asp');
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      
         // optional for debugging
      curl_setopt($ch, CURLOPT_VERBOSE, 1);
      curl_setopt($ch, CURLOPT_HEADER, 1);
      curl_setopt($ch, CURLINFO_HEADER_OUT, true);

         //SSL Settings
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
      
         // Post data to send
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

      $result = curl_exec($ch);
      curl_close($ch);
      return $result;
   }

$res = SendFax($phone, $ref - $patid, 'High', False, $filepath);


                                        And I get result ($res):

HTTP/1.1 100 Continue

HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Fri, 10 Nov 2017 01:05:02 GMT
Content-Length: 1
Connection: keep-alive
Set-Cookie: RCRoutingInfo=B:uOC8cy7hEWo33JgK7DLPkAwlI+UrhiIIHYO+ZOIeDQjRBXf2/RhoGEvBtSJ/nkrG; Path=/
Set-Cookie: SCS_ROUTE=50bafe740f8d71bfa84d0a51df96494f; Path=/
x-frame-options: sameorigin
x-xss-protection: 1; mode=block
Set-Cookie: RCRoutingAdvice=SJC01P05JWS06; Path=/
Set-Cookie: JSESSIONID=0CjTRrSPEpKrPENi9A5WPDn5; Path=/

4


I've read that code 4 is "No fax data specified". That's - weird because I've checked - this file exists and it is in doc format.
$data['Attachment'] looks as @C:\Some\nested\directories\filename.doc.
I'm using Windows 8 X64 bit and php version 5.6.25 if it is important.
Photo of Alexey  Megley

Alexey Megley

  • 112 Points 100 badge 2x thumb

Posted 3 months ago

  • 1
Photo of Phong Vu

Phong Vu, Devangelist

  • 1,520 Points 1k badge 2x thumb
Hi Alexey,

Any reason why you don't use the PHP SDK (https://github.com/ringcentral)? Using the SDK to call RingCentral APIs will be much easier and we can help you faster to tackle problems. Please check it out.

Meanwhile, let me how how do you set the content type (multipart/mixed), boundary etc. Please check the Fax POST API reference for more details. I am not sure about the URL you used either. It does not look familiar with me and I don't see how you specify the account id and the extension id.

+ Phong
Photo of Alexey  Megley

Alexey Megley

  • 112 Points 100 badge 2x thumb
I've rewritten my code:

function SendFax($phone, $filename) {

$sdk = new RingCentral\SDK\SDK('appKey', 'appSecret', RingCentral\SDK\SDK::SERVER_SANDBOX);
    $sdk->platform()->loggedIn();
    $sdk->platform()->login('+13133292659', 'ext', 'password');

$request = $sdk->createMultipartBuilder()
->setBody(array(
'to' => array(array('phoneNumber' => $phone),),
'faxResolution' => 'High',))
->add('Plain Text', 'file.txt')
->add(fopen($filename, 'r'))
->request('/account/~/extension/~/fax');
 
$response = $sdk->platform()->sendRequest($request);
return $response;
}

Now I get this error:

PHP Fatal error:  Uncaught exception 'Exception' with message 'Response has unsuccessful status' in folder\\with\\script\\bin\\vendor\\ringcentral\\ringcentral-php\\src\\Http\\Client.php:43\nStack trace:\n#0 folder\\with\\script\\bin\\vendor\\ringcentral\\ringcentral-php\\src\\Platform\\Platform.php(276): RingCentral\\SDK\\Http\\Client->send(Object(GuzzleHttp\\Psr7\\Request))\n#1 folder\\with\\script\\userman.php(26): RingCentral\\SDK\\Platform\\Platform->sendRequest(Object(GuzzleHttp\\Psr7\\Request))\n#2 folder\\with\\script\\userman.php(35): SendFax('8778961829', 'test_log.txt')\n#3 {main}\n\nNext exception 'RingCentral\\SDK\\Http\\ApiException' with message 'In order to call this API endpoint, application needs to have [Faxes] permission' in folder\\with\\script\\bin\\vendor\\ringcentral\\ringcentral-php\\src\\Http\\Client.php:54\nStack trace:\n#0 folder\\with\\script\\bin\\vendor\\ringcentral\\ringcentral-php\\src\\Platform\\Platform.php(276): RingCentral\\SDK\\Http\\Client->send(Object(GuzzleHttp\\Ps in folder\\with\\script\\bin\\vendor\\ringcentral\\ringcentral-php\\src\\Http\\Client.php on line 54, referer <siteURL>.

I use appKey = Client ID and appSecret = Client Secret from application credentials, username, extension and password from user account credentials. File 'test_log.txt' exists and is located in directory with php file, so I don't know which permissions the application needs.


Thanks for helping me.
Photo of Phong Vu

Phong Vu, Devangelist

  • 1,520 Points 1k badge 2x thumb
There are couple things you need to change and retest.

1. It seems your RingCentral app does not have the Faxes permission. Login your RC dev account and open the app then add the Faxes permission.
2. You passed the $phone and $filename to your SendFax() function, but you hardcode the "file.txt" file to send? Make sure the file exists and with full path.

Let me know.

+ Phong
Photo of Alexey  Megley

Alexey Megley

  • 112 Points 100 badge 2x thumb
Yes, exactly, problem was in adding Fax permissions to the application page. Thank you!!!