伙计们,我有使用swift3通过alamofire 4发布json数据的问题,以及使用php在XAMPP服务器端检索json数据的问题。 我的swift 3代码确实触发了XAMPP的PHP脚本,但不知怎的,我无法通过php中的$ _POST变量获取它 这是我的代码,
func uploadImage(image: UIImage){
//Now use image to create into NSData format
let imageData:NSData = UIImagePNGRepresentation(image)! as NSData
//convert the nsdata to base64 encoded string
let strBase64:String = imageData.base64EncodedString(options: .lineLength64Characters)
// let parameters = ["image": strBase64] as Dictionary
let parameters = ["image": strBase64]
print(strBase64)
Alamofire.request("http://localhost/Test/api/UploadPhoto.php",method: .post, parameters: parameters, encoding: JSONEncoding.default).response { response in
print(response)
}
}
这是我的服务器端代码(脚本确实是由来自alamofire的调用触发的,但不知怎的,我只能通过调用$ _POST [" image"]来获取数据)
<?php
//scripts below did get triggered, but can't get the json data through calling $_POST["image"];
$imageString = $_POST["image"];
$filename_path = md5(time().uniqid()).".png";
$data = base64_decode($imageString);
file_put_contents('../AllImages/'.$filename_path, $data);
echo json_encode($_POST["image"]);
?>
如果可能的话,请帮助我,我已经挣扎了近一个星期,但却找不到很多线索
谢谢
答案 0 :(得分:1)
我找到了解决这个问题的方法,基本上,我使用urlsession.shared.datatask帮助我而不是alamofire发布请求, 这是我的ios侧码
func uploadImage(image: UIImage, completionHandler: @escaping (String) ->()){
// Now use image to create into NSData format
let imageData:NSData = UIImagePNGRepresentation(image)! as NSData
//convert the nsdata to base64 encoded string
let strBase64:String = imageData.base64EncodedString(options: .lineLength64Characters)
// prepare json data
let json: [String: Any] = ["image": strBase64]
let jsonData = try? JSONSerialization.data(withJSONObject: json)
// create post request
let url = URL(string: "http://10.10.10.72/Test/api/UploadPhoto.php")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
// insert json data to the request
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
do {
guard let data = data else {
throw JSONError.NoData
}
guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: AnyObject] else {
throw JSONError.ConversionFailed
}
completionHandler(json["sign"] as! String)
} catch let error as JSONError {
print(error.rawValue)
} catch let error as NSError {
print(error.debugDescription)
}
}
task.resume()
}
我使用字典来存储我的数据,并将其转换为json数据格式以发送到服务器
let json: [String: Any] = ["image": strBase64]
let jsonData = try? JSONSerialization.data(withJSONObject: json)
然后在php端,我使用
检索它 $entityBody = file_get_contents('php://input');
,然后我从json解码,它生成了一个数组,我可以通过引用图像访问我的值,所以完整的PHP代码如下:
<?php
//get the posted json data
$entityBody = file_get_contents('php://input');
//decode the json data
$decoded = json_decode($entityBody, TRUE);
$imageString = $decoded["image"];
//create a unique name for the image
$filename_path = md5(time().uniqid()).".png";
//converted the image string back to image
$data = base64_decode($imageString);
//put it on the desired location
file_put_contents('../AllImages/uploads/signature/'.$filename_path, $data);
$response = array();
//create the response
$response['sign'] = '../AllImages/uploads/signature/'.$filename_path;
echo json_encode($response);
?>
请注意这里,我再次编码json数据作为从php到我的ios端的响应发送回来,你需要解码来自json的响应,所以完整的想法是如果你将值编码为json来自一方面,你需要从另一侧解码它才能正确访问价值,如果我错了就纠正我,我很高兴我的应用程序现在启动并运行所有请求:D