Salesforce & Google Maps Mashup using PHP

After reading this post, check out a more recent post on an improved mashup or visit the Arrowpointe’s product page for this functionality.

I have developed a mash-up of Salesforce.com and Google Maps. The script was developed in PHP and utilizes the PHP Nusoap Sforce Toolkit, created by Ryan Choi, to communicate with the Sforce API and Phoogle, developed by System 7 Designs, to communicate with the Google API.

Using the PHP Nusoap toolkit, this script logs into Salesforce and queries for all Account records that have a complete address. After setting up a Phoogle map instance, the script adds each address to the map. In addition to sending the map the latitude and longitude values, it also passes an HTML formatted string that populates each pin with the Account Name, Address and a link to the record in Salesforce.com.

Salesforce and Google Maps Mashup

The demo is now closed because my web host turned off calls to file_get_contents and I am not planning on re-writing the functionality. I noticed that the code is not running properly on IE. It gets an error and says “Operation Aborted”. I am not sure why. I have found a bunch of posts giving some solutions, but none of them seem to work. Still working on that.

The script has been tested on PHP 4.3.11, 4.4.1 and PHP5. In order for this to work on PHP5, you must download the latest version of the code, which came out on 12/6/2005. Older versions of the code need to use PHP4 because the soapclient class conflicts with another class in many PHP5 installs.

The script is free for download and licensed under the GPL. To get this setup on your own webserver, do the following:

  1. Download the scripts. The zip file contains all of the files you need.

    The included files unique to my development are:

    • MapAccounts.php: This is the primary script.
    • google-map-api-key.php: Holds the Google Maps API key for the web directory.
    • phoogle-extensions.php: I had to extend the Phoogle class.

    The PHP NuSoap Sforce toolkit code was not changed. The master source for this code, as well as other sample files for it, are at its Sourceforge page. The included PHP NuSoap Sforce toolkit files are:

    • salesforce.php: Primary file that defines the salesforce class.
    • nusoap.php
    • partner.wsdl

    I had to make some changes to the Phoogle files. I have communicated these changes to the Phoogle developers so they may get incorporated. The master source for the Phoogle code and documentation on its functionality is located here. The included Phoogle files are:

    • phoogle.php
  2. Sign-up for a Google API key. Google requires you to have a new API key for every web server directory you use.
  3. Enter your API key into the google-map-api-key.php file.
  4. Open the MapAccounts.php file and search for the phrase Enter a default username here. Update the file with your username.
  5. Open the MapAccounts.php file and search for the phrase Enter a default password here. Update the file with your password.
  6. Load all of these files to your web server.
  7. Run the MapAccounts.php file

Phoogle geocodes addresses using the Yahoo Geocode API. I have noticed that the current code has some trouble with multi-line addresses. If you are receiving errors, that could be why.

Please comment on this post and let me know what you think. Also, comment here and add links to how you have used this script or modified it. Have fun!

Updated 12/8/2005:
I made some small tweaks to the code that allows it to be run from within a custom tab so that it leverages the existing Salesforce session and applies all of the user’s visibility settings. I was able to get it working by having a Custom Tab with the following URL:

http://www.arrowpointe.com/dev/SalesforceGoogleMap/MapAccounts.php?sess= {!API_Session_ID}&srvr={!API_Partner_Server_URL_60}

You are welcome to try it out with the URL at arrowpointe.com if you want to, but I encourage you to host the PHP files yourself and link to your own files. My pages are NOT running in HTTPS. That’s why you should host them yourself. Also, I could change the code in my files at any time, so your link might break too.

In case you didn’t notice when looking at the code, you can alternatively pass in a username and password using the “un” and “pw” parameters the same way you can do it to log into Salesforce.

Note that the code is sensitive and, therefore, only pulls back Accounts with a complete addresses. Also, things like multi-line addresses or addresses that cannot be geocoded will result in script errors. I don’t have the error handling working now and this is not intended to be service I am providing at this time.

It works in PHP5 now too. The Salesforce PHP Nusoap package was updated on Sourceforge.

