从Swift到PHP的字典 [英] Post dictionary from Swift to PHP
问题描述
具体来说,任务是更新托管数据库上的许多字段。而不是将每个字段的新值定义为单独的参数,我希望通过一个字典,其中键是字段名称,值是新值。这个数据已经以Swift的 Dictionary< String,String>
的形式存在,如何以下列格式将其粘贴到php网址:
http://server/directory/targetScript.php/?recordId = \(recId)& newFieldArray = \(itemDict)
其中:
recId
=正在更新的记录ID(作为与字典一起传递单独变量的示例)
itemDict
= Swift中生成的字典
以下是当前代码 ...返回一个错误,显示 url
为nil
class func updateItemInfo(recordId:String,updatedDict:Dictionary< String,String>) - > Void {
// recordId =要更新的项目的FileMaker recordId
// updatedDict =当前项的primaryKey
//直接到php文件位置
let server:String =myServer.com
let phpFile:String =/php/UpdateRecord.php
let baseURL = NSURL(string:http:// \ (server)\(phpFile))
let url = NSURL(string:?recordId = \(recordId)& newData = \(updatedDict),relativeToURL:baseURL)
let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
var request = NSMutableURLRequest(URL:url !, cachePolicy:cachePolicy,timeoutInterval:20.0)
request.HTTPMethod =POST
//设置数据
var dataString =
let requestBodyData =(dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPBody = requestBodyData
var response:NSURLResponse? = nil
var error:NSError? = nil
让回复:NSData! = NSURLConnection.sendSynchronousRequest(request,returnsResponse:& response,error:& error)
let result = NSString(data:reply !, encoding:NSUTF8StringEncoding)
println(results)
}
为什么不使用JSON传递值。
- 转换字典到JSON字符串。
- 如果您以查询字符串的形式发送此数据;您必须对其进行URL编码
- 最后你可以发送给PHP;它将作为$ _GET中的单个参数达到。
您将需要PHP的 json_decode 来获取关联的数组从
How can a dictionary generated in Swift be posted as parameter in a PHP URL?
Specifically, the task is to update many fields on a hosted database. Rather than define new values for each field as a separate parameter, I am looking to pass a dictionary where the keys are the field names and the values are the new values. This data already exists as a Dictionary<String, String>
in Swift - how can it be tacked on to the php url in the following format:
"http://server/directory/targetScript.php/?recordId=\(recId)&newFieldArray=\(itemDict)"
Where:
recId
= the record id that is being updated (as an example of passing a separate variable along with the dictionary)
itemDict
= the dictionary generated in Swift
Here is the current code: ... which returns an error showing url
as "nil"
class func updateItemInfo ( recordId:String, updatedDict:Dictionary<String, String> ) -> Void {
//recordId = the FileMaker recordId of the item being updated
//updatedDict = the primaryKey of the current item
//direct to php file location
let server:String = "myServer.com"
let phpFile:String = "/php/UpdateRecord.php"
let baseURL = NSURL(string: "http://\(server)\(phpFile)")
let url = NSURL(string: "?recordId=\(recordId)&newData=\(updatedDict)", relativeToURL: baseURL)
let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
var request = NSMutableURLRequest(URL: url!, cachePolicy: cachePolicy, timeoutInterval: 20.0)
request.HTTPMethod = "POST"
// set data
var dataString = ""
let requestBodyData = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPBody = requestBodyData
var response: NSURLResponse? = nil
var error: NSError? = nil
let reply: NSData! = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&error)
let results = NSString(data:reply!, encoding:NSUTF8StringEncoding)
println(results)
}
Why don't you use JSON to pass the values.
- Convert the Dictionary to JSON String.
- If you are sending this data as a query string; you will have to URL encode it
- Finally you can send it to PHP; where it will reach as a single parameter in $_GET. There you will need PHP's json_decode to get an associated array back from it.
这篇关于从Swift到PHP的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!