Wednesday, December 23, 2015

Connect iOS device to HTTP GET/POST PHP service (Raspberry Pi Part)

This post shows how to create a PHP service on Raspberry Pi for iPhone / iPad to access using HTTP GET / POST methods.

Raspberry Pi Part


1. Make sure that Apache and PHP have been installed.

2. Create the ios.php file with this command:

sudo nano /var/www/ios.php

4. Edit and save the file as below:

<?php
if (isset($_GET[language]) and isset($_GET[product])) {
    print "GET:";
    print "\nYour language: " . $_GET[language];
    print "\nYour product: " . $_GET[product];
} else if (isset($_POST['name']) and isset($_POST['age'])) {
    print "Post:";
    print "\nYour name: " . $_POST['name'];
    print "\nYour age: " . $_POST['age'];
} else {
    print "Wrong request";
}
?>

In this example, parameters used in the GET method are language and product, while parameters used in the POST method are name and age.

5. Test the GET method with the browser:

Invalid HTTP GET:
http://192.168.0.149/ios.php


Valid HTTP GET:
http://192.168.0.149/ios.php?language=objective-c&product=applewatch 

iOS Part

1. Write a simple iOS app. See this:


2. Test the app. The results:

GET:


POST:



Reference:
PHP Manual

2 comments: