Jump to content

Remote Function (AutoIt <=> PHP)


Luigi
 Share

Recommended Posts

This little example, you can build a function in AutoIt, the function have options and this options is send to PHP script.

The PHP script proccess all options, and returns to AutoIt.

Its simple and still development, but is useful.

You can use xampp/wamp.

Br, Luigi

AutoIt script:

#include-once
#include <JSMN.au3> ; https://github.com/ez2sugul/stopwatch_client/blob/master/JSMN.au3
#include <object_dump.au3>

arpc_url("http://localhost", "arpc/arpc.php")

Local $sum = arpc_sum(20, 30)
ConsoleWrite("$sum[" & $sum & "]" & @LF)
Local $greetings = arpc_greetings()
ConsoleWrite("$greetings[" & $greetings & "]" & @LF)


Func arpc_sum($iNumber1, $iNumber2)
    Local $oo = ObjCreate($SD)
    $oo.Add("method", "sum")
    $oo.Add("number1", $iNumber1)
    $oo.Add("number2", $iNumber2)
    Local $oRet = arpc($oo)
    If @error Then

    Else
        Local $json = Jsmn_Decode($oRet)
        If Not $json.Exists("method") Then Return SetError(1, 0, "#unknow $json")
        Switch $json.Item("method")
            Case "@done"
                Return $json.Item("result")
            Case "@error"
                Return "@error." & $json.Item("result")
        EndSwitch
    EndIf
EndFunc   ;==>arpc_sum

Func arpc_greetings()
    Local $oo = ObjCreate($SD)
    $oo.Add("method", "greetings")
    $oo.Add("client_name", @ComputerName)
    Local $return = arpc($oo)
    If @error Then

    Else

    EndIf
EndFunc   ;==>arpc_sum



Func arpc_url($sHost = "", $sPage = "")
    If Not IsDeclared("oARPC") Then Global $oARPC = ObjCreate($SD)
    If IsDictionary($oARPC) Then $oARPC = ObjCreate($SD)
    __arpc_set("host", $sHost)
    __arpc_set("page", $sPage)
    __arpc_set("url", $sHost & "/" & $sPage)
    __arpc_set("version", "0.0.0.1")
EndFunc   ;==>arpc_url

Func __arpc_set($key, $value)
    If $oARPC.Exists($key) Then
        $oARPC.Item($key) = $value
    Else
        $oARPC.Add($key, $value)
    EndIf
EndFunc   ;==>__arpc_set

Func arpc($oo = "")
    Local $iStatusCode, $sReceived

    Static $sCookie = Cookie()
;~      If $DEBUG Then ConsoleWrite(@TAB & "> " & $sAddress & @LF)
    Local $oHttp = ObjCreate("WinHttp.WinHttpRequest.5.1")
    If @error Then Return SetError(1, 0, 0)
    $oHttp.Open("POST", $oARPC.Item("url"), False)
    $oHttp.setRequestHeader("User-Agent", "ARPC-AutoIt")
    $oHttp.setRequestHeader("arpc_version", $oARPC.Item("version"))
    $oHttp.setRequestHeader("Connection", "Keep-Alive")
    If $sCookie Then $oHttp.setRequestHeader("Cookie", $sCookie)
    $oHttp.setRequestHeader("Referer", $oARPC.Item("url"))
    Local $json = Jsmn_Encode_Compact($oo)
    ConsoleWrite("--> " & $json & @LF)
    $oHttp.Send(StringToBinary($json, 4))
    $oHttp.WaitForResponse()
    Local $oAllHeaders = $oHttp.GetAllResponseHeaders()
    $sReceived = $oHttp.ResponseText
    $iStatusCode = $oHttp.Status
    If Not $sCookie Then $sCookie = $oHttp.GetResponseHeader("Set-Cookie")
    $oHttp = 0
    If $iStatusCode == 200 Then
        Cookie($sCookie)
        ;ConsoleWrite("$oAllHeaders--------------------" & @LF & $oAllHeaders & "$oAllHeaders--------------------" & @LF)
        ConsoleWrite("<-- " & $sReceived & @LF)
        Return $sReceived
    Else
        ConsoleWrite("<-- @error" & @LF)
        Return SetError(1, 0, 0)
    EndIf
