I am trying to upload a text file of data to a soap server with a function called BulkLoadContacts. The request requires a value called
<ImportFileStream>base64Binary</ImportFileStream>
I am creating a file, then querying my database, writing to the file, then connecting to their soap server, then sending my request.
Here is a snippit of my code:
$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);
}
Here is a code example from the documentation:
Byte[] fileBytes = System.IO.File.ReadAllBytes("C:\\Temp\\ImportFiles\\TestImportFileWithProfile.txt");
Any ideas how to do this in PHP?
Maybe something like this:
$fileName = "test.txt";
$rawFile = fread(fopen($fileName, "r"), filesize($fileName));
$B64File = base64_encode($rawFile)
In the param array:
'ImportFileStream' => $B64File
Maybe PHP client SOAP engine encodes to B64 automatically. If this is the case, comment $B64File = base64_encode($rawFile)
and send $rawFile
instead.
You have no need to write your data to file - just pass it directly
$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);
}
If SOAP client does not encodes data into base64 automatically, you need to also add this:
$data = base4_encode($data);