Advertisement
  1. Code
  2. PHP

Make an iPhone App Using the Envato API

Scroll to top
Read Time: 9 min

With the release of the new Envato Marketplace API, third-party developers now have access to a wealth of information to create all kinds of useful applications. This tutorial will teach you how to make your very own iPhone app using data from the new API. Even if you don't have an iPhone, you can still learn the basics about using PHP and JSON.

Before We Begin

The app we will be creating is based off of my iPhone app, Envato Marketplace Mobile. While we won't be recreating the entire app, this tutorial will give you a general idea on how it was created. This tutorial is aimed at PHP and JSON beginners and serves as a basis for creating more advanced PHP applications.

Using the API

Before we can start retrieving data using the API, we must learn how to go about accessing it. The API is split up into 5 parts:

  1. Version : determines what version of the API to use.
  2. Username : the user whose data you wish to access.
  3. API-key : similar to a password and is unique to each user and grants access to their data.
  4. set and format : determine what data to access and what format to provide it in.

For more information on the API, click here.

Step 1 - Accessing Our Data

Now that we know what is required to access the API, we are ready to put it into practice. Since we want others to use our awesome app, we need to get a username and API key on the fly; so we are going to retrieve some PHP variables from the browser.

1
2
if(isset($_GET['user']) && $_GET['user'] != "")
3
{
4
	$userName = $_GET['user'];
5
	if(isset($_GET['key']) && $_GET['key'] != "")
6
	{
7
		$apiKey = $_GET['key'];
8
	}
9
	else
10
	{
11
		echo 'Api key not set!';
12
		exit;
13
	}
14
}
15
else
16
{
17
	echo 'Username not set!';
18
	exit;
19
}

The above code checks to see if the variables user and key have been set - and exits if they haven't. It also checks to make sure that the global variable isn't empty.

Retrieving Our Data

Now that we have our required information, we can go ahead and use it to retrieve our data. To do this we will simply create a url using the format outlined in the introduction.

1
2
// Creates a string that will be used to access the API

3
$json_url = "http://marketplace.envato.com/api/edge/".$userName."/".$apiKey."/vitals+recent-sales.json";

Notice that we are using the "edge" version and are using our previous variables for the username and api-key. Finally, we are going to retrieve the vitals and recent sales data sets in the JSON format.

Now that we have created our url to access the API, we must read its contents so that they may be passed to the json_decode function - which requires an actual json data set, not just a url. So, let's go ahead and use the file_get_contents() function to read the contents of the url.

1
2
// Get the contents of the $json_url string

3
$json_contents = file_get_contents($json_url);
4
5
// A little error checking

6
if(!$json_contents)
7
{
8
	echo "Error: The JSON file could not be read. Please check your username and api key.";
9
	exit;
10
}

We are finally ready to convert the JSON data to an array so that we can use it in our application. To accomplish this task we will use the json_decode() function which will take the data from our $json_contents variable and output it to an array.

1
2
// Output our data to an array

3
$json_data = json_decode($json_contents, true);

We are using two parameters in this function, the first is the string we wish to decode, and the second tells the function to output the data as an array. That's it! We are now ready to show our data to the user.

Step 2 - Displaying Our Results

We've retrieved our data and now it's time to show it to the user. For the sake of simplicity we will be adding our frontend HTML code into the same file as our PHP code. So go ahead and add the standard HTML document code below your PHP code.

1
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
<html xmlns="http://www.w3.org/1999/xhtml">
4
<head>
5
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6
<title>My Nettuts iPhone App - <?php echo $userName; ?></title>
7
8
<link rel="stylesheet" href="style.css" type="text/css" />
9
</head>
10
11
<body>
12
<div id="header">My Nettuts iPhone App</div>
13
</body>
14
</html>

Notice that in the <title> tag, I added a simple PHP "echo" statement that will display the user's name in the browser's title bar.

Getting the Username and Balance

Let's go ahead and add a simple div that displays the username retrieved from the API. We could do this using the $userName variable like we used in the page title, however, using JSON is more exciting.

1
2
<div id="username"><?php echo $json_data['vitals']['username']; ?></div>

What the above code does is retrieve the username from the vitals array, which is an array within our $json_data array that was created earlier. Makes sense?

Here is the structure of the array used in the API example: Array ( [vitals] => Array ( [username] => ryan [balance] => 32.75 ) )

Next, we want to show the user his balance. This is done the same way as we displayed the username. This time, however, we will simply change from "username" to "balance". The rest is simply for layout and styling.

1
2
<div id="content">
3
<div class="line"><img src="bank_plus.png" alt="Balance" class="icon" />Balance: $<?php echo $json_data['vitals']['balance']; ?></div>
4
<h3>Recent Sales:</h3>

Listing Recent Sales

The last thing on our agenda is to display the most recent sales for a user. This is a little more complicated than the previous examples, however, if you've ever worked with arrays you should be able to handle it.

1
2
<?php

3
// List Recent Sales

4
$count = 1;

5
$salesArray = $json_data['recent-sales'];

6
foreach($salesArray as $value)

7
{

8
	if($count <= 10)

9
	{

10
		echo "<div class='line'><img src='plus.png' alt='Sale' class='icon' />Sold ".$value['item']." for <strong>$".$value['amount']."</strong></div>";

11
		$count = $count + 1;

12
	}

13
	else

14
	{

15
		break;

16
	}

17
}

18
?>
19
</div><!--End Content-->

There's a lot to digest here so lets start with the foreach statement. First we create a $salesArray from the recent-sales array within $json_data. This isn't necessary, but I think it looks cleaner. The foreach statement creates a $value for each row in the $salesArray array and allows us to pull information from each row. So, basically you use $value to get data from the row as the foreach statement loops through it.

Next up is the code which is executed each time the foreach statement goes through a row. Instead of showing all of the recent sales I only want to list ten, which is why, if the number of rows checked exceeds ten, it will break the loop. To change the number of rows the statement loops through, just change ten to the number you want. Actually displaying the rows is quite simple, as it requires just echoing out each row inside a div with a small icon. We use the $value to access the sale information from the array. We pull "item", which is the item title and "amount" which is the amount the author made on the sale. Finally "$count + 1" just increases the count of rows the statement has looped through. You should now be all set! Try loading the file on a testing server and see if it works!

If you don't have a Envato Marketplace account, you can use the example login:

  • user: ryan
  • key: 26k6otse2s586e4hcbzjy3quq830t3o4

Step 3 - Designing the Frontend

Adding CSS

Well, our new app works great, but it looks bad - which means it's time to break out some CSS. I assume you know enough to understand the following code as I won't be going into it except for a few things. #browser is used by the iPhone/Touch to make sure that if the page it is too small that it will fill the screen. body.lanscape #browser does the same, however, is for when the browser is in landscape mode.

1
2
body {
3
	background: #efefef;
4
	margin: 0px;
5
	padding: 0px;
6
	font-family: Helvetica;
7
	-webkit-touch-callout: none;
8
	-webkit-text-size-adjust: none;
9
	width: 100%;
10
	color: #2a2a2a;
11
}
12
13
#browser {
14
 	/* ensure we always fill the whole screen */
15
  	min-height: 416px;
16
}
17
18
body.landscape #browser {
19
  	min-height: 268px;
20
}
21
22
h3 {
23
	margin-bottom: 5px;
24
}
25
26
p {
27
	margin: 0 0 5px 0;
28
}
29
30
/*

31
	Layout

32
*/
33
34
#header {
35
	padding: 10px 5px 5px;
36
	height: 30px;
37
	color: #fff;
38
	font-size: 22px;
39
	background: url(header_bg.jpg) repeat-x;
40
}
41
42
#username {
43
	font-size: 18px;
44
	font-weight: bold;
45
	text-transform: uppercase;
46
	padding: 5px;
47
	/* WebKit supports text-shadow... so why not make it look pretty */
48
	text-shadow: 0 1px 0 rgba(0, 0, 0, 0.5);
49
	color: #fff;
50
	background: #498929;
51
	border-top: 1px solid #85c952;
52
	border-bottom: 1px solid #34661c;
53
}
54
55
#content {
56
	padding: 5px;
57
	padding-top: 10px;
58
}
59
60
.icon { vertical-align: text-top; margin-right: 5px; }
61
.line { padding-bottom: 5px; border-bottom: 1px solid #cccccc; margin-bottom: 5px;}

iPhone Time

It's time to make your great app finally iPhone compatible. This is actually really easy and involves only one line of code.

1
2
<meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">

Just add the above code right below the <title> tag and it should be the right size for your iPhone or iPod Touch. That code scales the page to the correct size for viewing on the iPhone. The last thing you may need to do is add a homepage icon so your app looks cool when someone adds your iPhone application as a webclip. To do this we add another small line of code that is similar to a favicon.

1
2
<link rel="apple-touch-icon" href="apple-touch-icon.png"/>

Well... we're all done! You can see the finished product below.

Conclusion

You've made it to the end. I hope this was a good introduction for those who are still in the beginning stages of using PHP and JSON. You're now ready to make your own killer app using the API and JSON in general.

Now remember, this tutorial is for beginners and those who need a jump-start for using the API.

  • Subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.


Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.