24 Comments

  1. ryan choi Said,

    December 2, 2005 @ 3:42 pm

    “It has not been tested on PHP5 because I have been unable to get the Salesforce PHP Nusoap toolkit to run on PHP5.”

    I’ve made some fixes to make it work with PHP5, but I’m curious to know if you have the same or different problems.

    The problem I saw (and fixed) was related to sObjects not having all of the fields filled out.

    Feel free to email me at the address I provided, or the address in the code.

    Thanks!

    Ryan

  2. Scott Hemmeter Said,

    December 9, 2005 @ 7:58 am

    Ryan updated the PHP Nusoap code to run on PHP5. Download the 12/6/2005 version.

  3. bill Said,

    February 23, 2006 @ 6:37 pm

    Added

    $BillingStreet = str_replace( “\n”, “”, $BillingStreet );

    at line 107 (right after the “extract” to cure the mult-line address problem.

  4. Scott Hemmeter Said,

    February 24, 2006 @ 8:45 am

    The demo and downloadable code has been updated with this change. Thanks for the tip, bill.

  5. Peder Jakobsen Said,

    April 3, 2006 @ 6:08 pm

    I keep getting this error, even though the user loggin in has accounts.

    I’ve tried many combinations in the query field, and many types of querys that work in all my other PHP code. But none of them seem to work.

    Notice: Undefined index: result in C:\htdocs\maps\salesforce.php on line 225
    No records were found that met the criteria of your query.

    Any ideas? Your help is much appreciated.

    P.J. Ottawa, Canada

  6. Peder Jakobsen Said,

    April 3, 2006 @ 6:29 pm

    Finally got the query working, but now I keep getting these two errors:

    Notice: Undefined offset: 3 in C:\htdocs\maps\phoogle.php on line 307

    and this:

    Warning: file_get_contents(http://api.local.yahoo.com/MapsService/V1/geocode?appid= YahooDemo&location=%2CSanto+Domingo%2CD.N.%2C00000) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in C:\htdocs\maps\phoogle-extensions.php on line 41

  7. Jim Thompson Said,

    April 6, 2006 @ 11:34 am

    Thanks Scott. I was able to get this working fairly easily, even reading from custom objects.

  8. Ray Masa Said,

    April 8, 2006 @ 11:37 am

    Hi,

    This is a very cools script. However, since I don’t use Salesforce (or any other CRM), I thought I might be able to modify your script to work with my own database (which has addresses) and display them on the map.

    However, I have not been able to modify it. I keep getting different errors (depending on what I change). Wondering if you could point me in the right direction.

    First (since I am not connecting to Salesforce.com), I removed the following required file from the MapAccounts.php
    salesforce.php

    And replaced a different required file (db.php), which includes connection information to my database.

    Then I removed the salesforce specific code from the MapAccount.php:
    From Line 33: // Create Salesforce object and set the default batch size.

    To Line 80: } else { // successful login

    Then I changed the $queryString =, to query fields in my database.

    This is as far as I have been able to figure out. What am I missing? Have I removed things I should not have?

    Thanks for your help.

    Ray

  9. Scott Hemmeter Said,

    April 8, 2006 @ 12:27 pm

    Re: Ray Masa’s question…

    I think you are close. It does not appear that you have deleted anything you should not have. A few things still reference the salesforce.php file objects.

    • Line 81 ($queryResult =) is using the Salesforce tookit syntax to run the query method.
    • Line 86 (if ($queryResult[‘size’] > 0)) – The size parameter is returned in the salesforce query call. You need another way to determine if you have records.
    • Line 98 ($records = $queryResult[‘records’];) – The records array is returned in the salesforce query call. Your resultset might not have a records array.
    • The WHILE loop from lines 100 to 111 references the records array and Salesforce specific variables created from the extract function

    That’s a start.

    You might find a better example for a starting poing at the phoogle page.

  10. Ray Masa Said,

    April 9, 2006 @ 1:34 pm

    Cool….Thanks Scott. I will look through those lines as well as the phoogle page.

    Ray

  11. Ray Masa Said,

    April 10, 2006 @ 5:12 pm

    Hi Scott,

    Just a quick updated, looked through the Phoogle documentations and emailed Justin (Phppgle developer) and he was a great help as well. Among you, him and the documentations and scripts on your and his site I was able to get it working using my own database.

    Thanks again,

    Ray

  12. George Morris Said,

    April 26, 2006 @ 8:56 am

    Is an Sforce account required for this? I just tried it with a client’s salesforce.com username and password and I get “Login failed for user”

    What am I overlooking? Would it be the face that our hosted scripts are behind a firewall?

  13. Scott Hemmeter Said,

    April 26, 2006 @ 9:11 am

    George Morris, are you using the URL I have indicated above or did you download the files and install them on your webserver?

    The URL above is not supported. It was used as a test and you are welcome to try it, but I cannot confirm that it will work correctly. Using the downloaded files should work.

    Your client’s username and password should work fine. You client should be on the Enterprise or Ultimate Editions. These are the only ones that have access to the Sforce API. If you client is using the professional edition, then this solution will not work because the API is not enabled without an additional fee.

    Another option for Google Mapping your data is to use a Salesforce S-Control. Check it out here.

  14. George Morris Said,

    April 26, 2006 @ 9:13 am

    Yep, they are on professional! Thanks for the tip.

  15. Lou Goddard Said,

    May 22, 2006 @ 1:59 pm

    I’m getting this error sixteen times:
    Notice: Undefined offset: 3 in c:\Inetpub\wwwroot\phoogle.php on line 307

    I’ve downloaded the files and hosted them with 2003 Standard Server running PHP 5.1.4 on IIS 6. I have edited the files to include my Google API key as well as my username and password. The scirpt is not working from both within Salesforce and externally. Any suggestions are welcome.

    Thanks in advance,
    Lou Goddard

  16. pardeep Said,

    August 12, 2006 @ 7:05 am

    i am using googlemap api named ‘GoogleMapAPI.class.php’ but i m getting wrong locations. Can any body help me out??

  17. pardeep Said,

    August 12, 2006 @ 7:10 am

    i m using ‘GoogleMapAPI.class.php’ downloaded from internet but i m getting wrong locations. or we can say its locating on wrong directions

  18. Scott Hemmeter Said,

    August 23, 2006 @ 9:00 am

    pardeep: I have recently switched my preference from Phoogle to GoogleMapAPI.class.php, which you mention, and I have had good success with it.

    Can you provide more explanation as to what is not working properly? Maybe some examples.

    I have found that it works best using the Google geocoding service rather than Yahoo. Yahoo only supports the US. Google supports the US and a lot more. I would start there. Then make sure you are sending a street address, city, state (if applicable), postal code and country (use the ISO codes if you can). separate each value with spaces. It should work fine.

  19. ActiveSalesForce : un connecteur rails sur SalesForce at Shoob Said,

    September 3, 2006 @ 8:22 am

    […] Info via le blog Arrowpointe qui vient de mettre à jour son mashup GoogleMaps-SalesForce. […]

  20. Adam F Said,

    October 27, 2006 @ 8:51 am

    Is there a way to actually do everything inside SalesForce using an s-control? I saw that someone had done it about a year ago, but since then all of the pages have been removed. I really would like to contain everything inside the SalesForce system to maintain tight security and easy accessibility.

  21. Scott Hemmeter Said,

    October 27, 2006 @ 9:01 am

    Adam F:

    Salesforce had developed something a while back to do this. I know they have it working, but I don’t know of many people ever got this to work successfully for themselves. I think it has a lot of dependencies. I am sure it is possible, but not something I have done.

    What I have done is just as easy, but it works in PHP rather than s-controls. It uses the Salesforce API for communicating with Salesforce and the PHP toolkit to communicate with the API. It is currently not setup to use SSL, but it could be. The authentication to Salesforce could also be setup to be based upon a current Salesforce session. In other words, a user would access it via a web tab and the only way the code could access the salesforce database is to leverage the currently logged in users session. If someone went to the page without having logged into Salesforce.com, they’d get an error.

    Lastly, the files sit on your webserver, not mine. You can do anything you want with them once they are on your site.

  22. W2F Said,

    February 5, 2007 @ 2:00 am

    Hello all,

    I want to get the PHP developer tool provided by Sales Force to use the API which can edit the contacts and leads within Sales Force.

    I have tried running the script on PHP 5.2 on my machine (file SforcePartnerClientTest.php), but i keep getting the error on connection that says:

    Warning: require_once(PHPUnit2/Framework/TestCase.php) [function.require-once]: failed to open stream: No such file or directory in C:\wamp\www\salesforce update\test\SforcePartnerClientTest.php on line 2

    Can anybody help?

    Thanks

  23. Jordan Thompson Said,

    April 4, 2008 @ 2:44 pm

    Hi, Is anyone looking at doing this with Quickbooks? A Mashup for their customer list would be GREAT. I am not sure if it is a proprietary format and DB and therefore impossible. If so we will have to wait on QB to do it. Any thoughts would be great. Thank you !!!

  24. dania lee Said,

    July 22, 2009 @ 10:01 pm

    hye, how can I use my own map instead of google map with salesforce application using php?

RSS feed for comments on this post