国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊 How To Write A Simple PHP/MySQL Web Service for an iOS App

How To Write A Simple PHP/MySQL Web Service for an iOS App

Jun 06, 2016 pm 08:01 PM
mysql php simple web write

Twe Web Services Rule! As an iPhone/iPad developer, it can be really useful to be able to write your own simple web services that integrate with your apps. For example, you may wish to display some news updates that come from your web serv

Twe

How To Write A Simple PHP/MySQL Web Service for an iOS App

Web Services Rule!

As an iPhone/iPad developer, it can be really useful to be able to write your own simple web services that integrate with your apps.

For example, you may wish to display some news updates that come from your web server, and display it on startup. Or perhaps store some user data “in the cloud”. Your imagination is the only limit!

In this first tutorial in this two-part series, you’ll go step-by-step through the process of creating a simple web service, based on a promo code system I included in my latest app,?Wild Fables. In the next part of this series, you’ll write an iOS app that integrates with this web service!

To run through all of the steps on this tutorial, you’ll need a web server with MySQL and PHP. If you do not have a web server already, you have three options:

  • If you want to?enable Apache/MySQL/PHP directly on your Mac?(for free), there are lots of good guides out there, here’s?one I found?with a quick Google search.
  • If you want to?rent a web server?online (usually for $$), there are many good choices out there, but the one I personally use (and enjoy) is Linode – check?this tutorial?for more information.
  • And if you’re?just too lazy?to do either of the above, you can just read through the steps below, and use the web service I’ve already made in part 2 of series :]

You don’t necessarily need to know PHP or MySQL to go through this tutorial (although it will be helpful!), as the tutorial includes all of the code you’ll need.

What You’ll Make

As you might know already if you’ve added?In-App Purchases?into your app, there is no built-in system provided by Apple to give out promo codes for your in-app purchases.

However, it can be extremely helpful to build your own promo code system for your in-app purchases, for several reasons:

  1. First, it’s great to be able to give out promo codes for in-app purchase content to professional app reviewers.
  2. Second, it’s also nice to be able to give out promo codes to your friends so they can check out your app.
  3. Finally, if you build your system right, it also provides a great way to track out which of your marketing vectors pay off (and which don’t) – more on this in the second part of the series!

So in this tutorial, we’re going to build a system where you can enter a code into your app, and it will connect to a web service to see if the code is valid or not. If it’s valid, the app will then “unlock” some content.

Don’t worry if you have no plans on adding this particular system into your app – you’ll learn the general technique of developing a web service and integrating it with an iPhone app as well!

Creating the Database

The first step of this project is to create the database tables you’ll need. For the purposes of this tutorial, you’ll need three database tables:

  • rw_app: A table to keep track of the apps we’re using the promo code system for. This way, you can use the same database tables for multiple apps.
    • id: Unique id for the app.
    • app_id: Unique string identifying the app (mainly for your own purposes).
  • rw_promo_code: A table to keep track of what promo codes are available.
    • id: Unique id for the code.
    • rw_app_id: The app id this code is for (from rw_app).
    • code: Alphanumeric code that the user types in to unlock something.
    • unlock_code: Alphanumeric string you’ll pass back to the app so it can know what to unlock.
    • uses_remaining: You’ll set things up so that codes can be used more than once – this way you can give out the same code to all of our friends on Twitter, for example. You’ll use this field to specify how many uses the code should have, and every time it is used this will be decremented by one. When iti hits 0, the code is no longer valid.
  • rw_promo_code_redeemed: A table to keep track some info each time a promo code that is redeemed. This will help us prevent one device from redeeming the same code multiple times (if it’s a multi-use code), by simply checking to see if the device has already used the code.
    • id: Unique id for the app.
    • rw_promo_code_id: The id of the promo code redeemed (from rw_promo_code).
    • device_id: The device identifier of the redeemer.
    • redeemed_time: A timestamp of when the code was redeemed.

Here are the MySQL statements you’ll need to create these tables:

