Php 使用Swift发布请求
我正在尝试将数据从iOS应用程序发送到PHP脚本。我有头衔,描述和城市。我希望它总是保存我从iOS应用程序发送给它的每一条信息,并且每次发送数据时总是创建一条新信息。我遇到的问题是,当我在浏览器上打开PHP文件时,什么都没有显示。标题、说明和城市字段始终为空。但在我的Xcode控制台中,我看到它正确地返回了所有内容。这是我的swift密码Php 使用Swift发布请求,php,ios,json,swift,Php,Ios,Json,Swift,我正在尝试将数据从iOS应用程序发送到PHP脚本。我有头衔,描述和城市。我希望它总是保存我从iOS应用程序发送给它的每一条信息,并且每次发送数据时总是创建一条新信息。我遇到的问题是,当我在浏览器上打开PHP文件时,什么都没有显示。标题、说明和城市字段始终为空。但在我的Xcode控制台中,我看到它正确地返回了所有内容。这是我的swift密码 func postToServerFunction() { var url: NSURL = NSURL(string: "h
func postToServerFunction() {
var url: NSURL = NSURL(string: "http://vittletest.netii.net/index.php")!
var request:NSMutableURLRequest = NSMutableURLRequest(URL:url)
request.HTTPMethod = "POST"
let postString = "title=testing&description=hello&city=RanchoCordova"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
println("error=\(error)")
return
}
println("response = \(response)")
let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
println("responseString = \(responseString)")
}
task.resume()
}
这是我的PHP文件
<?php
$something = "Some Info";
echo $something;
$title = $_POST['title'];
$description = $_POST['description'];
$city = $_POST['city'];
echo "<br />";
echo "Title: ". $title;
echo "<br />";
echo "Description: ". $description;
echo "<br />";
echo "City: ". $city;
?>
这是我在Xcode上的控制台输出
2015-01-03 10:05:50.861 VittleTest[616:28516] Warning: A long-running operation is being executed on the main thread.
Break on warnBlockingOperationOnMainThread() to debug.
response = <NSHTTPURLResponse: 0x7f9f5ae5e3f0> { URL: http://vittletest.netii.net/index.php } { status code: 200, headers {
Connection = close;
"Content-Length" = 309;
"Content-Type" = "text/html";
Date = "Sat, 03 Jan 2015 18:05:52 GMT";
Server = Apache;
"X-Powered-By" = "PHP/5.2.17";
} }
responseString = Optional(<html>
<head>
<title>PHP Test</title>
</head>
<body>
Some Info<br />Title: testing<br />Description: hello<br />City: RanchoCordova
</body>
</html>
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->
)
我希望我给了你们足够的信息来帮助我,因为我似乎无法弄清楚,因为我不是PHP的高手。如果你通过浏览器查看脚本,那就不同了,因为它将是一个没有post值的单独浏览器请求
如果要持久化数据,您需要找到一种使用数据库或某些文件存储数据的方法。原因是。。。从斯威夫特你是通过邮政参数
let postString = "title=testing&description=hello&city=RanchoCordova"
但是,当您直接在浏览器中点击url时,您并没有发布这些参数。这就是为什么它们是空白的我如何发布和保存参数?您想保存在哪里不同于论坛网站,我们不使用感谢、感谢的帮助或签名。看见顺便说一句,这是提前感谢,不是提前感谢。