使用 RingCentral API 发送传真的 HTML 内容的正确格式
Proper formation of the HTML content for FAX sent using RingCentral API
通过 RingCentral API 为传真发送的 HTML 内容未按正确顺序格式化。
我用过的代码:
// The HTML content to be sent
$html = "<h3>Notification</h3><div>Lorem epsum Lorem epsum Lorem epsum Lorem epsum <b>My Site</b>Lorem epsum Lorem epsum</div><div><br></div><div>Lorem epsumLorem epsumLorem epsum<i><b>Lorem epsumLorem epsumLorem epsum</b></i>.</div><div><br></div><div>To view more and print more details, please log in to <a href='http://www.demo.mysite.com' target='_blank'>www.demo.mysite.com</a> using your email address.</div><div><br></div><div>Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum</div>";
// Creating a file
$fileRand = rand();
$filename = 'faxfile_'.$fileRand.'.html';
// Open the file in write mode
$faxFile = fopen('ringfax/'.$filename, 'w');
// Write the contents to the html file.
fwrite($faxFile, $html);
// Close the file.
fclose($faxFile);
// Setting up data for the RingCentral API
$faxData['Username'] = "XXXXXXXXXX";
$faxData['Password'] = "XXXXXXXXXX";
$faxData['Recipient'] = "XXXXXXXXXX";
$faxData['Sendtime'] = gmdate('d:m:y h:m');
$faxData['Coverpage'] = 0;
$faxData['Attachment'] = '@'.realpath('ringfax/'.$filename).';filename='.$filename.';content-type=text/html';
// Open connection
$ch = curl_init();
// Set the url, number of POST vars and other data
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, 'https://service.ringcentral.com/faxapi.asp?');
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: text/html", "charset: UTF-8"));
curl_setopt($ch, CURLOPT_POST, count($faxData));
curl_setopt($ch, CURLOPT_POSTFIELDS, $faxData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute post
$result = curl_exec($ch);
// Receives curl error
$cErr = curl_error($ch);
// curl curl info
$cInfo = curl_getinfo($ch);
// Write the error to the log file
ini_set("log_errors", 1);
ini_set("error_log", "logs/ring_central_error");
error_log($result);
//close connection
curl_close($ch);
// Delete the file
unlink('ringfax/'.$filename);
正在发送传真,但传真中的内容未按照我们希望的格式进行格式化。
传真中的内容类似于:
Notification Lorem epsum Lorem epsum Lorem epsum Lorem epsum My Site Lorem epsum Lorem epsumLorem epsumLorem epsumLorem epsum Lorem epsumLorem epsumLorem epsum To view more and print more details, please log in to www.demo.mysite.com using your email address.Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum.
我很确定它一定是 header 类型或未正确设置的东西,这就是 RingCentral API 表现如此的原因。
提前致谢。
看来您使用的是旧版本的传真机API。您能否在这里查看开发人员指南中的 API:
https://developer.ringcentral.com/api-docs/latest/index.html#!#FaxMessages.html
您还可以为新 API 使用 RingCentral PHP SDK,这将创建格式正确的请求:
- GitHub: https://github.com/ringcentral/ringcentral-php
- 包装商:https://packagist.org/packages/ringcentral/ringcentral-php
这是来自 README.md
的一些 PHP 代码:
$request = $rcsdk->createMultipartBuilder()
->setBody(array(
'to' => array(
array('phoneNumber' => '16501112233'),
),
'faxResolution' => 'High',
))
->add('Plain Text', 'file.txt')
->add(fopen('path/to/file', 'r'))
->request('/account/~/extension/~/fax'); // also has optional $method argument
$response = $platform->sendRequest($request);
通过 RingCentral API 为传真发送的 HTML 内容未按正确顺序格式化。
我用过的代码:
// The HTML content to be sent
$html = "<h3>Notification</h3><div>Lorem epsum Lorem epsum Lorem epsum Lorem epsum <b>My Site</b>Lorem epsum Lorem epsum</div><div><br></div><div>Lorem epsumLorem epsumLorem epsum<i><b>Lorem epsumLorem epsumLorem epsum</b></i>.</div><div><br></div><div>To view more and print more details, please log in to <a href='http://www.demo.mysite.com' target='_blank'>www.demo.mysite.com</a> using your email address.</div><div><br></div><div>Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum</div>";
// Creating a file
$fileRand = rand();
$filename = 'faxfile_'.$fileRand.'.html';
// Open the file in write mode
$faxFile = fopen('ringfax/'.$filename, 'w');
// Write the contents to the html file.
fwrite($faxFile, $html);
// Close the file.
fclose($faxFile);
// Setting up data for the RingCentral API
$faxData['Username'] = "XXXXXXXXXX";
$faxData['Password'] = "XXXXXXXXXX";
$faxData['Recipient'] = "XXXXXXXXXX";
$faxData['Sendtime'] = gmdate('d:m:y h:m');
$faxData['Coverpage'] = 0;
$faxData['Attachment'] = '@'.realpath('ringfax/'.$filename).';filename='.$filename.';content-type=text/html';
// Open connection
$ch = curl_init();
// Set the url, number of POST vars and other data
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, 'https://service.ringcentral.com/faxapi.asp?');
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: text/html", "charset: UTF-8"));
curl_setopt($ch, CURLOPT_POST, count($faxData));
curl_setopt($ch, CURLOPT_POSTFIELDS, $faxData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute post
$result = curl_exec($ch);
// Receives curl error
$cErr = curl_error($ch);
// curl curl info
$cInfo = curl_getinfo($ch);
// Write the error to the log file
ini_set("log_errors", 1);
ini_set("error_log", "logs/ring_central_error");
error_log($result);
//close connection
curl_close($ch);
// Delete the file
unlink('ringfax/'.$filename);
正在发送传真,但传真中的内容未按照我们希望的格式进行格式化。 传真中的内容类似于:
Notification Lorem epsum Lorem epsum Lorem epsum Lorem epsum My Site Lorem epsum Lorem epsumLorem epsumLorem epsumLorem epsum Lorem epsumLorem epsumLorem epsum To view more and print more details, please log in to www.demo.mysite.com using your email address.Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum.
我很确定它一定是 header 类型或未正确设置的东西,这就是 RingCentral API 表现如此的原因。
提前致谢。
看来您使用的是旧版本的传真机API。您能否在这里查看开发人员指南中的 API:
https://developer.ringcentral.com/api-docs/latest/index.html#!#FaxMessages.html
您还可以为新 API 使用 RingCentral PHP SDK,这将创建格式正确的请求:
- GitHub: https://github.com/ringcentral/ringcentral-php
- 包装商:https://packagist.org/packages/ringcentral/ringcentral-php
这是来自 README.md
的一些 PHP 代码:
$request = $rcsdk->createMultipartBuilder()
->setBody(array(
'to' => array(
array('phoneNumber' => '16501112233'),
),
'faxResolution' => 'High',
))
->add('Plain Text', 'file.txt')
->add(fopen('path/to/file', 'r'))
->request('/account/~/extension/~/fax'); // also has optional $method argument
$response = $platform->sendRequest($request);