DROP TABLE IF EXISTS rw_promo_code;
DROP TABLE IF EXISTS rw_app;
DROP TABLE IF EXISTS rw_promo_code_redeemed;

CREATE TABLE rw_promo_code (
    id mediumint NOT NULL AUTO_INCREMENT PRIMARY KEY,	
    rw_app_id tinyint NOT NULL, 
    code varchar(255) NOT NULL,
    unlock_code varchar(255) NOT NULL,
    uses_remaining smallint NOT NULL
);

CREATE TABLE rw_app (
    id mediumint NOT NULL AUTO_INCREMENT PRIMARY KEY,	
    app_id varchar(255) NOT NULL
);

CREATE TABLE rw_promo_code_redeemed (
    id mediumint NOT NULL AUTO_INCREMENT PRIMARY KEY,	
    rw_promo_code_id mediumint NOT NULL,
    device_id varchar(255) NOT NULL,
    redeemed_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

On your web server, you need to create a MySQL database and create the three tables as specified above. The way you do this varies depending on your web host, but just in case it’s useful I’ll tell you the steps I use on my web host, where I have full command-line access.

I save all of the above SQL statements to a file called create.sql, then I create and populate a new database with the following commands:

rwenderlich@kermit:~$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1286
Server version: 5.1.37-1ubuntu5.1-log (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database promos;
Query OK, 1 row affected (0.00 sec)

mysql> use promos;
Database changed
mysql> grant all privileges on promos.* to 'username'@'localhost' identified by 'password';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

rwenderlich@kermit:~$ mysql -u username -p promos  use promos;
Database changed
mysql> show tables ;
+------------------------+
| Tables_in_promos       |
+------------------------+
| rw_app                 | 
| rw_promo_code          | 
| rw_promo_code_redeemed | 
+------------------------+
3 rows in set (0.00 sec)

Now you should have three empty database tables in a new database. Next, go ahead and add a test app and a test code with the following statements:

INSERT INTO rw_app VALUES(1, 'com.razeware.test');
INSERT INTO rw_promo_code VALUES(1, 1, 'test', 'com.razeware.test.unlock.cake', 10000);

OK! Now that the database is connected and populated, on to writing the PHP web service!

Verifying PHP/MySQL functionality

Before you start implementing the PHP web service, first run a quick check to make sure PHP is working on your server OK. Create a new directory on your web server called promos, and create a new file inside called index.php with the following:

[php]?view plaincopy

  1. "margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline">??
  2. ???
  3. "margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline">class?RedeemAPI?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">{??
  4. ????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-family:inherit;?vertical-align:baseline;?color:rgb(102,102,102)">//?Main?method?to?redeem?a?code??
  5. ????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline">function?redeem"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">("margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">)?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">{??
  6. ????????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(177,177,0)">echo?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,255)">"Hello,?PHP!""margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">;??
  7. ????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">}??
  8. "margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">}??
  9. ???
  10. "margin:0px;?padding:0px;?border:0px;?outline:0px;?font-family:inherit;?vertical-align:baseline;?color:rgb(102,102,102)">//?This?is?the?first?thing?that?gets?called?when?this?page?is?loaded??
  11. "margin:0px;?padding:0px;?border:0px;?outline:0px;?font-family:inherit;?vertical-align:baseline;?color:rgb(102,102,102)">//?Creates?a?new?instance?of?the?RedeemAPI?class?and?calls?the?redeem?method??
  12. "margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,136)">$api?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">=?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline">new?RedeemAPI"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">;??
  13. "margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,136)">$api"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">->"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,64,0)">redeem"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">("margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">)"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">;??
  14. ???
  15. "margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline">?>??

This is a very basic PHP file that create an instance of a class (RedeemAPI) and calls a method on it that just outputs “Hello, PHP!”

You can test this by navigating to the URL on your web server with your browser. Even better, you can test this on the command line with a handy utility called curl similar to the following (but replace the URL with your own):

Ray-Wenderlichs-Mac-mini-2:~ rwenderlich$ curl http://www.wildfables.com/promos/
Hello, PHP!

Next, extend the class to make sure the service can connect to your database OK by replacing the RedeemAPI class with the following:

[php]?view plaincopy

  1. "margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline">class?RedeemAPI?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">{??
  2. ????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline">private?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,136)">$db"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">;??
  3. ???
  4. ????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-family:inherit;?vertical-align:baseline;?color:rgb(102,102,102)">//?Constructor?-?open?DB?connection??
  5. ????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline">function?__construct"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">("margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">)?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">{??
  6. ????????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,136)">$this"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">->"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,64,0)">db?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">=?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline">new?mysqli"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">("margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,255)">'localhost'"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">,?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,255)">'username'"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">,?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,255)">'password'"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">,?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,255)">'promos'"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">)"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">;??
  7. ????????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,136)">$this"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">->"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,64,0)">db"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">->"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,64,0)">autocommit"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">("margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">FALSE"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">)"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">;??
  8. ????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">}??
  9. ???
  10. ????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-family:inherit;?vertical-align:baseline;?color:rgb(102,102,102)">//?Destructor?-?close?DB?connection??
  11. ????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline">function?__destruct"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">("margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">)?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">{??
  12. ????????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,136)">$this"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">->"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,64,0)">db"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">->"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,64,0)">close"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">("margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">)"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">;??
  13. ????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">}??
  14. ???
  15. ????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-family:inherit;?vertical-align:baseline;?color:rgb(102,102,102)">//?Main?method?to?redeem?a?code??
  16. ????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline">function?redeem"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">("margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">)?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">{??
  17. ????????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-family:inherit;?vertical-align:baseline;?color:rgb(102,102,102)">//?Print?all?codes?in?database??
  18. ????????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,136)">$stmt?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">=?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,136)">$this"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">->"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,64,0)">db"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">->"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,64,0)">prepare"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">("margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,255)">'SELECT?id,?code,?unlock_code,?uses_remaining?FROM?rw_promo_code'"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">)"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">;??
  19. ????????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,136)">$stmt"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">->"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,64,0)">execute"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">("margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">)"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">;??
  20. ????????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,136)">$stmt"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">->"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,64,0)">bind_result"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">("margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,136)">$id"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">,?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,136)">$code"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">,?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,136)">$unlock_code"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">,?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,136)">$uses_remaining"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">)"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">;??
  21. ????????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(177,177,0)">while?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">("margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,136)">$stmt"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">->"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,64,0)">fetch"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">("margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">)"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">)?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">{??
  22. ????????????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(177,177,0)">echo?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,255)">"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,102,153)">$code?has?margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,102,153)">$uses_remaining?uses?remaining!""margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">;??
  23. ????????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">}??
  24. ????????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,136)">$stmt"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">->"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,64,0)">close"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">("margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">)"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">;??
  25. ????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">}??
  26. "margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">}??

