【发布时间】:2015-08-23 18:41:33
【问题描述】:
我正在学习编写我的第一个 IOS 应用程序,它将从 Proliphix IP 网络恒温器中查询一些基本的 OID。 Proliphix 支持 Curl 等多种方法; PHP 和 API GET & SET。在这些方法中,Swift 中最简单的方法是什么?
谁能告诉我如何为 Swift 转换以下方法之一?
以下是来自 Proliphix API 的示例,可在 google 搜索中找到。
卷曲
获取 curl –u 主机名:密码 –-data OID1.1= http://192.168.1.100:8100/get
设置 curl –u 主机名:密码 --data OID1.10.5=120 --data submit=提交 http://192.168.1.100:8100/pdp
API 获取
使用的 URL 是 /get。 API GET 请求是未指定其值的 OID 列表。格式正确的请求应提供 Content-Length 标头。 .该条目是编码的基本身份验证字(请参阅 RFC 2617 -HTTP 身份验证:基本和摘要式访问身份验证)。
请求
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 开发 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”。请勿在“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 是一种特定于网络服务器的脚本语言,类似于 mod_perl。它可以很好地集成到 Apache 中,并提供许多特定于 Web 的库作为基本系统的一部分。
获取
$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
【问题讨论】: