1

I have a simple PHP service file set up with this code:

<?php 

clearstatcache();


if ($_GET["method"] == "register") { 
    registerUser($_POST);
}

function registerUser($arrUser) { 

    $json = json_encode($arrUser);
    echo $json;


}

?>

and in my app I am calling it with this code:

func postData(dictData:Dictionary<String, Any>, method:String) {

    //set up the request

    let myURL = URL(string:"http://www.xxxxxxxxx.com/cs_services.php?method=register")

        var myRequest = URLRequest(url:myURL!)

        //post data
        var strPostParams = " "
        for(key, value) in dictData {
            strPostParams = strPostParams + "\(key)=\(value)&"
        }

        myRequest.httpMethod = "POST"
        myRequest.httpBody = strPostParams.data(using: .utf8)
        myRequest.cachePolicy = .reloadIgnoringLocalAndRemoteCacheData

        let myDataTask = URLSession.shared.dataTask(with: myRequest) { (data, response, error) in

            if error != nil {
                print(error?.localizedDescription)
            }

            guard let data = data, let response = response  else {
                print("error with data or response")
                return
            }

            do {

                let jsonResponse = try JSONSerialization.jsonObject(with: data, options: .allowFragments)

                let strData = String(data: data, encoding: String.Encoding.utf8) as String!

                if let dictJSON = jsonResponse as? [String: Any] {
                    print("Dictionary:\(dictJSON)\n\(String(describing: strData))")
                } else {
                    print("JSON Response:\(jsonResponse)")
                }

            } catch {
                let strData = String(data: data, encoding: String.Encoding.utf8) as String!
                print("json error: \(error.localizedDescription)\n\(String(describing: strData))")
            }
        }

        myDataTask.resume()



}

Previously I was testing the code and just returning array("name" => "test") in the PHP file. That worked fine as I was able to see that return in my app code. But now that I've changed it to return the POST data, I am still getting the name=test return.

Testing in the browser the PHP file outputs as expected. I'm not sure what to do in the app though. I have the URLRequest cache policy set to ignore local and remote. I reset the data in the sim. Switched from testing iPad Air to iPad Air2 to tethered iPad, same results.

Any help would be appreciated.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
PruitIgoe
  • 6,166
  • 16
  • 70
  • 137

1 Answers1

0

Chalk this up to developer error...I was pointing to an old file.

PruitIgoe
  • 6,166
  • 16
  • 70
  • 137