This adds a constructor that connects to your database given a username and password and a destructor that closes the database connection. The redeem loop is modified to run a MySQL statement to select all of the entries in rw_promo_code, and loop through to print a line about each entry.

Once again you can test this with curl to make sure it’s working:

Ray-Wenderlichs-Mac-mini-2:~ rwenderlich$ curl http://www.wildfables.com/promos/
test has 10000 uses remaining!

Web Service Stragegy: GET vs POST

OK, now that we know things are working, it’s almost time to implement the full behavior. But first, let’s talk about our strategy for the web service.

We know we need to pass some data from the iPhone app to our web service. Specifically, we need to tell the web service the ID of the app, the code to redeem, and the device id that is trying to redeem.

But how can we pass this data? If you aren’t familiar already, there are two ways to pass data to a web service – via GET (the normal way), or via POST (typically used for posting data to a web form).

Depending on which one you choose, the parameters get passed differently:

  • If you choose GET, the parameters are part of the URL.
  • If you choose POST, the parameters are passed as part of the request body.

Either one would work, but usually when you’re trying to “do something” like redeem a code (rather than just passively retrieving data), you would pick the POST method, so that’s what we’re goint to do here.

What does this mean in practice? All it means is if we want to access the passed parameters in PHP, we get them from the built in $_POST array, as follows:

[php]?view plaincopy

  1. "margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,136)">$_POST"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">["margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,255)">"rw_app_id""margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">]??

And when we’re using ASIHTTPRequest to connect to the web service later, we’ll use the ASIFormDataRequest class, which sends the request as a POST, as follows:

[objc]?view plaincopy

  1. ASIFormDataRequest?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,34,0)">*request?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,34,0)">=?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,34,0)">[ASIFormDataRequest?requestWithURL"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,34,0)">:url"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,34,0)">];??
  2. "margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,34,0)">[request?setPostValue"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,34,0)">:"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-family:inherit;?vertical-align:baseline;?color:rgb(17,116,10)">@"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(191,29,26)">"1"?forKey"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,34,0)">:"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-family:inherit;?vertical-align:baseline;?color:rgb(17,116,10)">@"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(191,29,26)">"rw_app_id""margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,34,0)">];??

For more information on GET vs POST, check out the?Wikipedia entry?on the HTTP protocol.

Update:?Also, check out @smpdawg’s comment in the?forum topic?for this tutorial some additional must-read tips and info on this!

Web Service Strategy: The Algorithm

Next, let’s go over the general algorithm the web service will take:

  1. Make sure the required parameters are passed in via POST.
  2. Make sure the code is actually in the database.
  3. Make sure the code still has uses remaining.
  4. Make sure the device hasn’t already used the code.
  5. If we got this far, success!
    • Add an entry into rw_promo_code_redeemed to track the redemption.
    • Decrement the uses_remaining in rw_promo_code.
    • Return the unlock_code to the app, so it can give content to the user.

OK now that we have a strategy in hand, onto the implementation!

Web Service Implementation

First, add two helper methods to the top of your PHP file that you’ll use to easily return HTTP status messages on success and failure:

[php]?view plaincopy

  1. "margin:0px;?padding:0px;?border:0px;?outline:0px;?font-family:inherit;?vertical-align:baseline;?color:rgb(102,102,102)">//?Helper?method?to?get?a?string?description?for?an?HTTP?status?code??
  2. "margin:0px;?padding:0px;?border:0px;?outline:0px;?font-family:inherit;?vertical-align:baseline;?color:rgb(102,102,102)">//?From?http://www.gen-x-design.com/archives/create-a-rest-api-with-php/???
  3. "margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline">function?getStatusCodeMessage"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">("margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,136)">$status"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">)??
  4. "margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">{??
  5. ????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-family:inherit;?vertical-align:baseline;?color:rgb(102,102,102)">//?these?could?be?stored?in?a?.ini?file?and?loaded??
  6. ????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-family:inherit;?vertical-align:baseline;?color:rgb(102,102,102)">//?via?parse_ini_file()...?however,?this?will?suffice??
  7. ????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-family:inherit;?vertical-align:baseline;?color:rgb(102,102,102)">//?for?an?example??
  8. ????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,136)">$codes?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">=?"http://www.php.net/array"?style="margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,104,55);?text-decoration:none">"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(153,0,0)">Array"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,153,0)">(??
  9. ????????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(204,102,204)">100?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">=>?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,255)">'Continue'"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">,??
  10. ????????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(204,102,204)">101?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">=>?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,255)">'Switching?Protocols'"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">,??
  11. ????????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(204,102,204)">200?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">=>?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,255)">'OK'"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">,??
  12. ????????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(204,102,204)">201?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">=>?"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(0,0,255)">'Created'"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(51,153,51)">,??
  13. ????????"margin:0px;?padding:0px;?border:0px;?outline:0px;?font-style:inherit;?font-family:inherit;?vertical-align:baseline;?color:rgb(204,102,204)">202?
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Why We Comment: A PHP Guide Why We Comment: A PHP Guide Jul 15, 2025 am 02:48 AM

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

