function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
joshQjoshQ 

Using PHP to add Attachments

Using PHP to add Attachments

Does anyone have a complete piece of PHP code to add attachments?

(PHP5/API7).

I’m getting “INVALID_TYPE: sObject type 'sObject' is not supported.”

 

Thx – joshQ

Best Answer chosen by Admin (Salesforce Developers) 
Tran ManTran Man
Try something like this:

Code:
try {
  $mySforceConnection = new SforcePartnerClient();
  $mySoapClient = $mySforceConnection->createConnection("partner.wsdl.xml");
  $mylogin = $mySforceConnection->login("yourlogin@xyc.com", "password");

  $sObject1 = new SObject();
  $sObject1->type = 'Attachment';
  $handle = fopen('gif_1.gif','rb');
  $file_content = fread($handle,filesize('gif_1.gif'));
  fclose($handle);
  $encoded = chunk_split(base64_encode($file_content));
  $sObject1->body = $encoded;
  $createFields = array (
    'Name' => 'gif_1.gif',
    'ParentId' => '0033000000FqGBwAAN',
    'Body' => $encoded
  );
  $sObject1->fields = $createFields;
  $createResponse = $mySforceConnection->create(array($sObject1));
  echo $mySforceConnection->getLastRequest();
  print_r($createResponse);
} catch (Exception $e) {
  echo $e->faultstring;
  echo $mySforceConnection->getLastRequest();
}

 

All Answers

Tran ManTran Man
Try something like this:

Code:
try {
  $mySforceConnection = new SforcePartnerClient();
  $mySoapClient = $mySforceConnection->createConnection("partner.wsdl.xml");
  $mylogin = $mySforceConnection->login("yourlogin@xyc.com", "password");

  $sObject1 = new SObject();
  $sObject1->type = 'Attachment';
  $handle = fopen('gif_1.gif','rb');
  $file_content = fread($handle,filesize('gif_1.gif'));
  fclose($handle);
  $encoded = chunk_split(base64_encode($file_content));
  $sObject1->body = $encoded;
  $createFields = array (
    'Name' => 'gif_1.gif',
    'ParentId' => '0033000000FqGBwAAN',
    'Body' => $encoded
  );
  $sObject1->fields = $createFields;
  $createResponse = $mySforceConnection->create(array($sObject1));
  echo $mySforceConnection->getLastRequest();
  print_r($createResponse);
} catch (Exception $e) {
  echo $e->faultstring;
  echo $mySforceConnection->getLastRequest();
}

 

This was selected as the best answer
joshQjoshQ
Thx Nick - works like a charm...
TikoTiko
Thank you!!!
This was realy usefull for me.
Philip_FPhilip_F

Thanks for the useful post Nick!

 

For anyone else finding this post, I've updated the code to work with the php Toolkit v. 13. 

 



try {
$sObject = new stdclass();
$sObject->FirstName = 'Leed';
$sObject->LastName = 'John';
$sObject->Company = 'Test Co';
$sObject->Status = 'Open - Not Contacted';

echo "**** Creating the following:\r\n";
$createResponse = $mySforceConnection->create(array($sObject), 'Lead');

$ids = array();
foreach ($createResponse as $createResult) {
print_r($createResult);
array_push($ids, $createResult->id);
}

$sObject1 = new stdclass();
$handle = fopen('gif_1.gif','rb');
$file_content = fread($handle,filesize('gif_1.gif'));
fclose($handle);
$encoded = chunk_split(base64_encode($file_content));
$sObject1->Name = 'gif_1.gif';
$sObject1->ParentId = "0033000000FqGBwAAN";
$sObject1->body = $encoded;
$createResponse = $mySforceConnection->create(array($sObject1), 'Attachment');
echo $mySforceConnection->getLastRequest();
print_r($createResponse);


} catch (Exception $e) {
echo $mySforceConnection->getLastRequest();
echo $e->faultstring;
}

 

 Cheers,

-Philip 

 

LantzvillianLantzvillian

Cleaned this up a tad more and added variables $parentID and $file so this code code be used more dynamically.  This should be a snippit for the php toolkit.


try {
$mySforceConnection = new SforcePartnerClient();
$mySoapClient = $mySforceConnection->createConnection('partner.wsdl.xml');
$mylogin = $mySforceConnection->login($USERNAME, $PASSWORD);

$file = 'test.txt';
$parentID = 'parentObjectIDGoesHere';

$sObject1 = new SObject();
$sObject1->type = 'Attachment';
$handle = fopen($file,'rb');
$file_content = fread($handle,filesize($file));
fclose($handle);
$encoded = chunk_split(base64_encode($file_content));
$sObject1->body = $encoded;
$createFields = array (
'Name' => $file,
'ParentId' => $parentID,
'Body' => $encoded
);
$sObject1->fields = $createFields;
$createResponse = $mySforceConnection->create(array($sObject1));
echo $mySforceConnection->getLastRequest();
print_r($createResponse);
} catch (Exception $e) {
echo $e->faultstring;
echo $mySforceConnection->getLastRequest();
}
?>

 

Message Edited by Lantzvillian on 06-11-2009 11:38 AM
IanHmanIanHman
Works perfectly, thanks!