# Fonctions PHP

**URL:** <https://forum.gce-electronics.com/t/fonctions-php/1111>\
**Category:** Tutoriels\
**Created:** [Juin 22, 2013, 8:53 UTC](https://forum.gce-electronics.com/t/fonctions-php/1111 "2013-06-22T20:53:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pierre](https://forum.gce-electronics.com/letter_avatar_proxy/v4/letter/p/d07c76/32.png) [@Pierre](https://forum.gce-electronics.com/u/Pierre)\
**Post date:** [Juin 22, 2013, 8:53 UTC](https://forum.gce-electronics.com/t/fonctions-php/1111/1 "2013-06-22T20:53:11Z")

</div>

Suite à mon message de présentation : [XML/JSON pour timers](http://forum.gce-electronics.com/t/xml-json-pour-timers/1110)  
Voilà les fonctions PHP que j’utilise pour contrôler l’IPX800 :

Je viens d’écrire ça rapidement, je n’ai donc pas encore tout testé (sous-entendu il y a de nombreuses erreurs).  
Les fonctions sont assez intuitives à utiliser mais j’écrirai une petite documentation dans les prochaines jours.

Ce qu’il faut savoir :  
-Les relais sont toujours numérotés de 1 à 32 (jamais de 0 à 31)  
-Dans les fonctions LED\_IPX800, PRESET\_IPX800, INPUT\_IPX800, ANALOG\_IPX800 ; $relay est un tableau (pour indiquer un ou plusieurs relais)  
-Dans la fonction M2M\_IPX800, $cmd est un tableau (afin de passer plusieurs commandes à la suite)  
-Dans chaque fonction on indique le numéro de la carte après les avoir définies ainsi :

```auto
// Carte 1
$card_ip[1] = '192.168.26.4';
$card_port[1] = '9870';

// Carte 2
$card_ip[2] = '192.168.26.5';
$card_port[2] = '9870';
```

Voilà mon fichier class\_ipix800.php :

[code]\<?php  
/\*\*

- class\_ipix800.php
- Version : 0.3 - 2013-06-22
- 
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License 2
- as published by the Free Software Foundation.
- 
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.  
\*/

function XML\_IPX800($card)  
{  
global $card\_ip;

if(empty($card\_ip[$card])) $error = ‹&nbsp;Card is unknow&nbsp;›;

$url = ‹&nbsp;http://&nbsp;›.$card\_ip[$card].‹&nbsp;/status.xml&nbsp;›;  
$xml = simplexml\_load\_file($url);

if(empty($error)) {  
if(!@file\_get\_contents($url)) return false;  
else return $xml;  
} else {  
return (DEBUGMODE === true) ? $error : false;  
}  
}

function ERASE\_TIMER\_IPX800($card, $timer)  
{  
global $card\_ip;

if(empty($card\_ip[$card])) $error = ‹&nbsp;Card is unknow&nbsp;›;  
if(!($timer \>= 0 && $timer \<= 127)) $error = ‹&nbsp;Timer id is out of range&nbsp;›;

if(empty($error)) {  
$url = ‹&nbsp;http://&nbsp;›.$card\_ip[$card].‹&nbsp;/protect/timers/timer1.htm?erase=&nbsp;›.$timer;  
if(!@file\_get\_contents($url)) return false;  
else return true;  
} else {  
return (DEBUGMODE === true) ? $error : false;  
}  
}

function PROG\_TIMER\_IPX800($card, $timer, $day, $time, $relay, $action)  
{  
global $card\_ip;

if(empty($card\_ip[$card])) $error = ‹&nbsp;Card is unknow&nbsp;›;  
if(!($timer \>= 0 && $timer \<= 127)) $error = ‹&nbsp;Timer id is out of range&nbsp;›;  
if(!($day \>= 0 && $day \<= 9)) $error = ‹&nbsp;Day is out of range&nbsp;›;  
if(!(preg\_match(‹&nbsp;/([01]?[0-9]|2[0-3]):[0-5][0-9]/&nbsp;›, $time))) $error = ‹&nbsp;Time is out of range&nbsp;›;  
if(!($relay \>= 1 && $relay \<= 32)) $error = ‹&nbsp;Relay is out of range&nbsp;›;  
if(!(($action \>= 1 && $action \<= 4) || $action == 7)) $error = ‹&nbsp;Action is out of range&nbsp;›;

if(empty($error)) {  
$relay = $relay -1;  
$url = ‹&nbsp;http://&nbsp;›.$card\_ip[$card].‹&nbsp;/protect/timers/timer1.htm?timer=&nbsp;›.$timer.‹&nbsp;&day=&nbsp;›.$day.‹&nbsp;&time=&nbsp;›.$time.‹&nbsp;&relay=&nbsp;›.$relay.‹&nbsp;&action=&nbsp;›.$action;  
if(!@file\_get\_contents($url)) return false;  
else return true;  
} else {  
return (DEBUGMODE === true) ? $error : false;  
}  
}

function LED\_IPX800($card, $relay, $mode = ‹&nbsp;output&nbsp;›)  
{  
global $card\_ip;

if(empty($card\_ip[$card])) $error = ‹&nbsp;Card is unknow&nbsp;›;

for($i=0;$i\<count($relay); $i++) {  
if($relay[$i] \>= 1 && $relay[$i] \<= 32) {  
if($mode == ‹&nbsp;input&nbsp;›) $relay[$i] = $relay[$i] + 100;  
$relay[$i] = $relay[$i] - 1;  
$url[$i] = ‹&nbsp;http://&nbsp;›.$card\_ip[$card].‹&nbsp;/leds.cgi?led=&nbsp;›.$relay[$i];  
if(!@file\_get\_contents($url[$i])) $error = ‹&nbsp;Cmd ‹&nbsp;.$i.&nbsp;› failed&nbsp;›;  
sleep(1);  
} else {  
$error = ‹&nbsp;Relay ‹&nbsp;.$relay[$i].&nbsp;› is out of range (cmd ‹&nbsp;.$i.&nbsp;›)&nbsp;›;  
}  
}

if(empty($error)) return true;  
else return (DEBUGMODE === true) ? $error : false;  
}

function PRESET\_IPX800($card, $relay, $state)  
{  
global $card\_ip;

if(empty($card\_ip[$card])) $error = ‹&nbsp;Card is unknow&nbsp;›;

for($i=0;$i\<count($relay); $i++) {  
if($relay[$i] \>= 1 && $relay[$i] \<= 32) {  
if($state[$i] == 0 || $state[$i] == 1) {  
$url[$i] = ‹&nbsp;http://&nbsp;›.$card\_ip[$card].‹&nbsp;/preset.htm?set&nbsp;›.$relay[$i].‹&nbsp;=&nbsp;›.$state[$i];  
if(!@file\_get\_contents($url[$i])) $error = ‹&nbsp;Cmd ‹&nbsp;.$i.&nbsp;› failed&nbsp;›;  
sleep(1);  
} else {  
$error[$i] = ‹&nbsp;State must be 0 or 1&nbsp;›;  
}  
} else {  
$error = ‹&nbsp;Relay ‹&nbsp;.$relay[$i].&nbsp;› is out of range (cmd ‹&nbsp;.$i.&nbsp;›)&nbsp;›;  
}  
}

if(empty($error)) return true;  
else return (DEBUGMODE === true) ? $error : false;  
}

function COUNTER\_IPX800($card, $counter, $name, $count)  
{  
global $card\_ip;

if(empty($card\_ip[$card])) $error = ‹&nbsp;Card is unknow&nbsp;›;  
if(!($counter \>= 1 && $timer \<= 3)) $error = ‹&nbsp;Counter id is out of range&nbsp;›;  
if(!($name)) $error = ‹&nbsp;Name must be set&nbsp;›;  
if(!($count \>= 0 && $count \<= 999999999)) $error = ‹&nbsp;Counter value is out of range&nbsp;›;

if(empty($error)) {  
$url = ‹&nbsp;http://&nbsp;›.$card\_ip[$card].‹&nbsp;/protect/assignio/counter.htm?countername&nbsp;›.$counter.‹&nbsp;=&nbsp;›.$name.‹&nbsp;&counter&nbsp;›.$counter.‹&nbsp;=&nbsp;›.$count;  
if(!@file\_get\_contents($url)) return false;  
else return true;  
} else {  
return (DEBUGMODE === true) ? $error : false;  
}  
}

function OUTPUT\_IPX800($card, $relay, $name, $on, $off)  
{  
global $card\_ip;

if(empty($card\_ip[$card])) $error = ‹&nbsp;Card is unknow&nbsp;›;  
if(!($relay \>= 1 && $relay \<= 32)) $error = ‹&nbsp;Relay is out of range&nbsp;›;  
if(!($name)) $error = ‹&nbsp;Name must be set&nbsp;›;  
if(!($on \>= 0 && $on \<= 65535)) $error = ‹&nbsp;On delay value is out of range&nbsp;›;  
if(!($off \>= 0 && $off \<= 65535)) $error = ‹&nbsp;On delay value is out of range&nbsp;›;

if(empty($error)) {  
$url = ‹&nbsp;http://&nbsp;›.$card\_ip[$card].‹&nbsp;/protect/settings/output1.htm?output=&nbsp;›.$relay.‹&nbsp;&relayname=&nbsp;›.$name.‹&nbsp;&delayon=&nbsp;›.$on.‹&nbsp;&delayon=&nbsp;›.$off;  
if(!@file\_get\_contents($url)) return false;  
else return true;  
} else {  
return (DEBUGMODE === true) ? $error : false;  
}  
}

function INPUT\_IPX800($card, $input, $name, $mode, $inv, $relay)  
{  
global $card\_ip;

if(empty($card\_ip[$card])) $error = ‹&nbsp;Card is unknow&nbsp;›;  
if(!($input \>= 0 && $input \<= 31)) $error = ‹&nbsp;Input is out of range&nbsp;›;  
if(!($name)) $error = ‹&nbsp;Name must be set&nbsp;›;  
if(!($mode \>= 0 && $mode \<= 4)) $error = ‹&nbsp;Mode value is out of range&nbsp;›;

if($inv) $input\_cmd = ‹&nbsp;&inv=1&nbsp;›;

for($i=0;$i\<count($relay); $i++) {  
if($relay[$i] \>= 1 && $relay[$i] \<= 32) {  
$relay[$i] = $relay[$i] - 1;  
$input\_cmd .= ‹&nbsp;l&nbsp;›.$relay[$i].‹&nbsp;=1&nbsp;›;  
} else {  
$error = ‹&nbsp;Relay ‹&nbsp;.$relay[$i].&nbsp;› is out of range (cmd ‹&nbsp;.$i.&nbsp;›)&nbsp;›;  
}  
}

if(empty($error)) {  
$url = ‹&nbsp;http://&nbsp;›.$card\_ip[$card].‹&nbsp;/protect/assignio/assign1.htm?input=&nbsp;›.$input.‹&nbsp;&inputname=&nbsp;›.$name.‹&nbsp;&mode=&nbsp;›.$mode.$input\_cmd;  
if(!@file\_get\_contents($url)) return false;  
else return true;  
} else {  
return (DEBUGMODE === true) ? $error : false;  
}  
}

function ANALOG\_IPX800($card, $input, $name, $mode, $max, $maxm, $min, $minm, $relay)  
{  
global $card\_ip;

if(empty($card\_ip[$card])) $error = ‹&nbsp;Card is unknow&nbsp;›;  
if(!($input \>= 0 && $input \<= 3)) $error = ‹&nbsp;Input is out of range&nbsp;›;  
if(!($name)) $error = ‹&nbsp;Name must be set&nbsp;›;  
if(!($mode \>= 0 && $mode \<= 5)) $error = ‹&nbsp;Mode value is out of range&nbsp;›;  
if(!($max \>= 0 && $max \<= 1023)) $error = ‹&nbsp;Max value is out of range&nbsp;›;  
if(!($maxm \>= 0 && $maxm \<= 2)) $error = ‹&nbsp;Max mode value is out of range&nbsp;›;  
if(!($min \>= 0 && $min \<= 1023)) $error = ‹&nbsp;Min value is out of range&nbsp;›;  
if(!($minm \>= 0 && $minm \<= 2)) $error = ‹&nbsp;Min mode value is out of range&nbsp;›;

for($i=0;$i\<count($relay); $i++) {  
if($relay[$i] \>= 1 && $relay[$i] \<= 🕶 {  
$input\_cmd .= ‹&nbsp;lka&nbsp;›.$relay[$i].‹&nbsp;=1&nbsp;›;  
} else {  
$error = ‹&nbsp;Relay ‹&nbsp;.$relay[$i].&nbsp;› is out of range (cmd ‹&nbsp;.$i.&nbsp;›)&nbsp;›;  
}  
}

if(empty($error)) {  
$url = ‹&nbsp;http://&nbsp;›.$card\_ip[$card].‹&nbsp;/protect/assignio/analog1.htm?analog=&nbsp;›.$input.‹&nbsp;&name=&nbsp;›.$name.‹&nbsp;&selectinput=&nbsp;›.$mode.‹&nbsp;&hi=&nbsp;›.$max.‹&nbsp;&mhi=&nbsp;›.$maxm.‹&nbsp;&lo=&nbsp;›.$min.‹&nbsp;&mlo=&nbsp;›.$minm.$input\_cmd;  
if(!@file\_get\_contents($url)) return false;  
else return true;  
} else {  
return (DEBUGMODE === true) ? $error : false;  
}  
}

function PING\_IPX800($card, $ip, $time, $retry, $relay)  
{  
global $card\_ip;

if(empty($card\_ip[$card])) $error = ‹&nbsp;Card is unknow&nbsp;›;  
if(!(filter\_var($ip, FILTER\_VALIDATE\_IP, FILTER\_FLAG\_IPV4))) $error = ‹&nbsp;IP adress is out of range&nbsp;›;  
if(!($time \>= 0 && $time \<= 65535)) $error = ‹&nbsp;Time value is out of range&nbsp;›;  
if(!($retry \>= 0 && $retry \<= 255)) $error = ‹&nbsp;Retry value is out of range&nbsp;›;  
if(!($relay \>= 1 && $relay \<= 32)) $error = ‹&nbsp;Relay is out of range&nbsp;›;

if(empty($error)) {  
$relay = $relay - 1;  
$url = ‹&nbsp;http://&nbsp;›.$card\_ip[$card].‹&nbsp;/protect/settings/ping.htm?pingip=&nbsp;›.$ip.‹&nbsp;&pingtime=&nbsp;›.$time.‹&nbsp;&pingretry=&nbsp;›.$retry.‹&nbsp;&prelay=&nbsp;›.$relay;  
if(!@file\_get\_contents($url)) return false;  
else return true;  
} else {  
return (DEBUGMODE === true) ? $error : false;  
}  
}

function M2M\_IPX800($card, $cmd)  
{  
global $card\_ip, $card\_port;

if(empty($card\_ip[$card])) $error = ‹&nbsp;Card is unknow&nbsp;›;

$socket = @socket\_create(AF\_INET, SOCK\_STREAM, SOL\_TCP);

if ($socket === false) {  
$errorcode = socket\_last\_error();  
$errormsg = socket\_strerror($errorcode);

```
$error[] = "Unable to create socket: [$errorcode] $errormsg";

```

}

$connect = @socket\_connect($socket, $card\_ip[$card], $card\_port[$card]);

if ($connect === false) {  
$errorcode = socket\_last\_error($socket);  
$errormsg = socket\_strerror($errorcode);

```
$error[] = "Unable to connect: [$errorcode] $errormsg";

```

}

for($i=0;$i\<count($cmd); $i++) {  
socket\_write($socket, $cmd[$i], strlen($cmd[$i]));  
$reponse = socket\_read($socket, 64);  
}  
socket\_close($socket);

if(empty($error)) {  
return $reponse;  
} else {  
return (DEBUGMODE === true) ? $error : false;  
}  
}  
?\>[/code]

---

<div class="post-metadata">

**Author:** ![thebest28](https://forum.gce-electronics.com/letter_avatar_proxy/v4/letter/t/59ef9b/32.png) [@thebest28](https://forum.gce-electronics.com/u/thebest28)\
**Post date:** [Juin 23, 2013, 5:00 UTC](https://forum.gce-electronics.com/t/fonctions-php/1111/2 "2013-06-23T17:00:30Z")

</div>

Super sympa pour ton partage , cela pourra me servir un jour où j’aurais plus de temps.  
Merci 😉
