gpt4 book ai didi

php - 在 Alamofire 中快速发送参数

转载 作者:行者123 更新时间:2023-11-30 11:46:57 25 4
gpt4 key购买 nike

我想在请求中发送两个值:

1) 操作字符串

2)用户对象

即使我在参数中包含了“操作”:“注册”,我仍收到消息操作未设置

我是阿拉莫菲尔的新手。谁能给我解释一下:

1)如何在请求中发送值?

2)如何发送用户对象?

3) 如何处理结果.Success.Failure

Swift 代码:

let urlString = URLFactory()
let url = URL(string: urlString.getAppURL())!
print("Log url: \(url)")

let user = User()
user.setEmail(email: email)

let parameters: Parameters = ["operation": "register", "user": user]
Alamofire.request(url, method: .post, parameters: parameters).responseJSON { response in
print("Log \(response)")
print("Log response.request: \(response.request)")
print("Log response.error: \(response.error)")
print("Log response.data: \(response.data)")
print("Log response.result: \(response.result)")
}

快速输出:

Log url: http://192.168.0.101/GeolocationNews/NewsDataCrawler/app.php
Log SUCCESS: {
message = "Invalid Parameters";
result = failure;
}
Log response.request: Optional(http://192.168.0.101/GeolocationNews/NewsDataCrawler/app.php)
Log response.error: nil
Log response.data: Optional(51 bytes)
Log response.result: SUCCESS

PHP 代码:

$login = new Login();
$fun = new FunctionsValidation();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$data = json_decode(file_get_contents("php://input"));

if(isset($data->operation)) {
$operation = $data->operation;
if(!empty($operation)) {
if($operation == 'register') {
echo $login->register($data);
}
} else { // if operation is empty
$response["result"] = "failure";
$response["message"] = "Operation is empty";
echo json_encode($response);
}
} else { // if operation is not set
$response["result"] = "failure";
$response["message"] = "Operation is not set";
echo json_encode($response);
}
}

更新

我已经通过Postman发送测试了API:

{
"operation": "register",
"user":
{
"email": "email value"
}
}

它给了我:{"result":"failure","message":"Invalid Email"} 所以 API 运行良好!

我尝试过仅通过参数中的操作发送 Alamofire 请求,并且它有效。所以看来问题在于将用户对象转换为字典。谁能给我一个如何做到这一点的例子?

用户对象:

class User: NSObject {

private var name: String,
email: String,
password: String,
oldPassword: String,
newPassword: String,
code: String
private var id: Int

override init() {
self.name = ""
self.email = ""
self.password = ""
self.oldPassword = ""
self.newPassword = ""
self.code = ""
self.id = 0
}
// set and get methods ...
}

最佳答案

我认为问题出在编码上。根据您的 PHP 代码,它接受 application/json 作为内容类型,并且应使用 JSON 编码通过 Almofire 发送。

试试这个:

Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default)
.responseJSON { response in
print("Log \(response)")
print("Log response.request: \(response.request)")
print("Log response.error: \(response.error)")
print("Log response.data: \(response.data)")
print("Log response.result: \(response.result)")
}

引用: https://github.com/Alamofire/Alamofire/blob/master/Documentation/Usage.md#parameter-encoding

关于php - 在 Alamofire 中快速发送参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48727144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com