EndFunc   ;==>arpc

Func Cookie($sInput = False)
;~  http://brugbart.com/setting-http-request-headers-autoit
    Static $sCookie
    If $sInput Then $sCookie = $sInput
    Return $sCookie
EndFunc   ;==>Cookie

PHP Server

<?php
    class arpc{
        private $sRaw;
        private $sJson;
        private $oReturn;
        private $version;
        private $aHeaders;

        function __construct(){
            $this->arpc_version = "0.0.0.1";

            $this->oReturn = new stdClass();
            $this->oFunctions = new stdClass();
            $this->oFunctions->sum = array("number1", "number2");
            $this->oFunctions->greetings = array("client_name");

            $this->aHeaders = getallheaders();
            if(!isset($this->aHeaders["arpc_version"])){
                $this->oReturn->method = "@error";
                $this->oReturn->result = "@version";
                die();
            }

            if(!isset($this->aHeaders["User-Agent"]) || ($this->aHeaders["User-Agent"] != "ARPC-AutoIt" )  ){
                $this->oReturn->method = "@error";
                $this->oReturn->result = "@user";
                die();
            }

            if(isset($HTTP_RAW_POST_DATA)){
                $sRaw = $HTTP_RAW_POST_DATA;
            }else{
                $sRaw = file_get_contents("php://input");
            }

            if(!$sRaw){
                $this->oReturn->method = "@error";
                $this->oReturn->result = "@sRaw";
                die();
            }

            if(($this->sJson = json_decode($sRaw)) != True){
                $this->oReturn->method = "@error";
                $this->oReturn->result = "@json";
                die();
            }

            if(property_exists($this->oFunctions, $this->sJson->method) && method_exists("arpc", $this->sJson->method) ){
                $method = $this->sJson->method;
                unset($this->sJson->method);
                $arr = $array = get_object_vars($this->sJson);
                $try = self::check_methods($this->oFunctions->$method, $arr);
                if($try){
                    $this->oReturn->method = "@done";
                    $this->oReturn->result = call_user_func_array("self::{$method}", $arr);
                }else{
                    $this->oReturn->method = "@error";
                    $this->oReturn->result = "@properties";
                    die();
                }
            }else{
                $this->oReturn->method = "@error";
                $this->oReturn->result = "@unknow method";
                die();
            }
        }

        function check_methods($aa, $bb){
            $arr = array();
            foreach($bb as $key => $value){
                $arr[] = $key;
            }
            $aDiff = array_diff($aa, $arr);
            if(count($aDiff)){
                return False;
            }else{
                return True;
            }
        }

        function __destruct(){
            self::output();
        }

        private function output(){
            header("Content-Type: application/json; charset=utf-8");
            header("version", $this->version);
            echo json_encode($this->oReturn);
            die();
        }

        private function sum($number1, $number2){
            return (int) $number1 + (int) $number2;
        }

        private function greetings($client_name){
            return "Welcome {$client_name}!";
        }
    }

$arpc = new arpc();

object_dump.au3

#include-once
#include <String.au3>

Func dump($oo, $key = "", $iIdent = 0)
;~  Local $sString = dump_str($oo, $key, $iIdent)
    ConsoleWrite(dump_str($oo, $key, $iIdent) & @LF)
