duancunsu9209 2018-01-03 07:51
浏览 91
已采纳

如何使用swift发布到php脚本后在IOS中获取php echo响应

I am wondering in the ios app, how to get php echo response after posting to the php script using swift

Now, when i print the response out in the ios app, it shows lots of info, such as status code,header,content type, date. But i want neither of them

All i want is the ios app can return the php echo string <?php echo 'You have successfully registered';?>

SWIFT CODE

    let myUrl = URL(string: "http://www.swiftdeveloperblog.com/http-post-example-script/");

    var request = URLRequest(url:myUrl!)

    request.httpMethod = "POST"// Compose a query string

    let postString = "firstName=James&lastName=Bond";

    request.httpBody = postString.data(using: String.Encoding.utf8);

    let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in

        if error != nil
        {
            print("error=\(error)")
            return
        }

        // You can print out response object
        print("response = \(response)")

        //Let's convert response sent from a server side script to a NSDictionary object:
        do {
            let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary

            if let parseJSON = json {

                // Now we can access value of First Name by its key
                let firstNameValue = parseJSON["firstName"] as? String
                print("firstNameValue: \(firstNameValue)")
            }
        } catch {
            print(error)
        }
    }
    task.resume()

PHP CODE

<?php echo 'You have successfully registered';?>
  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 内存管理的一段代码不是很理解
      • ¥20 打开anaconda时卡在Loading applications无法进入界面
      • ¥15 网页超时时间设置失效
      • ¥15 有关绿色信贷毕业论文的问题
      • ¥30 关于#机器人#的问题,如何解决?
      • ¥15 求MATLAB函数ScalarLayerDisplay的代码
      • ¥15 安卓如何自动执行检测到的NFC标签,无需再点确认
      • ¥15 pyHM库mouse模块的ValueError错误
      • ¥15 python opencv 摄像头 传感器
      • ¥30 eMMC&Android&C&Linux