send fax using curl

  • 1
  • Problem
  • Updated 1 month ago
  • (Edited)
Hi, I am trying to send fax using curl. I can send it via my localhost. But when I am uploading code to server and try to send it with attachment it is showing some error like:
                    [errorCode] => MSG-356
                    [message] => Either filename or content type should be specified, attachment [1]: []
                    [attachmentFileName] =>
                    [attachmentNumber] => 1.

I think It has some error in following part of code:

 $file_name_with_full_path = ('public/pdf/'.$item[0]['item_id'].'-'.$random_number.'_doc.pdf');
                
                            curl_setopt($ch, CURLOPT_POSTFIELDS, array(
                            'username'      => '+123456789',
                            'password'      => "abc@1234",
                            'to'     => '+12457899',
                            'from'=>'+795454663',
                            'attachment'    => '@'.$file_name_with_full_path,
                            'contentType'=>'application/pdf',
                          
                        ));



Photo of Nikunj Bhatnagar

Nikunj Bhatnagar

  • 160 Points 100 badge 2x thumb

Posted 1 month ago

  • 1
Photo of Tyler Long

Tyler Long, Official Rep

  • 9,336 Points 5k badge 2x thumb
Photo of Nikunj Bhatnagar

Nikunj Bhatnagar

  • 160 Points 100 badge 2x thumb
Yes I read it. Please send me complete code of send fax with attachment in php. I am newbie for curl also I am not able to understand it.
Photo of Nikunj Bhatnagar

Nikunj Bhatnagar

  • 160 Points 100 badge 2x thumb
My code is working properly on localhost but it is not working on server.
Here is my code:- Credentials are dummy 
/////////////////////////////////////////////////////////////////////////////////////
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://platform.devtest.ringcentral.com/restapi/oauth/token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=+15450345789&password=2145854#&extension=101&grant_type=password");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "kukadkashjasd" . ":" . "f9ReaNa3TSe1RYx8hS-8jahdkjdsjhadhjkjahds");

$headers = array();
$headers[] = "Accept: application/json";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

$result = json_decode($result);
if (curl_errno($ch)) {
   echo 'Error:' . curl_error($ch);
}
  $token = $result->access_token;
curl_close($ch);
$ch = curl_init();   

            $url = "https://platform.devtest.ringcentral.com/restapi/v1.0/account/~/extension/~/fax";
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, array(
        'username'      => '+15450345789',
        'password'      => "2145854",
        'to'     => '+9874563210',
        'from'=>'+142452454565',
        'attachment'    => '@' . realpath('/var/www/html/pdf2-199167_doc.pdf'),
        'coverpagetext' => 'PHP FaxOut Via cURL'
    ));
            $dataarray =array(
                              'accept: application/json',
                              'authorization: Bearer '.$token
                              );
            curl_setopt($ch, CURLOPT_HTTPHEADER, $dataarray);

            $response = curl_exec($ch);
            curl_close($ch);
            $data = (json_decode($response));
            echo '<pre>';
print_r($data); exit;
/////////////////////////////////////////////////////////////////////////////////////////////
It is working perfect on my local server. But when I upload it to server,
I am getting this error:-

    [errorCode] => InvalidContent
    [message] => Either filename or content type should be specified, attachment [1]: []
    [errors] => Array
        (
            [0] => stdClass Object
                (
                    [errorCode] => MSG-356
                    [message] => Either filename or content type should be specified, attachment [1]: []
                    [attachmentFileName] => 
                    [attachmentNumber] => 1
                )

        )

    [attachmentFileName] => 
    [attachmentNumber] => 1
)

(Edited)
Photo of Phong Vu

Phong Vu, Devangelist

  • 4,566 Points 4k badge 2x thumb
First of all, is there any reason you don't want to use our SDKs? Using our SDKs is the most convenient way to access RingCentral platform and call the APIs. What programming language do you use? https://github.com/ringcentral

If you still want to write from scratch using curl, see the example from the API explorer https://developer.ringcentral.com/api-reference#Fax-sendFaxMessage

+ Phong
Photo of Nikunj Bhatnagar

Nikunj Bhatnagar

  • 160 Points 100 badge 2x thumb
I am using php. Could you please send me complete code of send fax with attachment becouse I faced some authentication problem when I tried using sdk.
Photo of Nikunj Bhatnagar

Nikunj Bhatnagar

  • 160 Points 100 badge 2x thumb
