通过$ _POST从alamofire swift3的发布请求中检索json数据?

伙计们,我遇到了通过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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Alamofire POST 请求 URL

来自分类Dev

POST请求Swift 3.0 Alamofire

来自分类Dev

通过WebSocket发送的JSON数据在Swift中发送POST请求

来自分类Dev

alamofire通过肥皂发送请求

来自分类Dev

使用alamofire发出POST请求

来自分类Dev

通过Alamofire发送json数组

来自分类Dev

Swift Alamofire POST请求变为GET

来自分类Dev

Swift Alamofire POST请求变为GET

来自分类Dev

通过POST方法的Alamofire NSURLRequest不起作用

来自分类Dev

通过POST方法的Alamofire NSURLRequest不起作用

来自分类Dev

通过Alamofire在SWIFT中使用Twitter API

来自分类Dev

通过Alamofire在SWIFT中使用Twitter API

来自分类Dev

Alamofire POST请求正在进行

来自分类Dev

状态码 405 问题..Alamofire POST 请求...

来自分类Dev

使用 Alamofire 向 API 发送 POST 请求

来自分类Dev

Alamofire POST方法中的POST对象数组-Swift / IOS

来自分类Dev

通过JSTL访问发布数据-Angular $ post

来自分类Dev

在PHP中使用“ POST”在C#中通过JSON检索数据库信息

来自分类Dev

在PHP中使用“ POST”在C#中通过JSON检索数据库信息

来自分类Dev

jQuery-通过POST(ajax)发送JSON数据并在php中检索

来自分类Dev

在PHP中使用“ POST”在C#中通过JSON检索数据库信息

来自分类Dev

jQuery-通过POST(ajax)发送JSON数据并在php中检索

来自分类Dev

swift 无法挖掘通过 alamofire 获得的 JSONDecoder 数据

来自分类Dev

通过移动数据发送POST请求

来自分类Dev

Solr:通过 HTTP POST 通过 JSON 请求进行分组

来自分类Dev

在$ Codeigniter中通过$ _POST发布HTML

来自分类Dev

Swift:通过分页JSON API使用Alamofire和SwiftyJSON

来自分类Dev

如何在Vuex中通过POST请求发送状态数据?

来自分类Dev

通过 Json Post 请求登录 iCloud

Related 相关文章

热门标签

归档