Fresh Beginning
  • Home
  • Portfolio
  • My Clothing Shop
  • Etsy Clothing Store
  • Buy Me A Coffee
  • Patreon
  • Speaking
  • Reading
  • About Me
  • Contact 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
Announcing my Clothing Shop - Burnousstraat Designs

Announcing my Clothing Shop - Burnousstraat Designs

Life is too short to wear boring clothes - Anonymous Hey folks, this is going to be a slightly different post than usual. I've got some exciting news to share with you all – we've officially launched Burnousstraat Designs, your new go-to destination for one-of-a-kind and unique graphic apparel! At Burnousstraat
Mar 20, 2024 2 min read
Using RxSwift to Populate Data on UITableView (iOS and Swift)

Using RxSwift to Populate Data on UITableView (iOS and Swift)

It's been a while since I posted. But better late than never. In today's blog post, I am going to write about how to use RxSwift to populate data in UITableView on iOS. RxSwift is a library for composing asynchronous and event-based code by using observable sequences and functional style
Oct 26, 2023 6 min read
SwiftUI: Apply Custom Formatting to Localized Strings

SwiftUI: Apply Custom Formatting to Localized Strings

In today's post, I am going to touch on a niche area in SwiftUI designs and string formatting. This is about how to apply custom formatting to localizable strings. It sounds complicated, so let me explain it with an example, Suppose I have a localizable string like this, Going from
Sep 27, 2023 6 min read
Fresh Beginning © 2025
Powered by Ghost