Parsing JSON in PHP


A lot of APIs will give you responses in JSON format. Here we'll review JSON parsing in PHP so that you can get to the interesting data faster.

This tutorial assumes that you've already gone through our PHP getting started tutorial and are familiar with how our PHP SDK works. Although we use the output from our YouTube ListSearchResults Choreo in this tutorial, the same steps we outline here will work for parsing any JSON in PHP.

Get JSON output

1 Log in to Temboo and go to the YouTube > Search > ListSearchResults Choreo in our Library.

2 Enter any search term you want for the Query input and click Run.

3 You get a whole bunch of JSON in the Response output. These are the results of the search. Next we'll see how to parse through this response in PHP and pick out only the pieces we're interested in.

Parse it in PHP

4 Create a new PHP script and copy in the code below, making sure to substitute in your Temboo account details. In steps 5-8, we'll go over what happens in the code.

<?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");
$listSearchResults = new YouTube_Search_ListSearchResults($session);

// Get an input object for the Choreo
$listSearchResultsInputs = $listSearchResults->newInputs();

// Set inputs
$listSearchResultsInputs->setQuery("quantum entanglement");

// Execute Choreo and get results
$listSearchResultsResults = $listSearchResults->execute($listSearchResultsInputs)->getResults();

// Parse JSON response
$results = json_decode($listSearchResultsResults->getResponse());

// Get items array
$items = $results->items;

// Get first item
$item = $items[0];

// Get the snippet object within the first item
$snippet = $item->snippet;

// Get the title and description fields
$title = $snippet->title;
$description = $snippet->description;

// Print title and description
echo $title . "\n";
echo $description . "\n";

?>

5First we converted the JSON text from the response to a PHP object using json_decode.

6 Next, we parsed out the piece of data we want from the JSON. It helps to look at the JSON file's structure to get an idea of how it is organized. The two main elements you should look for are:

PHP typeHow it appears in the JSON file
Array"name": [
Object"name": {

For these results, we wanted the title of the first video in the search results. We got the items array, then the first item in that array (at index 0). Then we wanted the snippet object within the first item in the array.

We could also have traversed the parsed JSON structure more compactly with:

$snippet = $results->items[0]->snippet;

7 To finish up, we got the title property from the snippet object.

8All finished! Run the code in your PHP IDE to try it out. You should see the title of your first YouTube Search result in the console.

What next?

Now you should be to able to parse all sorts of JSON responses with our PHP SDK. Check out the 2000+ Choreos in our Library and find some exciting data to parse.

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.

Get Live Updates About Your Application

Integrate Temboo with Slack to get up-to-the-second notifications and alerts about your deployed Temboo code.

Commit Code to GitHub

Commit Temboo-generated code directly to GitHub and share with the world. Learn more.

Need help?

We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.


Back