How to Install PHP on Windows How to Install PHP on Windows Jul 15, 2025 am 02:46 AM

The key steps to install PHP on Windows include: 1. Download the appropriate PHP version and decompress it. It is recommended to use ThreadSafe version with Apache or NonThreadSafe version with Nginx; 2. Configure the php.ini file and rename php.ini-development or php.ini-production to php.ini; 3. Add the PHP path to the system environment variable Path for command line use; 4. Test whether PHP is installed successfully, execute php-v through the command line and run the built-in server to test the parsing capabilities; 5. If you use Apache, you need to configure P in httpd.conf

PHP Syntax: The Basics PHP Syntax: The Basics Jul 15, 2025 am 02:46 AM

The basic syntax of PHP includes four key points: 1. The PHP tag must be ended, and the use of complete tags is recommended; 2. Echo and print are commonly used for output content, among which echo supports multiple parameters and is more efficient; 3. The annotation methods include //, # and //, to improve code readability; 4. Each statement must end with a semicolon, and spaces and line breaks do not affect execution but affect readability. Mastering these basic rules can help write clear and stable PHP code.

python if else example python if else example Jul 15, 2025 am 02:55 AM

The key to writing Python's ifelse statements is to understand the logical structure and details. 1. The infrastructure is to execute a piece of code if conditions are established, otherwise the else part is executed, else is optional; 2. Multi-condition judgment is implemented with elif, and it is executed sequentially and stopped once it is met; 3. Nested if is used for further subdivision judgment, it is recommended not to exceed two layers; 4. A ternary expression can be used to replace simple ifelse in a simple scenario. Only by paying attention to indentation, conditional order and logical integrity can we write clear and stable judgment codes.

PHP 8 Installation Guide PHP 8 Installation Guide Jul 16, 2025 am 03:41 AM

The steps to install PHP8 on Ubuntu are: 1. Update the software package list; 2. Install PHP8 and basic components; 3. Check the version to confirm that the installation is successful; 4. Install additional modules as needed. Windows users can download and decompress the ZIP package, then modify the configuration file, enable extensions, and add the path to environment variables. macOS users recommend using Homebrew to install, and perform steps such as adding tap, installing PHP8, setting the default version and verifying the version. Although the installation methods are different under different systems, the process is clear, so you can choose the right method according to the purpose.

What is PHP and What is it Used For? What is PHP and What is it Used For? Jul 16, 2025 am 03:45 AM

PHPisaserver-sidescriptinglanguageusedforwebdevelopment,especiallyfordynamicwebsitesandCMSplatformslikeWordPress.Itrunsontheserver,processesdata,interactswithdatabases,andsendsHTMLtobrowsers.Commonusesincludeuserauthentication,e-commerceplatforms,for

Your First PHP Script: A Practical Introduction Your First PHP Script: A Practical Introduction Jul 16, 2025 am 03:42 AM

How to start writing your first PHP script? First, set up the local development environment, install XAMPP/MAMP/LAMP, and use a text editor to understand the server's running principle. Secondly, create a file called hello.php, enter the basic code and run the test. Third, learn to use PHP and HTML to achieve dynamic content output. Finally, pay attention to common errors such as missing semicolons, citation issues, and file extension errors, and enable error reports for debugging.

How Do You Handle File Operations (Reading/Writing) in PHP? How Do You Handle File Operations (Reading/Writing) in PHP? Jul 16, 2025 am 03:48 AM

TohandlefileoperationsinPHP,useappropriatefunctionsandmodes.1.Toreadafile,usefile_get_contents()forsmallfilesorfgets()inaloopforline-by-lineprocessing.2.Towritetoafile,usefile_put_contents()forsimplewritesorappendingwiththeFILE_APPENDflag,orfwrite()w

See all articles