0

I have looked all over and want to do a simple upload from an iPhone app of a string. I have used all the code online. The iPhone works. It retrieves the code from the php file and prints it to the console. But the value in the php script returns empty.

<?php

$string = $_POST["string"];

    if(strlen ($string)==0){
       echo"empty";
    }else{
       echo "The string is: ". $string;  
}
?>

Here is the iOS Code

 - (void) sendCode{
// Create your request string with parameter name as defined in PHP file
NSString *myRequestString = [NSString stringWithFormat:@"string=%@&",signInCode.text];

// Create Data from request
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://artistfolio.net/signin.php"]];
// set Request Type
[request setHTTPMethod: @"POST"];
// Set content-type
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody: myRequestData];
// Now send a request and get Response
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(conn)
{
    NSLog(@"Good");
}
else
{
    NSLog(@"Bad");
}
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
// Log Response
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",response);

}

Kyle Griffith
  • 114
  • 10

2 Answers2

0

Perhaps iOS send data into the body of the request. Try file_get_contents('php://input') into server side. Or change Content-Type: application/x-www-form-urlencoded header into client side.

On the second case, you should read: POST request using application/x-www-form-urlencoded

Community
  • 1
  • 1
C Würtz
  • 856
  • 9
  • 20
  • Regarding Content-type I have this setting in the client side. I don't fully understand what you mean by file_get_contents(www.example.com). I searched and found that it would be done on a website, but I'm trying to do it on the post from the iPhone. @C Würtz – Kyle Griffith Feb 23 '16 at 09:24
  • Can you add `NSLog(@"%@",myRequestString);` Then in PHP, add `var_dump(file_get_contents('php://input'), $_POST);` Just for checking. – C Würtz Feb 23 '16 at 10:15
  • In my iOS output is 'string(19) "string=Hello World&" array(1) { ["string"]=> string(11) "Hello World" } empty' and online its still empty – Kyle Griffith Feb 23 '16 at 21:35
  • I don't understand the end of the output. I think there is only one argument in `var_dump()`. Which one is it? – C Würtz Feb 24 '16 at 09:13
0

Check this Answer , Originally posted here.

PHP Code

<?php
 $title = $_POST['title'];
 $description = $_POST['description'];
 $city = $_POST['city'];

 echo "Title: ". $title;
 echo "Description: ". $description;
 echo "City: ". $city;
?>

iOS Side coding:

Objective C

// Create your request string with parameter name as defined in PHP file
NSString *myRequestString = [NSString stringWithFormat:@"title=%@&description=%@&city=%@",eventTitle.text,eventDescription.text,eventCity.text];

// Create Data from request
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://www.youardomain.com/phpfilename.php"]];
// set Request Type
[request setHTTPMethod: @"POST"];
// Set content-type
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody: myRequestData];
// Now send a request and get Response
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
// Log Response
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",response);

Swift

// Create your request string with parameter name as defined in PHP file
var myRequestString: String = "title=\(eventTitle.text!)&description=\(eventDescription.text!)&city=\(eventCity.text!)"
// Create Data from request
var myRequestData: NSData = NSData.dataWithBytes(myRequestString.UTF8String(), length: myRequestString.characters.count)
var request: NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: "http://www.youardomain.com/phpfilename.php")!)
// set Request Type
request.HTTPMethod = "POST"
// Set content-type
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "content-type")
// Set Request Body
request.HTTPBody = myRequestData
// Now send a request and get Response
var returnData: NSData = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
// Log Response
var response: String = String(bytes: returnData.bytes(), length: returnData.characters.count, encoding: NSUTF8StringEncoding)
NSLog("%@", response)
Community
  • 1
  • 1
Siba Prasad Hota
  • 4,779
  • 1
  • 20
  • 40
  • Then you might check my answer here : http://stackoverflow.com/questions/35570676/how-to-post-data-from-ios-app-to-mysql-database/35571006#35571006 I have two different methods to get it done , hope that might work. – Siba Prasad Hota Feb 24 '16 at 02:53