简体   繁体   English

PHP Wamp服务器未从AlamoFire请求接收参数

[英]PHP Wamp server not receiving parameters from AlamoFire request

When I receive a request from AlamoFire the script in PHP not recognize the parameter neither in POST, GET nor REQUEST variable. 当我收到AlamoFire的请求时,PHP中的脚本无法识别POST,GET或REQUEST变量中的参数。

The code in AlamoFire is AlamoFire中的代码是

let parameters: [String: AnyObject] =
        ["email" : email,
         "password": password]

    Alamofire.request(.POST, url, parameters: parameters, encoding: .JSON)
        .responseJSON { response in

            print(response)

            switch response.result {

            case .Success: // Connection stablished



            case .Failure(let error): //  Connection not stablished
               print(error)
               self.showLoginError(Constants.connectionError)
            }
    }

And the code in php is: php中的代码是:

$response = array();
header('Content-Type: application/json');
if (isset($_POST['email']) && isset($_POST['password'])) {
  ... stuff
}else{
    $response["Failure"] = 1;
    $response["message"] = "Required field(s) is missing";
    echo json_encode($response);
}

Then response in AlamoFire always is the JSON with message of required field 然后,AlamoFire中的响应始终是带有必填字段消息的JSON

At one point PHP gave me this error: PHP Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. 有一次,PHP给了我这个错误: 不推荐使用PHP:不推荐使用自动填充$ HTTP_RAW_POST_DATA并将在以后的版本中将其删除。 To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. 为避免此警告,请在php.ini中将“ always_populate_raw_post_data”设置为“ -1”,并改用php:// input流。 in Unknown on line 0 在第0行的Unknown中

I had change the php.ini with the option of always_populate_raw_post_data to -1 and reach me to the state what I am now. 我将使用always_populate_raw_post_data选项的php.ini更改为-1,并使我达到现在的状态。

But if the request URL is like this http://192.168.1.108:8080/labor/login.php?email=slicingit@yahoo.com&password=brown1234 the server receive the parameter. 但是,如果请求URL像这样http://192.168.1.108:8080/labor/login.php?email=slicingit@yahoo.com&password=brown1234,则服务器会收到该参数。 Also if I create the request using NSURLSession with the parameters as part of the HTTP Body the server does get them. 同样,如果我使用带有参数的NSURLSession作为HTTP主体的一部分创建请求,服务器也会获取它们。

How do you know its not receiving it? 您怎么知道它没有收到? How do you know if alomfire is sending it? 您怎么知道alomfire是否正在发送它? use 'echo' see what alomfire brings back in your php script. 使用“ echo”查看alomfire在您的php脚本中带来了什么。 if it is indeed json values remember you have to decode the json values before you can use or assign them. 如果确实是json值,请记住您必须先解码json值,然后才能使用或分配它们。 How to decode json 如何解码json

 $mymail= ($_POST['email']);
 $mypass= ($_POST['password']);
 echo $mymail;
 echo $mypass;

 //Decode your json first here

 if ($mymail && $mypass) {
     ... stuff
 }else{
 $response["Failure"] = 1;
$response["message"] = "Required field(s) is missing";
echo json_encode($response);

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM