1

我刚开始研究RingCentral API

我对他们对数据的期望有点困惑。

我首先尝试使用 curl :

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

然后我尝试使用 number@ringcentral.com 作为电子邮件发送,但我仍然无法让它工作。他们的支持网站毫无用处,因为我看到了许多未解决的问题,但我别无选择,需要让它发挥作用。

我希望有人在 PHP 中做到了这一点,并且可以为我提供一个示例或为我指明正确的道路。

4

2 回答 2

2

我能够让原始代码做两件事:

(1) 去掉 $url 的前导空格:

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

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

(2) 确保从http://php.net/manual/en/function.curl-setopt.php的 PHP 文档中指定的ROOT_PATHa 开始。@CURLOPT_POSTFIELDS

cURL 和 Guzzle 示例

以下是一些使用 cURL 和 Guzzle 验证的示例。

卷曲示例

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 平台 API,该 API 具有更全面的传真 API 和此处记录的其他功能:https ://developers.ringcentral.com/api-and-docs.html

于 2015-03-18T15:08:18.260 回答
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; 是绝对正确的路径

于 2014-06-28T01:01:00.983 回答