Temboo makes it easy to build PHP applications that connect to over 100 web-based resources and services (e.g. Facebook, Dropbox, US Census data) by standardizing how you interact with their Application Programming Interfaces (APIs). Don't worry if you're not familiar with APIs – with Temboo you don't have to worry about the details.
Here we'll show you how to use the Temboo PHP SDK to write a simple PHP script that uses Google's Geocoding API to retrieve the latitude and longitude for a specific address e.g., 104 Franklin St, New York City. What makes Temboo uniquely powerful and useful is that, once you know how to use one API, you know how to work with any API in our Library.
Before we get started, make sure that you've got PHP 5 (or later) and a PHP code editor. We've used Sublime Text for these tutorials but they will work equally well in whatever IDE you are using.
1 Create a new PHP file in your preferred editor.
2Download the Temboo PHP SDK.
3 Unzip the downloaded SDK and move the resulting
php-sdk folder into the same parent folder as your blank script.
4 Add the Temboo PHP SDK to your script:
6 Go to the Google > Geocoding > GeocodeByAddress Choreo in our Library.
7 Enter any address or ZIP code in the Address input field e.g., 104 Franklin Street, New York City.
8 Now click Run. After a moment you'll see the data that Google sends back shown in the Output section.
When you run any Choreo from our website, we automatically generate code that can be used to make the same API call many languages, including PHP. Here we'll show you how to use these snippets in your code.
9 Scroll down to the Code section of the Library page, click on the Language drop-down menu, and select PHP.
10 Copy the code snippet and paste it into your PHP script. At this point, your code should look something like this:
<?php require 'php-sdk/src/temboo.php'; // Instantiate the Choreo, using a previously instantiated Temboo_Session object, eg: // $session = new Temboo_Session("ACCOUNT_NAME", "APP_NAME", "APP_KEY"); $geocodeByAddress = new Google_Geocoding_GeocodeByAddress($session); // Get an input object for the Choreo $geocodeByAddressInputs = $geocodeByAddress->newInputs(); // Set inputs $geocodeByAddressInputs->setAddress("104 Franklin St., New York NY 10013"); // Execute Choreo and get results $geocodeByAddressResults = $geocodeByAddress->execute($geocodeByAddressInputs)->getResults(); ?>
Before we try a test run of the script, let's update the code to print the results returned from Google's Geocoding service.
Each Choreo in the PHP SDK returns a
Temboo_Results subclass that contains get methods tailored specifically to the outputs of that Choreo. Using the
Temboo_Results, you can retrieve the raw data (typically XML or JSON) returned by a third-party API or relevant fields that we've parsed out of the response for you.
To print the latitude and longitude returned by the Choreo, simply do the following:
11 Add the following code at the bottom of your script:
// Print lat and lon echo "Latitude: " . $geocodeByAddressResults->getLatitude() . "\n"; echo "Longitude: " . $geocodeByAddressResults->getLongitude() . "\n";
12 Run the code and you will see the results in the console. Congrats! You just ran your first Choreo from our PHP SDK.
Now you're ready to run any of our 2000+ Choreos in PHP. You're just a few steps away from making something extraordinary.
Once you've got your code up and running, you're ready to move on and do more. From monitoring your running applications, to moving your generated Temboo code to your preferred development environment and sharing it with colleagues, collaborators and friends - we've got you covered.
We're always happy to help. Just email us at firstname.lastname@example.org, and we'll answer your questions.