dongzhi6463 2015-01-03 18:06
浏览 65

使用Swift发布POST请求

I'm trying to send data over from my iOS app to a PHP script. I have Title, Description and City. I want it to always save every info I send to it from my iOS app and always make a new one every time I send it data. The problem I'm having is, when I go to the PHP file on my browser, nothing shows up. The title, description and city field is always blank. But in my Xcode console I see it returning everything correctly. Here's my swift code.

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()
    }

And here's my PHP file.

<?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;  
 ?> 

And here's my console output on 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 -->
)

I hope I gave you guys enough info to help me out because I can't seem to figure it out since I'm not the best with PHP.

  • 写回答

2条回答 默认 最新

  • douzhang1852 2015-01-03 18:14
    关注

    If you view the script through the browser than thats differrent because it will be a separate browser request without post values.

    If you want to persist data, you will need to find a way to store that data using a database or perhaps some files.

    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发