My code is working properly on localhost but it is not working on server.
Here is my code:- Credentials are dummy 
/////////////////////////////////////////////////////////////////////////////////////
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://platform.devtest.ringcentral.com/restapi/oauth/token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=+15450345789&password=2145854#&extension=101&grant_type=password");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "kukadkashjasd" . ":" . "f9ReaNa3TSe1RYx8hS-8jahdkjdsjhadhjkjahds");

$headers = array();
$headers[] = "Accept: application/json";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

$result = json_decode($result);
if (curl_errno($ch)) {
   echo 'Error:' . curl_error($ch);
}
  $token = $result->access_token;
curl_close($ch);
$ch = curl_init();   

            $url = "https://platform.devtest.ringcentral.com/restapi/v1.0/account/~/extension/~/fax";
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, array(
        'username'      => '+15450345789',
        'password'      => "2145854",
        'to'     => '+9874563210',
        'from'=>'+142452454565',
        'attachment'    => '@' . realpath('/var/www/html/pdf2-199167_doc.pdf'),
        'coverpagetext' => 'PHP FaxOut Via cURL'
    ));
            $dataarray =array(
                              'accept: application/json',
                              'authorization: Bearer '.$token
                              );
            curl_setopt($ch, CURLOPT_HTTPHEADER, $dataarray);

            $response = curl_exec($ch);
            curl_close($ch);
            $data = (json_decode($response));
            echo '<pre>';
print_r($data); exit;
/////////////////////////////////////////////////////////////////////////////////////////////
It is working perfect on my local server. But when I upload it to server,
I am getting this error:-

    [errorCode] => InvalidContent
    [message] => Either filename or content type should be specified, attachment [1]: []
    [errors] => Array
        (
            [0] => stdClass Object
                (
                    [errorCode] => MSG-356
                    [message] => Either filename or content type should be specified, attachment [1]: []
                    [attachmentFileName] => 
                    [attachmentNumber] => 1
                )

        )

    [attachmentFileName] => 
    [attachmentNumber] => 1
)
(Edited)
Photo of Tyler Long

Tyler Long, Official Rep

  • 9,246 Points 5k badge 2x thumb
I don't think it is a localhost/server issue.

The files you are trying to send on server is different from the one on localhost. Could you please double check.

If you have issue with the PHP SDK: https://github.com/ringcentral/ringcentral-php could you please create a GitHub issue there?


Photo of Nikunj Bhatnagar

Nikunj Bhatnagar

  • 160 Points 100 badge 2x thumb
Sir I triple checked. Code is same. Could you please check my code and error. I thing there is some permission issue of curl on server. Curl is not able to fetch the file from path.
Photo of Phong Vu

Phong Vu, Devangelist

  • 4,566 Points 4k badge 2x thumb
First of all, what issue did you face when you use the RingCentral SDK to get authentication? If your app is password flow, check out this tutorial

Secondly, if your code works well on localhost, your code to call RingCentral fax endpoint must be correct. Now, if it does not work on your remote server and the complaint is about the attachment, the problem is most likely with your file on the server or as you said, some "permission" issue setting for your server that I cannot be aware of.

This is not a RingCentral API problem but I guess our server cannot reach the attachment file with the specified path realpath('/var/www/html/pdf2-199167_doc.pdf'). Can you put the file somewhere that it could be accessible e.g. from the browser and use that link.

I strongly recommend you to try the SDK again.

+ Phong
Photo of Nikunj Bhatnagar

Nikunj Bhatnagar

  • 160 Points 100 badge 2x thumb
Sir could you please send me complete code step by step to send fax with attachment using RingCentral SDK in php?
Photo of Nikunj Bhatnagar

Nikunj Bhatnagar

  • 160 Points 100 badge 2x thumb
Hello Sir.
I tried through SDK and now I am getting this error:-


login/relogin
Exception: Resource Owner credentials are incomplete
SDK HTTP Error at https://platform.devtest.ringcentral.com/restapi/oauth/token
Response text: {
  "error" : "invalid_request",
  "error_description" : "Resource Owner credentials are incomplete",
  "errors" : [ {
    "errorCode" : "OAU-143",
    "message" : "Resource Owner credentials are incomplete"
  } ]
}
Photo of Tyler Long

Tyler Long, Official Rep

  • 8,884 Points 5k badge 2x thumb
Please double check your clientID, clientSecret, username & password.
Photo of Nikunj Bhatnagar

Nikunj Bhatnagar

  • 160 Points 100 badge 2x thumb
Its done sir. Every thing was correct. I did it through ringcentral lite.
Now I have another problem. I have records of inbound and outbound fax in my project. I am getting the records but if I want do download any record I is not working.
Please suggest me how to download it.