Alamofire没有发布到PHP脚本

时间:2017-01-29 15:59:36

标签: php swift post get alamofire

我正在创建一个快速的应用程序,我需要将数据发送到我自己的网络服务器 目前,我的快速代码看起来像这样。

fileprivate class AlamofireHelper {
     static var Manager : Alamofire.SessionManager = {
        // Create the server trust policies
        let serverTrustPolicies: [String: ServerTrustPolicy] = [
            "192.168.1.243": .disableEvaluation
        ]
        // Create custom manager
        let configuration = URLSessionConfiguration.default
        configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
        let man = Alamofire.SessionManager(
            configuration: URLSessionConfiguration.default,
            serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
        )
        return man
    }()

}
class UploadHelper
{

    private var _postData = [String:Any]()
    private let almoHelper = AlamofireHelper.Manager
    init(username:String) {
        _postData["username"] = username
        print("Setup")
        submitRequest()
    }

    func submitRequest()
    {
        print("Data is: \(_postData)")
        almoHelper.request("http:/192.168.1.243/networkStorage", method: .post, parameters: _postData, encoding: JSONEncoding.default).responseString { (responce) in
            print("Responce: \(responce)")
        }
    }
}

PHP:

<?php
    echo "POST: " . var_dump($_POST) . "\n\n\n\n";
    echo "GET: " . var_dump($_GET);
?>

唯一的问题是,当我运行swift代码let helper = UploadHelper(username: iProgram)时,我从PHP脚本得到以下响应:

Data: ["username": "iProgram"]
Responce: SUCCESS: array(0) {
}
POST: 



array(0) {
}
GET: 

为什么我的POST和GET变量为空?我知道GET是空的,因为我只发送POST数据,但POST不应该显示像&#34; username = iProgrammer&#34;?

最后,这是我从burp诉讼中捕获的HTTP请求:

GET /networkStorage/ HTTP/1.1
Host: 192.168.1.243
Accept: */*
User-Agent: Network%20Storage/1 CFNetwork/808.2.16 Darwin/16.3.0
Accept-Language: en-gb
Accept-Encoding: gzip, deflate
Connection: close

修改 出于某种原因,当我使用下面的代码并更改我的PHP脚本以显示&#39; GET&#39;变量,我可以看到它应该发送的数据。

almoHelper.request("http://192.168.1.243/networkStorage", method: .get, parameters: _postData).responseString { (myResponce) in
            let result = myResponce.result
            print("Result is: \(result.value!)")
        }

这是我从burp suite拦截的请求:

GET /networkStorage/?username=iProgram HTTP/1.1
Host: 192.168.1.243
Accept: */*
User-Agent: Network%20Storage/1 CFNetwork/808.2.16 Darwin/16.3.0
Accept-Language: en-gb
Accept-Encoding: gzip, deflate
Connection: close

2 个答案:

答案 0 :(得分:1)

首先,var_dump没有返回任何内容,它会输出结果,所以请使用

echo "POST: "; var_dump($_POST); echo "\n\n\n\n";

echo "POST: " . print_r($_POST, true) . "\n\n\n\n";

GET /networkStorage/ HTTP/1.1表示您发出GET请求,而不是POST。另外,您有错误的编码JSONEncoding.defaul。尝试稍微更改请求代码:

almoHelper.request("http:/192.168.1.243/networkStorage", method: .post, parameters: _postData, encoding: URLEncoding.httpBody).responseString { (responce) in
    print("Responce: \(responce)")
}

如果这不起作用,请尝试将网址从http:/192.168.1.243/networkStorage更改为http:/192.168.1.243/networkStorage/http:/192.168.1.243/networkStorage/index.php。 当您向/networkStorage发送请求时,这可能会解决问题,它会向/networkStorage/发送新请求,如burpsuit中捕获的数据所示。

答案 1 :(得分:0)

  

为什么我的POST和GET变量为空?我知道GET是空的,因为我只发送POST数据,但POST不应该显示像“username = iProgrammer”这样的东西吗?

没有。 $_POST仅填充HTML表单式编码,例如application / x-www-form-urlencoded或multipart / form-data。看起来你发送的是JSON,在这种情况下你需要阅读php://input流来获取数据。