Jeremy
Jeremy

Reputation: 1972

RingCentral PHP FaxOut API example

I just started looking at the RingCentral API

I am a little confused on how they expect the data.

I tried first with curl using:

    $url = ' https://service.ringcentral.com/faxapi.asp';
    $faxData = array();
    $faxData['Username'] = 'xxxxxxxx';
    $faxData['Password'] = 'xxxxxxxx';
    $faxData['Recipient'] = $faxNumber.'|TEST';
    $faxData['Attachment'] = ROOT_PATH.$fileLocation;

    // build url encoded string
    $fields_string='';
    foreach($faxData as $key=>$value) {
        $fields_string .= $key.'='.urlencode($value).'&';
    }
    rtrim($fields_string, '&');

    //open connection
    $ch = curl_init();
    //set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, count($faxData));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $faxData);

    //execute post
    $result = curl_exec($ch);
    $err = curl_errno ( $ch );
    $errmsg = curl_error ( $ch );
    $header = curl_getinfo ( $ch );
    $httpCode = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );

    //close connection
    curl_close($ch);

Then I tried sending as an email using the [email protected] and I still am unable to get this to work at all. Their support site is useless as I see many unanswered questions but I have no choice and need to get this working.

I am hoping someone has done this in PHP and can provide me with an example or point me in the right path.

Upvotes: 1

Views: 1531

Answers (2)

Grokify
Grokify

Reputation: 16354

I was able to get the original code to work doing two things:

(1) Removing the leading space from $url:

# Original
$url = ' https://service.ringcentral.com/faxapi.asp';

# New
$url = 'https://service.ringcentral.com/faxapi.asp';

(2) Ensuring ROOT_PATH began with a @ as specified in the PHP documentation for CURLOPT_POSTFIELDS at http://php.net/manual/en/function.curl-setopt.php.

cURL and Guzzle Examples

Here are some examples using cURL and Guzzle verified to work.

cURL Example

function ringcentral_faxout_api_via_curl($username,$password,$recipient,$file,$coverpagetext) {

    $request = curl_init('https://service.ringcentral.com/faxapi.asp');

    curl_setopt($request, CURLOPT_POST, true);
    curl_setopt($request, CURLOPT_POSTFIELDS, array(
        'username'      => $username,
        'password'      => $password,
        'recipient'     => $recipient,
        'attachment'    => '@' . realpath($file),
        'coverpagetext' => $coverpagetext
    ));
    curl_setopt($request, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($request);
    curl_close($request);
    return $response;
}

$username  = 'myusername';
$password  = 'mypassword';
$recipient = 'myrecipient';
$file      = '/path/to/myfile';

$result = ringcentral_faxout_api_via_curl( $username, $password, $recipient, $file, 'PHP FaxOut Via cURL');

Guzzle Example

use GuzzleHttp\Client;

function ringcentral_faxout_api_via_guzzle($username,$password,$recipient,$file,$coverpagetext) {

    $client = new Client();
    $response = $client->post('https://service.ringcentral.com/faxapi.asp', [
        'body' => [
            'username'      => $username,
            'password'      => $password,
            'recipient'     => $recipient,
            'attachment'    => fopen($file, 'r'),
            'coverpagetext' => $coverpagetext
        ]
    ]);

    return $response->getBody();
}

$username  = 'myusername';
$password  = 'mypassword';
$recipient = 'myrecipient';
$file      = '/path/to/myfile';

$result = ringcentral_faxout_api_via_guzzle( $username, $password, $recipient, $file, 'PHP FaxOut Via Guzzle');

New RingCentral API

Also check out the newer RingCentral Platform API which has a much more comprehensive API for faxing and other capabilities documented here: https://developers.ringcentral.com/api-and-docs.html

Upvotes: 2

J-Dizzle
J-Dizzle

Reputation: 3191

function fetch_url_post($url, $variable_array){
    $fields_string = "";
    //set POST variables
    #$url = 'http://domain.com/get-post.php';
    foreach($variable_array as $key => $value){
        $fields[$key] =  urlencode($value);
    }

    //url-ify the data for the POST
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string, '&');

    //open connection
    $ch = curl_init();

    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

    //execute post
    $result = curl_exec($ch);
    return $result;
    //close connection
    curl_close($ch);
}
$url = ' https://service.ringcentral.com/faxapi.asp';
$faxData = array();
$faxData['Username'] = 'xxxxxxxx';
$faxData['Password'] = 'xxxxxxxx';
$faxData['Recipient'] = $faxNumber.'|TEST';
$faxData['Attachment'] = ROOT_PATH.$fileLocation;
echo fetch_url_post($url, $faxData);

make sure ROOT_PATH.$fileLocation; is an absolute and correct path

Upvotes: 0

Related Questions