OHSCE introductory tutorial (4) - big belly function PHP easily engages in HTTP(S) API (URL/REST)

  In various projects, we often deal with industrial bus/Ethernet equipment and some RF/IR equipment, and everyone must be familiar with this aspect. With the development of technology, we can now see some hybrid communication devices. They are control devices or terminal devices equipped with REST-style control interfaces common in the Internet field. Such devices first appeared in the home field, mostly in the form of WIFI. Access and cloud communication, and now such devices are beginning to appear in the field of conventional control. Of course, the current demand often brings us closer to the cloud, and inevitably we have to access many open platforms and cloud services. In these aspects, the HTTP interface is very popular. In the current network environment, HTTP is actually an upper-layer protocol based on the TCP/IP protocol (occasionally made by UDP). The actual OHSCE provides convenient calls to the HTTP interface.

   Get to know your powerful tool partner function: Ohsce_url_c   ( Ohsce_eng_url_c )

(OHSCE >=V0.1.22 -執行一箇URL請求。支持身份驗證,支持帶COOKIE、支持GET/POST、支持多協議。
本函數設計爲多用於控製HTTP方式控製的控製設備(如HTTP/TCP網絡繼電器),亦可以做爪取網頁內容時使用。
依賴:PHP/EXT/CURL 請開啟CURL擴展,該擴展爲PHP自帶基本覆蓋所有目前主流使用的PHP環境,大部分部署的環境已默認開啟。

/*************本函數是Ohsce_url_c函數的彆名**********/

Ohsce_eng_url_c($surl,&$odata,$username=null,$password=null,$cookie=false,$short=true,$headers=null)
-$surl 資源目標
string:URL地址 如http://192.168.1.40/do/on1
array:複閤資源地址
如array(0=>'http://192.168.1.40/do/on1','postdata'=>$postdata,'proxy'=>'socks5','proxyaddress'=>'10.10.58.42','proxypassword'=>'123456','headers’=>$headerdata)
-$odata 取迴數據所存入的變量
-$username 登陸用戶名
-$password 登陸密碼
-$cookie 保存cookie的文件地址
-$short 短操作
-$headers HEADER數據 ARRAY 0=>true 1=>data
EXAMPLE:
<?php
ini_set('memory_limit',"88M");//重置php可以使用的內存大小爲64M
set_time_limit(0);
ob_implicit_flush(1);
i n c l u d e('loadohsce.php');
Ohsce_url_c('http://www.baidu.com',$data);
echo $data;
sleep(30);
NOTICE:
硬件延時由邏輯控製

     We use this built-in function of OHSCE to smash various HTTP(S) APIs through almost one line of code. 

     1.【GET】

    The GET method is the most popular HTTP interface invocation method. Most of the interfaces of many open platforms are called in the GET method, and of course some devices are also used. Let's take the weather API of Portable Cloud as an example to query a piece of weather information.

<?php
echo ohsce_url_c("http://wthrcdn.etouch.cn/weather_mini?citykey=101010100");

or

<?php
Ohsce_url_c("http://wthrcdn.etouch.cn/weather_mini?citykey=101010100",$ohsceurldata);
echo $ohsceurldata;

Of course, we recommend you to use the second method, because the data you retrieve is likely to have problems with encoding, and you need to perform encoding conversion and JSON/XML decoding. It is more reasonable to temporarily store the data in a variable.

    more internet

   Many current APIs need to use HEADER to transmit TOKEN, such as Baidu APISTORE. The last parameter of the ohsce_url_c function is where you use to set the HEADER. Of course, HEADER can also be added to the URL part as array data to make the code shorter (such as POST). 

    2. [POST]

    POST is commonly used in form submission, but it is also common in the field of network controllers that some manufacturers use it as a way to submit control instructions. Although it may not be the best solution (at least I won't do it), its existence means Reasonable, we have to adapt to such a product.

<?php
Ohsce_url_c(array(0=>'http://192.168.0.166:80/relay.cgi','postdata'=>"saida2pluse=pluse"),$ohsceurldata,'admin','12345678');

    Just change the URL of OHSCE into an array and add the POST content.

    Third, a small test.

    This is an example of a relay device from a treasure. (There are files in the group space)

<?php
//OHSCE V0.1.25 更新组件请去OHSCE.ORG
include('loadohsce.php');
error_reporting(1);
if(isset($_GET['kg'])){
switch($_GET['kg']){
case "on1":
$sd_kjg_data='saida1on=on';
goto urlc;
case "off1":
$sd_kjg_data='saida1off=off';
goto urlc;
case "pluse1":
$sd_kjg_data='saida1pluse=pluse';
goto urlc;
case "on2":
$sd_kjg_data='saida2on=on';
goto urlc;
case "off2":
$sd_kjg_data='saida2off=off';
goto urlc;
case "pluse2":
$sd_kjg_data='saida2pluse=pluse';
goto urlc;

default:
	goto cend;
}
}
goto thtml;
urlc:
Ohsce_url_c(array(0=>'http://192.168.1.166:80/relay.cgi','postdata'=>$sd_kjg_data),$ohsceurldata,'admin','12345678');
thtml:
?>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>演示</title>
<link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">
<link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap-theme.min.css">
<script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
<script src="http://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
</head>
<body>
<br />
<a href="?kg=on1" class="btn btn-success">打开回路1</a>&nbsp;
<a href="?kg=off1" class="btn btn-danger">关闭回路1</a>&nbsp;
<a href="?kg=pluse1" class="btn btn-info">点触1</a>&nbsp;
<br /><br />
<a href="?kg=on2" class="btn btn-success">打开回路2</a>&nbsp;
<a href="?kg=off2" class="btn btn-danger">关闭回路2</a>&nbsp;
<a href="?kg=pluse2" class="btn btn-info">点触2</a>&nbsp;


<nav class="navbar navbar-default navbar-fixed-bottom" role="navigation">
  <div class="container">
   <center>Powered by <a href="http://www.ohsce.org">OHSCE 0.1.25</a></center>
  </div>
</nav>
</body>
</html>
<?php
cend:
exit;

   Submit to the relay.cgi file on port 80 of the device with the IP address of 192.168.1.166 via HTTP to open the specified loop, close the specified loop and control the specified loop, and authenticate admin: 12345678.

   4. HTTPS

   Called in the same way as HTTP, making the HTTPS URL available.

<?php
Ohsce_url_c("https://www.baidu.com",$ohsceurldata);
echo $ohsceurldata;

   5. More

   This function also supports more protocols at the same time, which provides convenience for you to operate various devices. For example, the surveillance camera of the Hanwha brand of Samsung factory provides FTP operation, you can use this function to operate it.

   6. Polish your process Refine your control process and keep updating your OHSCE framework to the latest stable version.

   OHSCE hopes that every control engineer can take the wings of Internet +, every PHPWEB engineer can rush to the foreword of the Internet of Things, our program is your reliable cornerstone.

     Official website: http://www.ohsce.org & http://www.ohsce.com

     Developer QQ group: 374756165

     GITHUB:https://github.com/OpenIBC/Ohsce

     GIT@OSC:https://git.oschina.net/SFXH/Ohsce

     Manual address: http://www.ohsce.com/index.php/book/

     Donate & Support OHSCE: http://www.ohsce.com/index.php/company/

     Cooperation & sponsorship: 393562235 ([email protected]

{{o.name}}
{{m.name}}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324126852&siteId=291194637