伙计们,我遇到了通过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"]);
?>
如果可能的话,请帮助我,我已经挣扎了近一个星期,但找不到很多线索谢谢
我找到了解决此问题的方法,基本上,我使用urlsession.shared.datatask来帮助我,而不是用post请求来帮助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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句