0

I am having trouble with my php file reading JSON data. I use swift to send data to a server. I encode the request and when I send it as a string everything works fine. My String that works fine in PHP looks like this

id=e61db0&time=Feb 13, 2015 so this works fine when received by PHP

However, when I use dictionary and encode NSData my PHP file is not able to read it. I decoded the data that I send and it looks like that:

{"id":"e61db0", "time":"Feb 13, 2015"} this doesn't work when received by php

For my request setting headers I do this:

var imageData = UIImageJPEGRepresentation(ui_image, 0.5)
let base64encoded = imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.allZeros)
var params = ["id": uid, "time": date, "comment": img_com_e, "image": base64encoded]
var request:NSMutableURLRequest = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"    
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
var error: NSError?
        request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: .allZeros, error: &error)
        if let error = error {
            println("\(error.localizedDescription)")
        }
        let dataTask = session.dataTaskWithRequest(request) { data, response, error -> Void in
            let res = response as NSHTTPURLResponse!;
            NSLog("Response code: %ld", res.statusCode);

        if (res.statusCode >= 200 && res.statusCode < 300)
        {
            var responseData:NSString  = NSString(data: data, encoding:NSUTF8StringEncoding)!
            return_val = responseData
            NSLog("Response ==> %@", responseData);
        } else {
            return_val = "bad"
            //println(return_val)
        }

    }

    dataTask.resume()

Not sure what is going on. Does my server PHP file does not recognize that it is JSON data and can't accept $_POST['id']. Should I set my headers somehow differently?

4
  • take a look on: stackoverflow.com/questions/24566180/… Commented Feb 13, 2015 at 17:50
  • Hi Kim, I saw that. that example has it as a string. I updated my question a little. My 'params' is Dictionary Commented Feb 13, 2015 at 17:59
  • 1
    Have you tried json_decode()? Commented Feb 13, 2015 at 18:04
  • I was thinking that I can set it somehow to that it reads JSON directly, since I have same php file reading info from android, I guess I just set it up little bit modified version with Json_decode() for iOS. It looks like I am sending info from Android little bit differently. Commented Feb 13, 2015 at 18:44

1 Answer 1

1

Where is your php code?

If you send Content-Type: application/json

You should read php://input not $_POST

try:

$data = json_decode(file_get_contents('php://input'), true);
print_r($data);
echo $data["id"];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.