我刚开始看RingCentral 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);
然后我尝试使用 number@ringcentral.com 作为电子邮件发送,但我仍然无法让它工作。他们的支持网站毫无用处,因为我看到许多未解决的问题,但我别无选择,需要让它工作。
我希望有人在PHP中做到了这一点,并且可以为我提供一个例子或为我指出正确的道路。
我能够让原始代码做两件事:
(1( 从$url中删除前导空格:
# Original
$url = ' https://service.ringcentral.com/faxapi.asp';
# New
$url = 'https://service.ringcentral.com/faxapi.asp';
(2( 确保ROOT_PATH
以 PHP 文档中指定的@
开头,用于 http://php.net/manual/en/function.curl-setopt.php 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 GuzzleHttpClient;
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用于传真和其他功能,记录如下:https://developers.ringcentral.com/api-and-docs.html
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; 是绝对正确的路径