;~  Local $iTab = 4
;~  Local $sKeys
;~  If IsDictionary($oo) Then
;~      $sKeys = $oo.Keys
;~      For $each In $sKeys
;~          If IsDictionary($oo.Item($each)) Or IsArray($oo.Item($each)) Then
;~              ConsoleWrite(($iIdent == 0 ? "" : (_StringRepeat(" ", $iIdent))) & $each & "{" & @LF)
;~              dump($oo.Item($each), ($key == "" ? "" : $key & ".") & $each, $iTab + $iIdent)
;~              ConsoleWrite(($iIdent == 0 ? "" : (_StringRepeat(" ", $iIdent))) & "}" & @LF)
;~          Else
;~              ConsoleWrite(($iIdent == 0 ? "" : (_StringRepeat(" ", $iIdent))) & ($key == "" ? "" : $key & ".") & $each & " = " & $oo.Item($each) & @LF)
;~          EndIf
;~      Next
;~  Else
;~      If IsArray($oo) Then
;~          Switch UBound($oo, 0)
;~              Case 1
;~                  For $ii = 0 To UBound($oo, 1) - 1
;~                      If IsDictionary($oo[$ii]) Or IsArray($oo[$ii]) Then
;~                          dump($oo[$ii], $key & "[" & $ii & "]", $iTab + $iIdent)
;~                      Else
;~                          ConsoleWrite(($iIdent == 0 ? "" : (_StringRepeat(" ", $iIdent))) & ($key == "" ? "" : $key & "[") & $ii & "] = " & $oo[$ii] & @LF)
;~                      EndIf
;~                  Next
;~              Case 2
;~                  For $ii = 0 To UBound($oo, 1) - 1
;~                      For $jj = 0 To UBound($oo, 2) - 1
;~                          If IsDictionary($oo[$ii][$jj]) Or IsArray($oo[$ii][$jj]) Then
;~                              dump($oo[$ii][$jj], $key & "[" & $ii & "][" & $jj & "]", $iTab + $iIdent)
;~                          Else
;~                              ConsoleWrite(($iIdent == 0 ? "" : (_StringRepeat(" ", $iIdent))) & ($key == "" ? "" : $key & "[") & $ii & "][" & $jj & "] = " & $oo[$ii][$jj] & @LF)
;~                          EndIf
;~                      Next
;~                  Next
;~          EndSwitch
;~      Else

;~      EndIf
;~  EndIf
EndFunc   ;==>dump

Func dump_str($oo, $key = "", $iIdent = 0)
    Local $iTab = 4
    Local $sKeys
    Local $sString = ""
    If IsDictionary($oo) Then
        $sKeys = $oo.Keys
        For $each In $sKeys
            If IsDictionary($oo.Item($each)) Or IsArray($oo.Item($each)) Then
                $sString &= (($iIdent == 0 ? "" : (_StringRepeat(" ", $iIdent))) & $each & "{" & @LF)
                $sString &= dump_str($oo.Item($each), ($key == "" ? "" : $key & ".") & $each, $iTab + $iIdent)
                $sString &= (($iIdent == 0 ? "" : (_StringRepeat(" ", $iIdent))) & "}" & @LF)
            Else
                $sString &= (($iIdent == 0 ? "" : (_StringRepeat(" ", $iIdent))) & ($key == "" ? "" : $key & ".") & $each & " = " & $oo.Item($each) & @LF)
            EndIf
        Next
    Else
        If IsArray($oo) Then
            Switch UBound($oo, 0)
                Case 1
                    For $ii = 0 To UBound($oo, 1) - 1
                        If IsDictionary($oo[$ii]) Or IsArray($oo[$ii]) Then
                            $sString &= dump_str($oo[$ii], $key & "[" & $ii & "]", $iTab + $iIdent)
                        Else
                            $sString &= (($iIdent == 0 ? "" : (_StringRepeat(" ", $iIdent))) & ($key == "" ? "" : $key & "[") & $ii & "] = " & $oo[$ii] & @LF)
                        EndIf
                    Next
                Case 2
                    For $ii = 0 To UBound($oo, 1) - 1
                        For $jj = 0 To UBound($oo, 2) - 1
                            If IsDictionary($oo[$ii][$jj]) Or IsArray($oo[$ii][$jj]) Then
                                $sString &= dump_str($oo[$ii][$jj], $key & "[" & $ii & "][" & $jj & "]", $iTab + $iIdent)
                            Else
                                $sString &= (($iIdent == 0 ? "" : (_StringRepeat(" ", $iIdent))) & ($key == "" ? "" : $key & "[") & $ii & "][" & $jj & "] = " & $oo[$ii][$jj] & @LF)
                            EndIf
                        Next
                    Next
            EndSwitch
        Else

        EndIf
    EndIf
    Return $sString
EndFunc   ;==>dump_str

Func IsDictionary($oo = Null)
    If Not IsObj($oo) Or Not (ObjName($oo, 2) == "Scripting.Dictionary") Then Return 0
;~  If Not (ObjName($oo, 2) == "Scripting.Dictionary") Then Return 0
    Return 1
EndFunc   ;==>IsDictionary
Edited by Luigi

Visit my repository

Link to comment
Share on other sites

  • 2 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...