我怎样才能迅速获得一个PHP变量 [英] How can I get a PHP variable over to swift
问题描述
我希望php中的一个变量能够迅速克服(或获取该变量的值).我该怎么办?
I want a variable in php to get over in swift (or get the value for the variable). How can I do that?
$name = "William";
如何在我的Swift脚本中获取字符串"William"?谁能帮我吗?
How can I get this string "William" to my Swift script? Can anyone help me?
我知道它与JSON和POST有关,但否则我完全迷路了.
I know it's something with JSON and POST or something but otherwise I am complete lost.
推荐答案
当您想将PHP中的数据获取到iOS设备时,我建议让PHP代码将其作为JSON发送. JSON对于客户端应用程序来说更容易解析(尤其是当您的Web服务响应变得更加复杂时),并且它使区分有效响应和某些常规服务器错误变得更加容易.
When you want to get data from PHP to an iOS device, I would recommend having the PHP code send it as JSON. JSON is easier for the the client app to parse (especially as your web service responses get more complicated) and it makes it easier to differentiate between a valid response and some generic server error).
要从PHP发送JSON,通常创建一个关联数组"(例如下面的$results
变量),然后调用json_encode
:
To send JSON from PHP, I generally create an "associative array" (e.g., the $results
variable below), and then call json_encode
:
<?php
$name = "William";
$results = Array("name" => $name);
header("Content-Type: application/json");
echo json_encode($results);
?>
此(a)指定一个Content-Type
标头,该标头指定响应将为application/json
; (b)然后对$results
进行编码.
This (a) specifies a Content-Type
header that specifies that the response is going to be application/json
; and (b) then encodes $results
.
传递到设备的JSON如下所示:
The JSON delivered to the device will look like:
{"name":"William"}
然后,您可以编写Swift代码来调用NSJSONSerialization
来解析该响应.例如,在Swift 3中:
Then you can write Swift code to call NSJSONSerialization
to parse that response. For example, in Swift 3:
let url = URL(string: "http://example.com/test.php")!
let request = URLRequest(url: url)
// modify the request as necessary, if necessary
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print("request failed \(error)")
return
}
do {
if let json = try JSONSerialization.jsonObject(with: data) as? [String: String], let name = json["name"] {
print("name = \(name)") // if everything is good, you'll see "William"
}
} catch let parseError {
print("parsing error: \(parseError)")
let responseString = String(data: data, encoding: .utf8)
print("raw response: \(responseString)")
}
}
task.resume()
或者在Swift 2中:
Or in Swift 2:
let url = NSURL(string: "http://example.com/test.php")!
let request = NSMutableURLRequest(URL: url)
// modify the request as necessary, if necessary
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
guard let data = data else {
print("request failed \(error)")
return
}
do {
if let json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String: String], let name = json["name"] {
print("name = \(name)") // if everything is good, you'll see "William"
}
} catch let parseError {
print("parsing error: \(parseError)")
let responseString = String(data: data, encoding: NSUTF8StringEncoding)
print("raw response: \(responseString)")
}
}
task.resume()
这篇关于我怎样才能迅速获得一个PHP变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!