Home » Php » php – How to GET parameters from AFNETWORKING POST

php – How to GET parameters from AFNETWORKING POST

Posted by: admin July 12, 2020 Leave a comment

Questions:

How can I get the the parameter values from Swift, the file upload is already working, I’ve tried $_GET[“familyId”], but it didn’t work?

Swift:

    let manager = AFHTTPRequestOperationManager()
    let url = "http://localhost/test/upload.php"
    var fileURL = NSURL.fileURLWithPath(NSBundle.mainBundle().pathForResource("test_1", ofType: "mov")!)
    var params = [
        "familyId":locationd,
        "contentBody" : "Some body content for the test application",
        "name" : "the name/title",
        "typeOfContent":"photo"
    ]

    manager.POST( url, parameters: params,
        constructingBodyWithBlock: { (data: AFMultipartFormData!) in
            println("")
            var res = data.appendPartWithFileURL(fileURL, name: "fileToUpload", error: nil)
            println("was file added properly to the body? \(res)")
        },
        success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in
            println("Yes thies was a success")
        },
        failure: { (operation: AFHTTPRequestOperation!, error: NSError!) in
            println("We got an error here.. \(error.localizedDescription)")
    })

PHP:

 $target_dir = "uploads/";
 $target_dir = $target_dir . basename($_FILES["fileToUpload"]["name"]);

 if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_dir)) {
echo json_encode([
    $user_info;
    "Message" => "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.",
    "Status" => "OK",
     ]);
How to&Answers:

The issue that you’re facing is you’re looking the in wrong place. As you’ve said, you’re using AFNetworking’s POST method to POST the data. Emphasis on POST. GET and POST are two totally different things. GET is used to retrieve values stored in the url, such as www.example.com/example-get.php?key1=value1&key2=value2. You’d access the different values in PHP by doing $_GET['key1']. POST is something different. This is sent along with the HTTP message body, and can NOT be seen in browsing history or in the url. You’d be able to access your data by using $_POST['familyId']. I’d suggest reading into it a little more, so I’ll provide this to start off with.

Best of luck to you.

Answer:

Your main problem is that the variables are in $_POST and you are looking for the in $_GET.

I can see from the code that the data is being sent in the post body of the request. Which is basically what jkaufman has said in his answer.

Try some variant of:

<?php
    $FamilyID = $_POST['familyId'];
    // ... do stuff with it ...
?>

You may find these links informative (as you have asked for credible and/or official sources).