Fresh Beginning
  • Home
  • Portfolio
  • Buy Me A Coffee
  • Patreon
  • Speaking
  • Reading
  • About Me
Subscribe
iOS

POSTing parameters to PHP from an iOS app

  • Jayesh Kawli

Jayesh Kawli

Oct 10, 2015 • 1 min read
POSTing parameters to PHP from an iOS app

Recently I ran into an interesting problem on how to POST parameters from an iOS app (I was using AFNetworking for communications) to PHP. Looks like it's quite straightforward for GET requests such that you can easily retrieve parameters with $_GET['parameter_name'].

But for POST it's little complicated. Here's how I did it.

Objective-C :

Here's a code I used to POST parameters to PHP server script for further processing.

I am using AFNetworking-RACExtensions to make network requests

Initialize AFHTTPRequestOperationManager with required parameters and acceptable content types,


AFHTTPRequestOperationManager* manager = [[AFHTTPRequestOperationManager alloc] init];   
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"application/json", nil];

Send the request with required parameters


[[[manager] rac_POST:[remote_url] parameters:@{@"value": @"100"}] subscribeNext:^(id x) {
    NSLog(@"Operation succeeded with response %@", [x first]);
} error:^(NSError* error) {
    NSLog(@"Failed with an error %@", [error localizedDescription]);
}];

And here's PHP script to retrieve these values,

PHP


  // Input paylaod received from client
  $handle = fopen("php://input", "rb");
  $post_data = '';
  while (!feof($handle)) {
      $post_data .= fread($handle, 8192);
  }
  fclose($handle); 
  $postDataDictionary = json_decode($raw_post_data, true);

Once you get the hold of $postDataDictionary which holds POSTed parameters, you can safely retrieve its value as follows


$value = $postDataDictionary['value']; // $value = 100. 

Hope this will help someone who is writing an iOS app with PHP as a backend (Of course without any backend framework. If you're using any PHP framework to build a backend, I don't think you have to go through all this PHP boilerplate code)

Please check out my Patreon and BuyMeACoffee pages too.
If you like my articles and want to keep me going on this journey, please consider donating on these platforms.
Your support in any form is much appreciated.
Buy me a coffee support

Patreon support

Sign up for more like this.

Enter your email
Subscribe
Portfolio

Portfolio

Hello, I am Jayesh Kawli. I have been writing iOS applications for the last 9 years. During this time, I got a chance to work on iOS applications from various domains. Below is the list of all the apps I was responsible for developing and releasing in the app store.
Jan 30, 2023 6 min read
Using MVVM Architecture with SwiftUI - Clean Architecture

Using MVVM Architecture with SwiftUI - Clean Architecture

MVVM has been a choice of architecture in iOS development for a long time. It survived the Objective-C era and has been quite popular in Swift world too. However, things became slightly complicated when SwiftUI came along. It was a bit confusing to see how it can be used in
Oct 24, 2022 8 min read
A Better Way to Generate Reuse Identifiers in Swift and iOS

A Better Way to Generate Reuse Identifiers in Swift and iOS

iOS offers an elegant way to improve table view and collection view performance by cell reuse. These scroll views may have millions of cells, but iOS only stores the visible cells in memory and reuses them as they go out of view. This enables OS to save on memory and
Oct 20, 2022 2 min read
Fresh Beginning © 2023
Powered by Ghost