如何将CURL命令转换为Swift [英] How to convert CURL command to Swift

查看:87
本文介绍了如何将CURL命令转换为Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习编写我的第一个IOS应用程序,该应用程序将从Proliphix IP网络恒温器查询一些基本的OID。 Proliphix支持多种方法,例如Curl; PHP和API GET&组。在这些方法中,Swift最简单的方法是什么?

I am learning to write my first IOS app that will query some basic OIDs from a Proliphix IP Network Thermostat. Proliphix supports several methods such as Curl; PHP and API GET & SET. Out of these methods, what would be the easiest in Swift?

有人可以告诉我如何为Swift转换以下方法之一吗?

Can someone tell me how to convert one of the the following methods for Swift?

以下是示例这些来自Proliphix API,可以在Google搜索中找到。

Here are examples of these from the Proliphix API that can be found on a google search.

卷曲

获取
curl –u主机名:密码–-data OID1.1 = http://192.168.1.100:8100/get

设置
curl –u主机名:密码--data OID1 .10.5 = 120-数据提交=提交
http://192.168.1.100:8100/pdp

API GET

使用的URL是/ get。 API GET请求是未指定OID值的OID列表。格式正确的请求应提供Content-Length标头。 。该条目是编码的基本身份验证字(请参阅RFC 2617 -HTTP身份验证:基本和摘要访问身份验证)。

The URL used is /get. An API GET request is a list of OIDs where their value is not specified. A properly formatted request should provide the Content-Length header. . The entry is the encoded basic authentication word (See RFC 2617 -HTTP Authentication: Basic and Digest Access Authentication).

Request

POST /get HTTP/1.1
  Authorization: Basic <credentials>
  Content-Type: application/x-www-form-urlencoded
  User-Agent: Jakarta Commons-HttpClient/2.0.2
  Host: 192.168.111.114:8214
  Content-Length: 92
  OID1.10.9=&OID1.2=&OID1.1=&OID1.4=&OID1.8=&OID2.7.1=&

响应

HTTP/1.1 200 OK
  Cache-control: no-cache
  Server: Ubicom/1.1
  Content-Length: 166

OID1.10.9=example@proliphix.com& OID1.2 = SW Dev 114& OID1.1 = therm_rev_2 0.1.40& OID1.4 = 192.168。 111.114& OID1.8 = 00:11:49:00:00:58& OID2.7.1 = NT100

OID1.10.9=example@proliphix.com&OID1.2=SW Dev 114&OID1.1=therm_rev_2 0.1.40&OID1.4=192.168.111.114&OID1.8=00:11:49:00:00:58&OID2.7.1=NT100

API设置

使用的URL是/ pdp。 API SET与请求消息的API GET相似,不同之处在于在等号处提供了所需的值。响应的格式不同。该条目是编码的基本身份验证字(请参阅RFC 2617 -HTTP身份验证:基本和摘要访问身份验证)。请求中的最后一项必须为 submit = Submit。

The URL used is /pdp . An API SET is similar to the API GET for the request message, except that the desired value is provided at the equals sign. The response is formatted differently. The entry is the encoded basic authentication word (See RFC 2617 -HTTP Authentication: Basic and Digest Access Authentication). The last item in the request must be "submit=Submit". Do not include an ‘&’ after the "submit=Submit".

请求

 POST /pdp HTTP/1.1
  Authorization: Basic <credentials>
  Content-Type: application/x-www-form-urlencoded
  User-Agent: Jakarta Commons-HttpClient/2.0.2
  Host: 192.168.111.114:8214
  Content-Length: 193

响应

HTTP/1.1 200 OK
  Cache-control: no-cache
  Server: Ubicom/1.1
  Content-Length: 308






PHP

PHP是Web服务器特定的脚本语言,类似于mod_perl。它很好地集成到Apache中,并提供了许多特定于Web的库作为基本系统的一部分。

PHP is a web-server specific scripting language, akin to mod_perl. It integrates well into Apache and offer many web-specific libraries as part of the base system.

获取

$oids = array('OID1.4'=>'', // commonIpAddr
              'OID1.10.5'=>'',
              ‘submit’=>’Submit’); // commonCallhomeInterval
$url = "http://192.168.1.100:8100/get";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPGET, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$myHeader = array("Content-Type: application/x-www-form-urlencoded" ); curl_setopt($ch, CURLOPT_HTTPHEADER, $myHeader);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($oids)); $response = curl_exec($ch);
curl_close($ch);
$oids = array();
parse_str($response, $oids); // converts '.' to underscore
$localip = $oids['OID1_4'];
$interval = $oids['OID1_10_5']; // in minutes


推荐答案

我会说你应该使用Proliphix提供的API。

I would say that you should use the API that Proliphix is providing.

如您所见,它们提供了一个示例,并且您已经设法弄清楚如何通过cURL提供正确的参数,因此现在您只是需要将其转换为Swift。

As you can see, they provide an example, and you've already managed to figure out how to provide the correct parameters through cURL so now you "just" need to convert this to Swift.

为此,您需要HTTP网络API,可以使用 NSURLSession 苹果公司提供的API,或者

For this you need a HTTP networking API, you could use either the NSURLSession API provided by Apple, or perhaps Alamofire, just to mention a pair.

这些API的网址为 / get / pdp 。然后,您需要告诉他们这是GET还是POST请求。如果API需要任何数据(例如您的情况下的OID参数),则还需要提供该数据,然后设置最终标头。

These API's take an URL which would be /get or /pdp in your case. Then you need to tell them wether this is a GET or a POST request. If the API needs any data (like the OID parameters in your case), you'll need to provide that as well and then you need to set up eventual headers.

然后您发送请求并等待答案,然后您对其做出反应。

Then you send your request and wait for an answer, which you then react to.

这里是有关如何使用NSURLSession执行此操作的示例:

Here is an example on how to do this with NSURLSession:

if let url = NSURL(string: "http://httpbin.org/post"){
    let request = NSMutableURLRequest(URL: url)
    request.HTTPMethod = "POST" //Or GET if that's what you need
    request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")  //This is where you add your HTTP headers like Content-Type, Accept and so on
    let params = ["OID1.2" : "SW+Dev+114", "OID1.4" : "192.168.111.114"] as Dictionary<String, String> //this is where you add your parameters

    let httpData = NSKeyedArchiver.archivedDataWithRootObject(params) //you need to convert you parameters to NSData or to JSON data if the service accepts this, you might want to search for a solution on how to do this...hopefully this will get you in the right direction :-)
    request.HTTPBody = httpData
    let session = NSURLSession.sharedSession()
    session.dataTaskWithRequest(request, completionHandler: { (returnData, response, error) -> Void in
        var strData = NSString(data: returnData, encoding: NSUTF8StringEncoding)
        println("\(strData)")
    }).resume() //Remember this one or nothing will happen :-)
}

希望这能使您朝正确的方向前进。现在,您知道要搜索的内容了,您也可以在Google搜索NSURLSession或Alamofire教程。

Hope this gets you in the right direction. You could also do a Google search for NSURLSession or Alamofire tutorial, now that you know what to search for.

这篇关于如何将CURL命令转换为Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