Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to GET parameters from AFNETWORKING POST

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",
     ]);
like image 594
MasterWizard Avatar asked Apr 17 '15 16:04

MasterWizard


2 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.

like image 132
Jeffrey Avatar answered Nov 12 '22 14:11

Jeffrey


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).

  • What is the difference between POST and GET?
  • http://php.net/manual/en/reserved.variables.post.php
  • http://developer.infoconnect.com/get-vs-post-%E2%80%93-restful-primer
  • http://www.w3schools.com/tags/ref_httpmethods.asp
  • http://www.programmerinterview.com/index.php/general-miscellaneous/html-get-vs-post/
  • http://www.diffen.com/difference/GET_%28HTTP%29_vs_POST_%28HTTP%29
  • http://blog.teamtreehouse.com/the-definitive-guide-to-get-vs-post
like image 1
Matthew Brown aka Lord Matt Avatar answered Nov 12 '22 15:11

Matthew Brown aka Lord Matt