我正在尝试使用名为BulkLoadContacts的函数将数据的文本文件上传到soap服务器。该请求需要一个名为
的值 <ImportFileStream>base64Binary</ImportFileStream>
我正在创建一个文件,然后查询我的数据库,写入文件,然后连接到他们的soap服务器,然后发送我的请求。
以下是我的代码的信息:
$file = 'tempfile.txt';
$handle = fopen($file, 'w') or die ('Cannot open file: ' . $file);
$list = dbExec("select * from listrak_upload");
foreach ($list as $l){
$data = $l['email'] . "|" . $l['First_Name'] . "|" . $l['Last_Name'];
fwrite($handle, $data);
}
$ap_param = array(
'ListID' => (integer) 252403,
'WSImportDirectives' => array(
'ImportTypeEnum' => 'AddSubscribers',
'ImportProfileTypeEnum' => 'Overwrite',
'FileName' => '',
'HasColumnNames' => true,
'FileName' => $file,
'FileDelimiter' => '|',
),
'fileMappings' => array(
'WSFileMappings' => array(
'FileColumn' => 0,
'IsEmailAddressColumn' => true,
'AttributeID' => 'email',
),
),
'ImportFileStream' => $handle
);
try {
$return = $soapClient->__soapCall("BulkLoadContacts",array('parameter' => $ap_param));
var_dump($return);
}
catch(SoapFault $fault){
var_dump($fault);
}
以下是文档中的代码示例:
Byte[] fileBytes = System.IO.File.ReadAllBytes("C:\\Temp\\ImportFiles\\TestImportFileWithProfile.txt");
有任何想法如何在PHP中执行此操作?
答案 0 :(得分:3)
也许是这样的:
$fileName = "test.txt";
$rawFile = fread(fopen($fileName, "r"), filesize($fileName));
$B64File = base64_encode($rawFile)
在param数组中:
'ImportFileStream' => $B64File
也许PHP客户端SOAP引擎会自动编码到B64。如果是这种情况,请发表评论$B64File = base64_encode($rawFile)
并发送$rawFile
。
答案 1 :(得分:2)
您无需将数据写入文件 - 只需直接传递
即可$list = dbExec("select * from listrak_upload");
foreach ($list as $l){
$data = $l['email'] . "|" . $l['First_Name'] . "|" . $l['Last_Name'];
}
$ap_param = array(
'ListID' => (integer) 252403,
'WSImportDirectives' => array(
'ImportTypeEnum' => 'AddSubscribers',
'ImportProfileTypeEnum' => 'Overwrite',
'FileName' => '',
'HasColumnNames' => true,
'FileName' => $file,
'FileDelimiter' => '|',
),
'fileMappings' => array(
'WSFileMappings' => array(
'FileColumn' => 0,
'IsEmailAddressColumn' => true,
'AttributeID' => 'email',
),
),
'ImportFileStream' => $data
);
try {
$return = $soapClient->__soapCall("BulkLoadContacts",array('parameter' => $ap_param));
var_dump($return);
}
catch(SoapFault $fault){
var_dump($fault);
}
如果SOAP客户端没有自动将数据编码到base64中,您还需要添加:
$data = base4_encode($data);