Updating csv datasource with the SOAP API


(Joel Porgand) #1

So I spent a little while mucking around getting this to work, and I thought I'd share. You can't use the file retrieval service "Upload" function to do this as csv datasources aren't file assets. What you have to do instead is serialise the csv yourself & set the "cached content" attribute. Here's a fairly quick & dirty example in PHP. 

<?php
$csvpath = "/path/to/csv.csv";
$csvid = "12345";
$wsdl = "http://path.to/soap_server?wsdl";

$client = new SoapClient($wsdl, array(‘login’ => “username”, ‘password’ => “password”));

$file = fopen($csvpath, “r”);
if($file){
$csvarray = array();
$nn = 0;
while (($data = fgetcsv($file, 1000, “,”)) !== FALSE) {
$c = count($data);
for ($x=0;$x<$c;$x++)
{
$csvarray[$nn][$x] = $data[$x];
}
$nn++;
}
fclose($file);
$parameters = array();
$parameters[] = new SoapVar(serialize($csvarray), XSD_STRING, null, null, ‘AttributeValue’ );
$parameters[] = new SoapVar(‘cached_content’, XSD_STRING, null, null, ‘AttributeName’ );
$parameters[] = new SoapVar($csvid, XSD_STRING, null, null, ‘AssetID’ );
try{
$soap_response = $client->SetAttributeValue(new SoapVar($parameters, SOAP_ENC_OBJECT));
if($soap_response->SetAttributeValueResult === “Array”){
die(“it worked, yay”);
}else{
throw new Exception(“didn’t work, how mysterious”);
}
}catch (SoapFault $e){
throw new Exception("SOAP call failed: ".$e->getMessage());
}
}else{
throw new Exception(“can’t open file”);
}

It feels a bit sketch but it works. 


(Joel Porgand) #2

I should add that this skips the checks & balances that are normally associated with uploading a new csv to a csv datasource. Have a look in data_source_csv_edit_fns.inc if you wish to borrow them.