PHP 教學 - 利用 curl 對設備進行開關機
工作上遇到一些控制設備的需求,需要將投影機的 web 開關機功能整合到自行開發的 web 控制系統中,初步只能看到前端的網頁
html 部分
<tr>
<td class="ItemName">Power On:</td>
<td colspan=2 width=250px><input type="button" id="#0,0,0,0,0,0,0,system,projector,write,on" value="Do it" class="selection-input" onclick="onButton('#0,0,0,0,0,0,0,system,projector,write,on','custom_function_call_function')">
</input></td>
</tr>
<tr>
<td class="ItemName">Power Off:</td>
<td colspan=2 width=250px><input type="button" id="#0,0,0,0,0,0,0,system,projector,write,standby" value="Do it" class="selection-input" onclick="onButton('#0,0,0,0,0,0,0,system,projector,write,standby','custom_function_call_function')">
</input></td>
</tr>
javascript 部分
function makeRpcCall(type, url, params) {
var myRpc;
var returnValue = 0;
if (window.XMLHttpRequest) {
myRpc = new XMLHttpRequest()
}
else {
myRpc = new ActiveXObject("Microsoft.XMLHTTP");
}
if (type == "POST") {
myRpc.open("POST", url, false);
myRpc.send(params.toString());
returnValue = myRpc.responseText;
if (returnValue.substring(0,5) == "error") {
var str = "Error: " + returnValue + " POST call failed";
alert(str);
return 0;
}
}
else if (type == "GET") {
// Add date/time so that request is not cached
params.setParam("dateTime", getDateTime());
myRpc.open("GET", url+"?"+params.toString(), false);
myRpc.send();
returnValue = myRpc.responseText;
if (returnValue.substring(0,5) == "error") {
var str = "Error: " + returnValue + " GET call failed";
alert(str);
return 0;
}
}
return returnValue;
}
function onButton(name, actfunc) {
var returnValue;
returnValue=makeRpcCall('POST', '/rpc_call', 'call=function&name='+name+'&actfunc='+actfunc);
alert(name);
if(parseInt(returnValue) == 88) {
document.getElementById("WaitingWindow").style.display = "block";
setTimeout("location.reload(true);", 15000);
}
else {
load();
}
}
初步得到的資訊大概是這樣,有辦法做到控制別人的網頁嗎?查看一下 code 我們發現到這是利用 ajax 來做 get 和 post 的傳值,於是我想到利用 php curl 的方式,和朋友討論一下,認為是可行的,剩下的只是要知道是用 get 還是 post ,從 onclick 的行為中可以發現到 javascript 寫了一個函式稱為 onButton 會再去執行 makeRpcCall 函式,簡單說就是利用 ajax 中 post 來傳值,那麼只要把網址給找出來就好了,網址應該長成這樣 /rpc_call?call=function&name={變數name}&actfunc={變數actfunc} ,得到公式後我們就可以直接來 alert 變數,果然,找到解答案就是 rpc_call?call=function&name=#0,0,0,0,0,0,0,system,projector,write,on&actfunc=custom_function_call_function,但從網址上直接輸入查看投影機發現到沒有動靜,只好放大絕招 php curl ,老實說我只有寫過 curl 爬表單,不過利用 php curl post 傳遞參數應該也可以,最後成功了,提供下列範例程式
index.php
<html>
<head>
<script>
function on(url) {
location.href = "on.php"
}
function off(url) {
location.href = "off.php"
}
</script>
</head>
<body>
<input type="button" value="開機" onclick="on('on.php');" />
<input type="button" value="關機" onclick="off('off.php');" />
</body>
</html>
on.php
<?php
$post_data =
array(
'call=function',
'name=#0,0,0,0,0,0,0,system,projector,write,on',
'actfunc=custom_function_call_function',
);
$post_data = implode('&',$post_data);
$url='192.168.0.100/rpc_call';
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
ob_start();
curl_exec($ch);
$result = ob_get_contents() ;
ob_end_clean();
echo $post_data."<br>";
echo $result;
header("Refresh: 2; url=http://localhost/dsirm2/index.php");
off.php
<?php
$post_data =
array(
'call=function',
'name=#0,0,0,0,0,0,0,system,projector,write,standby',
'actfunc=custom_function_call_function',
);
$post_data = implode('&',$post_data);
$url='192.168.0.100/rpc_call';
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
ob_start();
curl_exec($ch);
$result = ob_get_contents() ;
ob_end_clean();
echo $result;
header("Refresh: 2; url=http://localhost/dsirm2/index.php");
假如做到這裡那麼你就學會如何利用 php 來控制別人的設備了
html 部分
<tr>
<td class="ItemName">Power On:</td>
<td colspan=2 width=250px><input type="button" id="#0,0,0,0,0,0,0,system,projector,write,on" value="Do it" class="selection-input" onclick="onButton('#0,0,0,0,0,0,0,system,projector,write,on','custom_function_call_function')">
</input></td>
</tr>
<tr>
<td class="ItemName">Power Off:</td>
<td colspan=2 width=250px><input type="button" id="#0,0,0,0,0,0,0,system,projector,write,standby" value="Do it" class="selection-input" onclick="onButton('#0,0,0,0,0,0,0,system,projector,write,standby','custom_function_call_function')">
</input></td>
</tr>
javascript 部分
function makeRpcCall(type, url, params) {
var myRpc;
var returnValue = 0;
if (window.XMLHttpRequest) {
myRpc = new XMLHttpRequest()
}
else {
myRpc = new ActiveXObject("Microsoft.XMLHTTP");
}
if (type == "POST") {
myRpc.open("POST", url, false);
myRpc.send(params.toString());
returnValue = myRpc.responseText;
if (returnValue.substring(0,5) == "error") {
var str = "Error: " + returnValue + " POST call failed";
alert(str);
return 0;
}
}
else if (type == "GET") {
// Add date/time so that request is not cached
params.setParam("dateTime", getDateTime());
myRpc.open("GET", url+"?"+params.toString(), false);
myRpc.send();
returnValue = myRpc.responseText;
if (returnValue.substring(0,5) == "error") {
var str = "Error: " + returnValue + " GET call failed";
alert(str);
return 0;
}
}
return returnValue;
}
function onButton(name, actfunc) {
var returnValue;
returnValue=makeRpcCall('POST', '/rpc_call', 'call=function&name='+name+'&actfunc='+actfunc);
alert(name);
if(parseInt(returnValue) == 88) {
document.getElementById("WaitingWindow").style.display = "block";
setTimeout("location.reload(true);", 15000);
}
else {
load();
}
}
初步得到的資訊大概是這樣,有辦法做到控制別人的網頁嗎?查看一下 code 我們發現到這是利用 ajax 來做 get 和 post 的傳值,於是我想到利用 php curl 的方式,和朋友討論一下,認為是可行的,剩下的只是要知道是用 get 還是 post ,從 onclick 的行為中可以發現到 javascript 寫了一個函式稱為 onButton 會再去執行 makeRpcCall 函式,簡單說就是利用 ajax 中 post 來傳值,那麼只要把網址給找出來就好了,網址應該長成這樣 /rpc_call?call=function&name={變數name}&actfunc={變數actfunc} ,得到公式後我們就可以直接來 alert 變數,果然,找到解答案就是 rpc_call?call=function&name=#0,0,0,0,0,0,0,system,projector,write,on&actfunc=custom_function_call_function,但從網址上直接輸入查看投影機發現到沒有動靜,只好放大絕招 php curl ,老實說我只有寫過 curl 爬表單,不過利用 php curl post 傳遞參數應該也可以,最後成功了,提供下列範例程式
index.php
<html>
<head>
<script>
function on(url) {
location.href = "on.php"
}
function off(url) {
location.href = "off.php"
}
</script>
</head>
<body>
<input type="button" value="開機" onclick="on('on.php');" />
<input type="button" value="關機" onclick="off('off.php');" />
</body>
</html>
on.php
<?php
$post_data =
array(
'call=function',
'name=#0,0,0,0,0,0,0,system,projector,write,on',
'actfunc=custom_function_call_function',
);
$post_data = implode('&',$post_data);
$url='192.168.0.100/rpc_call';
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
ob_start();
curl_exec($ch);
$result = ob_get_contents() ;
ob_end_clean();
echo $post_data."<br>";
echo $result;
header("Refresh: 2; url=http://localhost/dsirm2/index.php");
off.php
<?php
$post_data =
array(
'call=function',
'name=#0,0,0,0,0,0,0,system,projector,write,standby',
'actfunc=custom_function_call_function',
);
$post_data = implode('&',$post_data);
$url='192.168.0.100/rpc_call';
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
ob_start();
curl_exec($ch);
$result = ob_get_contents() ;
ob_end_clean();
echo $result;
header("Refresh: 2; url=http://localhost/dsirm2/index.php");
假如做到這裡那麼你就學會如何利用 php 來控制別人的設備了
留言
張貼留言