question

er-express-fax541 avatar image
er-express-fax541 asked er-express-fax541 answered

cURL error when Integrating the PHP SDK on my project

Hi,


I am Using RingCentral Connect Platform PHP SDK (https://github.com/ringcentral/ringcentral-php) to send fax messages, I get a curl error when i run the script. I have a developer account and testing the authenticate and Fax message and it works fine.


Error details:

Warning: curl_close(): 28 is not a valid cURL handle resource in <path_to_project>\vendor\ringcentral\ringcentral-php\src\Http\Client.php on line 104    Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Invalid response string' in vendor\ringcentral\psr7\src\functions.php:478  Stack trace:  #0 <path_to_project>\vendor\ringcentral\ringcentral-php\src\Http\ApiResponse.php(56): RingCentral\Psr7\parse_response('HTTP/1.1 0 OK??')  #1 <path_to_project>\vendor\ringcentral\ringcentral-php\src\Http\Client.php(99): RingCentral\SDK\Http\ApiResponse->__construct(Object(RingCentral\Psr7\Request), false, 0)  #2 <path_to_project>\vendor\ringcentral\ringcentral-php\src\Http\Client.php(25): RingCentral\SDK\Http\Client->loadResponse(Object(RingCentral\Psr7\Request))  #3 <path_to_project>\vendor\ringcentral\ringcentral-php\src\Platform\Platform.php(214): RingCentral\SDK\Http\Client->send(Object(RingCentral\Psr7\Request))  #4 <path_to_project>\vendor\ringcentral\ringcentral-php\src\Platform\Platform.php(301): RingCentral\SDK\Platform\Platform->sendReq in C:\inetpub\wwwroot\erexpress\public\vendor\ringcentral\ringcentral-php\src\Http\Client.php on line 44


Sample code (basically the sample code from - https://github.com/ringcentral/ringcentral-php/blob/master/demo/fax.php):


<?php

use RingCentral\SDK\SDK;


require_once(__DIR__.'/vendor/autoload.php');


$credentials = require(__DIR__ . '/vendor/ringcentral/ringcentral-php/demo/_credentials.php');


// Authorize

$platform->login($credentials['username'], $credentials['extension'], $credentials['password']);


// Find Fax-enabled phone number that belongs to extension

$phoneNumbers = $platform->get('/account/~/extension/~/phone-number', array('perPage' => 'max'))

->json()->records;


print 'Fax Phone Number: ' . $credentials['username'] . PHP_EOL;


// Send Fax

$request = $rcsdk->createMultipartBuilder()

->setBody(array(

'to' => array(

array('phoneNumber' => $credentials['username']),

),

'faxResolution' => 'High',

))

->add('Plain Text', 'file.txt')

->add(fopen('https://developers.ringcentral.com/assets/images/ico_case_crm.png', 'r'))

->request('/account/~/extension/~/fax');


//print $request->getBody() . PHP_EOL;


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


print 'Sent Fax ' . $response->json()->uri . PHP_EOL;












sdk
1 |1500 characters needed characters left characters exceeded

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

ak avatar image
ak answered
Hi ,

Did you instantiate the SDK Object within the sample code that your are using to Authenticate and Send Fax :

Please include this code within your demo application :

// Create SDK instance
$rcsdk = new SDK($credentials['appKey'], $credentials['appSecret'], $credentials['server'], 'Demo', '1.0.0');
$platform = $rcsdk->platform();
If you take a lookat our Demo Folder within the SDK you would see a file named " _credentialsSample.json" . Please make sure you are using all the details on your sandbox account like appKey, appSecret and the credentials on your sandbox account etc. 


Also for your reference, i am providing a sample code below to Authenticate and Send Fax : 

$credentials_file = count($argv) > 1         ? $argv[1] : __DIR__ . '/_credentialsSample.json';


        $credentials = json_decode(file_get_contents($credentials_file), true);

        // Create SDK instance
        $rcsdk = new SDK($credentials['appKey'], $credentials['appSecret'], $credentials['server'], 'Demo', '1.0.0');
        $platform = $rcsdk->platform();
        // // Authorize
        $platform->login($credentials['username'], $credentials['extension'], $credentials['password'], true);
    
        
        $request = $rcsdk->createMultipartBuilder()
                 ->setBody(array(
                     'to'         => array(
                         array('phoneNumber' => 'toNumber'),
                     ),
                     'faxResolution' => 'High',
                 ))
                 ->add('Plain Text', ' file.txt')
                 ->add(fopen(' https://developers.ringcentral.com/assets/images/ico_case_crm.png', 'r'))    
                 ->request('/account/~/extension/~/fax');


        $response = $platform->sendRequest($request);
        print_r("Request");
        print_r($response->request()-> getHeaders());
        print_r("Response");
        print_r($response->raw());

1 |1500 characters needed characters left characters exceeded

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

er-express-fax541 avatar image
er-express-fax541 answered
Hi Anil,

Thank you for your response. I did instantiate the SDK Object. Somehow missed pasting it in  sample code above. 

I get the same error when i substituted the reference code you provided

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Invalid response string' in vendor\ringcentral\psr7\src\ functions.php:478
Stack trace:
#0 <path_to_project>\vendor\ringcentral\ringcentral-php\src\Http\ ApiResponse.php(56): RingCentral\Psr7\parse_response('HTTP/1.1 0 OK??')
#1 <path_to_project>\vendor\ringcentral\ringcentral-php\src\Http\ Client.php(99): RingCentral\SDK\Http\ApiResponse->__construct(Object(RingCentral\Psr7\Request), false, 0)
#2 <path_to_project>\vendor\ringcentral\ringcentral-php\src\Http\ Client.php(25): RingCentral\SDK\Http\Client->loadResponse(Object(RingCentral\Psr7\Request))
#3 <path_to_project>\vendor\ringcentral\ringcentral-php\src\Platform\ Platform.php(214): RingCentral\SDK\Http\Client->send(Object(RingCentral\Psr7\Request))
#4 <path_to_project>\vendor\ringcentral\ringcentral-php\src\Platform\ Platform.php(301): RingCentral\SDK\Platform\Platform->sendReq in C:\inetpub\wwwroot\erexpress\public\vendor\ringcentral\ringcentral-php\src\Http\ Client.php on line 44
This error occurs when i try to call the  

// Authorize call
        $platform->login($credentials['username'], $credentials['extension'], $credentials['password'], true);
1 |1500 characters needed characters left characters exceeded

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

ak avatar image
ak answered
Could you please provide the Organization Name or the sandbox number with which you are using the developer platform, we could take a look at your account. 
1 |1500 characters needed characters left characters exceeded

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

er-express-fax541 avatar image
er-express-fax541 answered
The sandbox username is +18024486632. I tested the sandbox credentials on ringway API explorer : Send Fax. It works fine and I receive a fax.
1 |1500 characters needed characters left characters exceeded

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

ak avatar image
ak answered
I have created a sample Demo Application to Authenticate and send Fax . 

Please take a look at the file named " sendFax.php" in the Github Repo and you could modify the code accordingly within your application:

https://github.com/anilkumarbp/ringcentral-php-demo


1 |1500 characters needed characters left characters exceeded

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

er-express-fax541 avatar image
er-express-fax541 answered er-express-fax541 commented
Hi Anil,

I copied your code and tried to run it. 

Errors:

Error 1
 -  Fatal error: Class 'RingCentral\SDK\SDK' not found in  C:\inetpub\wwwroot\ringcentral-php-demo\app\authenticate.php on line  10

The vendor\ringcentral\ringcentral-php folder was empty, so i downloaded the zip file from  https://github.com/ringcentral/ringcentral-php and pasted those files in the folder. The error disappears which leads to Error 2

Error 2 - 
Fatal error: Interface 'Psr\Http\Message\RequestInterface' not found in  C:\inetpub\wwwroot\ringcentral-php-demo\vendor\ringcentral\psr7\src\Request.php on line  13
1 comment
1 |1500 characters needed characters left characters exceeded

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

Hi Anil,

Resolved Error 2 by downloading the PSR Http Message package from  https://github.com/php-fig/http-message

Now i get the same error i used to get earlier, attaching the error message below

exception 'InvalidArgumentException' with message 'Invalid response string' in C:\inetpub\wwwroot\ringcentral-php-demo\vendor\ringcentral\psr7\src\functions.php:478 Stack trace: #0 C:\inetpub\wwwroot\ringcentral-php-demo\vendor\ringcentral\ringcentral-php\src\Http\ApiResponse.php(56): RingCentral\Psr7\parse_response('HTTP/1.1 0 OK??') #1 C:\inetpub\wwwroot\ringcentral-php-demo\vendor\ringcentral\ringcentral-php\src\Http\Client.php(99): RingCentral\SDK\Http\ApiResponse->__construct(Object(RingCentral\Psr7\Request), false, 0) #2 C:\inetpub\wwwroot\ringcentral-php-demo\vendor\ringcentral\ringcentral-php\src\Http\Client.php(25): RingCentral\SDK\Http\Client->loadResponse(Object(RingCentral\Psr7\Request)) #3 C:\inetpub\wwwroot\ringcentral-php-demo\vendor\ringcentral\ringcentral-php\src\Platform\Platform.php(214): RingCentral\SDK\Http\Client->send(Object(RingCentral\Psr7\Request)) #4 C:\inetpub\wwwroot\ringcentral-php-demo\vendor\ringcentral\ringcentral-php\src\Platform\Platform.php(301): RingCentral\SDK\Platform\Platform->sendRequest(Object(RingCentral\Psr7\Request), Array) #5 C:\inetpub\wwwroot\ringcentral-php-demo\vendor\ringcentral\ringcentral-php\src\Platform\Platform.php(123): RingCentral\SDK\Platform\Platform->requestToken('/restapi/oauth/...', Array) #6 C:\inetpub\wwwroot\ringcentral-php-demo\app\authenticate.php(14): RingCentral\SDK\Platform\Platform->login('username', '', 'password') #7 C:\inetpub\wwwroot\ringcentral-php-demo\index.php(12): require('C:\inetpub\wwwr...') #8 {main}  Next exception 'RingCentral\SDK\Http\ApiException' with message 'Invalid response string' in C:\inetpub\wwwroot\ringcentral-php-demo\vendor\ringcentral\ringcentral-php\src\Http\Client.php:44 Stack trace: #0 C:\inetpub\wwwroot\ringcentral-php-demo\vendor\ringcentral\ringcentral-php\src\Platform\Platform.php(214): RingCentral\SDK\Http\Client->send(Object(RingCentral\Psr7\Request)) #1 C:\inetpub\wwwroot\ringcentral-php-demo\vendor\ringcentral\ringcentral-php\src\Platform\Platform.php(301): RingCentral\SDK\Platform\Platform->sendRequest(Object(RingCentral\Psr7\Request), Array) #2 C:\inetpub\wwwroot\ringcentral-php-demo\vendor\ringcentral\ringcentral-php\src\Platform\Platform.php(123): RingCentral\SDK\Platform\Platform->requestToken('/restapi/oauth/...', Array) #3 C:\inetpub\wwwroot\ringcentral-php-demo\app\authenticate.php(14): RingCentral\SDK\Platform\Platform->login('username', '', 'password') #4 C:\inetpub\wwwroot\ringcentral-php-demo\index.php(12): require('C:\inetpub\wwwr...') #5 {main} --------------Send FAX-------------- Exception: Refresh token has expired #0 C:\inetpub\wwwroot\ringcentral-php-demo\vendor\ringcentral\ringcentral-php\src\Platform\Platform.php(318): RingCentral\SDK\Platform\Platform->refresh() #1 C:\inetpub\wwwroot\ringcentral-php-demo\vendor\ringcentral\ringcentral-php\src\Platform\Platform.php(187): RingCentral\SDK\Platform\Platform->ensureAuthentication() #2 C:\inetpub\wwwroot\ringcentral-php-demo\vendor\ringcentral\ringcentral-php\src\Platform\Platform.php(214): RingCentral\SDK\Platform\Platform->inflateRequest(Object(RingCentral\Psr7\Request), Array) #3 C:\inetpub\wwwroot\ringcentral-php-demo\app\sendFax.php(24): RingCentral\SDK\Platform\Platform->sendRequest(Object(RingCentral\Psr7\Request)) #4 C:\inetpub\wwwroot\ringcentral-php-demo\index.php(16): require('C:\inetpub\wwwr...') #5 {main}
0 Likes 0 ·
er-express-fax541 avatar image
er-express-fax541 answered
Hi Anil, 

We have resolved the issue we came across while trying to integrate Ringcentral PHP SDK. The issue was caused due to cURL SSL certificate issue.

Curl error: SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed. Copied cacert.pem file and modified php.ini with the path to cacert.pem and it works.

Thank you for your support.
1 |1500 characters needed characters left characters exceeded

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

er-express-fax541 avatar image
er-express-fax541 answered
Hi,

Can you tell me how i can format the fax message into a proper content with bold and next line
eg:

$request = $rcsdk->createMultipartBuilder()   
->setBody(array(
'to'  => array(
array('phoneNumber' => '12672800722'),
'faxResolution' => 'High',))
->add('Plain Text <Br> <b> Hello </b>', 'file.txt')
->request('/account/~/extension/~/fax');
Right now the content shows up in the fax message as such:
Plain Text <Br> <b> Hello </b>.
Can we have html formatting:
Plain Text 
Hello.
1 |1500 characters needed characters left characters exceeded

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

ak avatar image
ak answered
You are trying to add HTML formatting to a plain text. You could try replacing the "<" and ">" symbols with "&lt" and "&gt" respectively but i suppose that would work with the text file. 

However, you could create a .pdf format file and you could have it included as your attachment before sending the fax.


1 |1500 characters needed characters left characters exceeded

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

er-express-fax541 avatar image
er-express-fax541 answered
Created a pdf file and as suggested included it as an attachment. Works as expected

Thanks
1 |1500 characters needed characters left characters exceeded

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