2014-06-28 36 views
1

我剛開始看RingCentral APIRingCentral PHP FaxOut API例如

我對他們如何指望數據有點困惑。

我第一次嘗試,捲曲使用:

$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); 

然後我嘗試使用[email protected]發送的電子郵件,我仍然是無法得到這在所有的工作。他們的支持網站是無用的,因爲我看到許多沒有答案的問題,但我沒有選擇,需要得到這個工作。

我希望有人已經在PHP中完成了這項工作,並且可以爲我提供一個示例或指引我走入正確的道路。

回答

0
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); 

確保ROOT_PATH。$ fileLocation;是一個絕對正確的路徑

+0

我得到了同樣的迴應。感謝壽。我用他們的電子郵件rcfax.com電子郵件取而代之,因爲這仍然不起作用。 – Jeremy

+0

再次看我看你如何使它工作原來的方式 - 改變'$ url ='https://service.ringcentral.com/faxapi.asp';'到'$ url ='https:// service。 ringcentral.com/faxapi.asp?';',刪除最初的空格並添加一個'?' –

2

我能得到原代碼工作做兩件事情:

(1)刪除從$ URL前導空格:

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

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

(2)確保ROOT_PATH開頭的@在PHP文檔CURLOPT_POSTFIELDShttp://php.net/manual/en/function.curl-setopt.php中指定。

捲曲和狂飲例子

下面是使用捲曲一些例子,狂飲驗證的工作。

捲曲例

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'); 

狂飲例

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'); 

新RingCentral API

還檢查了新RingCentral Platform API其中有一個更加全面的API FO r傳真和其他功能記錄在這裏:https://developers.ringcentral.com/api-and-docs.html