Showing Salesforce Images/Attachments on PHP/JSP page

Saturday, October 1, 2011 by Aslam - The Alexendra
Hi All,
Today I am going to demo you how to fetch images (attachments) from salesforce instance in to your PHP script and how you can show those imags on page as a portal or catalog way.

1) index.php script
The below code shows how you can query to attachment object in salesforce to fetch some attachment records. For example i have query the pics (attachment) for my two SFDC contact records in sample code. The concept is simple. You need to query the Id of attachment in your main page, then you will have one another php script which will fetch the SFDC attachment (image) binary data and show the image on screen as embedded. We can wrap/embed that image in <img /> tag directly.

<?php
error_reporting(E_ALL);
require_once ('soapclient/SforcePartnerClient.php');
require_once('connection.php');

$ids = array();
$ids[] = '00390000006IUVK';
$ids[] = '00390000001lwbd';
getPhotos(
$ids);


function getPhotos($contactids){
$mySforceConnection = getConnection();
$query = "SELECT Id, Name from Attachment Where ParentId in (";
foreach($contactids as $id){
$query .= "'". $id . "',";
}
$query = substr($query , 0, strlen($query) - 1);
$query .= ")";
$queryResult = $mySforceConnection->query($query);

$records = $queryResult->records;
if(count($records) > 0){
echo "<table cellspacing='5' cellpadding='5'><tr>";
foreach($records as $rec){
echo "<td style='text-align:center;font-size:12px;'>".
"<img src='image.php?id=".$rec->Id."' ".
"style='width:200px;height:200px;border:1px solid #ccc;padding:10px'/> <br/>"
. $rec->fields->Name.
"</td>";
}
echo "</table>";
}
return NULL;
}
?>


2) image.php script
This code simply accepts one Id of the attachment record and fetch the binary body of attachment, then it shows that image on the screen as image content. To view this image in other page you simply need to wrap/embed its call into <img /> html tag. Like below:

<img src="image.php?id=00P9000000185ioEAA" />


<?php
error_reporting(E_ALL);
header('content-type: image/jpeg');
require_once ('soapclient/SforcePartnerClient.php');
require_once('connection.php');

getPhotos(
$_GET['id']);


function getPhotos($id){
$mySforceConnection = getConnection();
$query = "SELECT Id, Name, Body from Attachment Where Id ='" .$id ."'";
$queryResult = $mySforceConnection->query($query);

$records = $queryResult->records;

print_r(base64_decode($records[0]->fields->Body));
return NULL;
}

?>



Here is the demo of the scripts where i am fetching two contact's images from Salesforce and showing on simple PHP page.

http://labs.aslambari.com/sfdc_photos_demo/


Thanks
Aslam Bari

11 comments:

Shaikh Mustakim said...

Great....useful Stuff

Amit said...

Hey Aslam, can you please tell me bit more about following two files.

soapclient/SforcePartnerClient.php
connection.php

From where can I generate them, where should I place them? what are those etc.

SEO tools said...

Excellent pieces. Keep posting such kind of information on your blog. I really impressed by your blog.

tampa personal injury lawyer said...

I must say your article is astonishing. The clarity of it is simply amazing and no doubt you are a proficient in this field.

Aslam - The Alexendra said...

Thanks @SEO and @tampa

Anonymous said...

You just saved me a lot of time at a time when I have very little time available.
Much appreciated! Thanks for your time :-)

Anonymous said...

Aslam, you should not accept comments of SEO spammers. They just use general phrases to get links back to their services. They did not even understand what you were doing.

vasudha dharani said...

Salesforce Admin Online Training - 21st Century Software ...
www.21cssindia.com/courses/salesforce-admin-online-training-143.html
Salesforce admin online training by 21cssindia the largest institute in providing online trainings in all technologies. Salesforce admin training, salesforce ...
salesforce developer online training| salesforce developer ...
www.21cssindia.com/.../salesforce-developer-online-training-144.html
21cssindia provides Salesforce developer online training by real time Experts. Call us +91 9000444287 for online training and demo. Online Salesforce ...
salesforce crm online training| salesforce crm training| call ...
www.21cssindia.com/courses/salesforce-crm-online-training-212.html
21cssindia provides Salesforce crm online training by real time Experts. Call us +91 9000444287 for online training and demo. Online Salesforce crm training ...

Raj Solanki said...

Nice post on Salesforce CRM to get useful info. Thank you very much for the informative post.

Salesforce CRM Services

karthik anu said...

I’ve been browsing on-line greater than three hours today, but I never discovered any attention-grabbing article like yours. It is beautiful worth sufficient for me. Personally, if all webmasters and bloggers made good content material as you did, the net will be a lot more helpful than ever before.
iOS Training in Chennai
Android Training in Chennai
php Training in Chennai

Priya Kannan said...

This information really worth saying, i think you are master of the content and thank you so much sharing that valuable information and get new skills after refer that post.
PHP Training in Chennai

Post a Comment