Question

Problem with Fax API service

  • 10 November 2017
  • 3 replies
  • 1064 views

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 esteddirectoriesfilename.doc.

I'm using Windows 8 X64 bit and php version 5.6.25 if it is important.


3 replies

Userlevel 1
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
I've rewritten my code:

function SendFax($phone, $filename) {

$sdk = new RingCentralSDKSDK('appKey', 'appSecret', RingCentralSDKSDK::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 folderwithscriptinvendor ingcentral ingcentral-phpsrcHttpClient.php:43 Stack trace: #0 folderwithscriptinvendor ingcentral ingcentral-phpsrcPlatformPlatform.php(276): RingCentralSDKHttpClient->send(Object(GuzzleHttpPsr7Request)) #1 folderwithscriptuserman.php(26): RingCentralSDKPlatformPlatform->sendRequest(Object(GuzzleHttpPsr7Request)) #2 folderwithscriptuserman.php(35): SendFax('8778961829', 'test_log.txt') #3 {main} Next exception 'RingCentralSDKHttpApiException' with message 'In order to call this API endpoint, application needs to have [Faxes] permission' in folderwithscriptinvendor ingcentral ingcentral-phpsrcHttpClient.php:54 Stack trace: #0 folderwithscriptinvendor ingcentral ingcentral-phpsrcPlatformPlatform.php(276): RingCentralSDKHttpClient->send(Object(GuzzleHttpPs in folderwithscriptinvendor ingcentral ingcentral-phpsrcHttpClient.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.

Userlevel 1
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

Reply