Fresh Beginning
  • Home
  • Speaking
  • Reading
  • Github
  • Alma Mater
  • Contact
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)

Sign up for more like this.

Enter your email
Subscribe
How to Use Gradients in SwiftUI - Radial Gradients

How to Use Gradients in SwiftUI - Radial Gradients

💡This is the second post in the series of Gradients in SwiftUI. If you want to learn about linear gradients in SwiftUI, please follow Linear Gradients in SwiftUI link to know more about itApple defines general gradient as, A color gradient represented as an array of color stops, each having

  • Jayesh Kawli
Jayesh Kawli May 17, 2022 • 8 min read
How to Use Gradients in SwiftUI - Linear Gradients

How to Use Gradients in SwiftUI - Linear Gradients

💡This is the first post in the series of Gradients in SwiftUI. If you want to learn about radial gradients in SwiftUI, please follow Radial Gradients in SwiftUI link to know more about itApple defines general gradient as A color gradient represented as an array of color stops, each having

  • Jayesh Kawli
Jayesh Kawli May 14, 2022 • 6 min read
The Correct Way to Dismiss Screen in SwiftUI

The Correct Way to Dismiss Screen in SwiftUI

In today's blog post, I am going to talk about the most commonly used API to dismiss presented or pushed view controllers on the screen in SwiftUI. However, when I say "talk", I am going to write about how that approach may not be suitable everywhere, what's the Gotcha behind

  • Jayesh Kawli
Jayesh Kawli May 11, 2022 • 8 min read
Fresh Beginning © 2022
Powered by Ghost