3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
#!/usr/bin/env php <?php $ts = date_timestamp_get(date_create()); $pdu6 = new PduStats(6, $ts); //$pdu6->toArray(); //$pdu6->putval(); $pdu7 = new PduStats(7, $ts); //$pdu7->toArray(); //$pdu7->putval(); class PduStats { /** * Statistics * @var array */ private $stats = array(); private $host; private $cookie; private $baseurl; private static $st; /** * Constructor. * * @param integer $id PDU identifier */ public function __construct($id, $TS, $user = 'apc', $pass = 'apc') { $this->ts = $TS; $this->cookie = tempnam(sys_get_temp_dir(), 'ucspdu-'); $this->host = "10.10.254.${id}"; $loginUrl = "http://".$this->host."/Forms/login1"; $datosLogin = 'login_username='.$user.'&login_password='.$pass; // Hacemos logging para obtener la cookie $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $loginUrl); curl_setopt($ch, CURLOPT_POST, 1); // a 0 curl_setopt($ch, CURLOPT_POSTFIELDS, $datosLogin); curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_exec($ch); $url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); if (!preg_match('@(http://[^/]+/[^/]+/[^/]+/)@Ui', $url, $match)) { throw new RuntimeException('No redirection'); } $this->baseurl=$match[0]; curl_close($ch); $this->toArray(); $this->putval(); $this->close(); } public function close() { // Logout $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->baseurl . 'logout.htm'); curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_exec($ch); curl_close($ch); // Delete cookie unlink($this->cookie); } public function toArray() { $this->getDeviceInfo(); $this->getPhaseInfo(); $this->getBankInfo(); } protected function getDeviceInfo() { // Parse info preg_match_all( '@<span name="Value_with_Units\?\d+,\d">([0-9.]+) \w+<@Ui', $this->getUrl('loadman.htm'), $matches, PREG_SET_ORDER ); // Store clean stats $this->stats['device']['load'] = 0 + $matches[0][1]; $this->stats['device']['peak'] = 0 + $matches[1][1]; $this->stats['device']['total'] = 0 + $matches[2][1]; } protected function getPhaseInfo() { // Parse info preg_match_all( '@<tr>\s*<td noWrap>&nbsp;([^<]+)</td>\s*<td noWrap>(\d+\.?\d*)\D*</td>\s*<td noWrap>(\d+\.?\d*)\D*</td>\s*<td noWr $this->getUrl('phload.htm'), $matches, PREG_SET_ORDER ); // Store clean stats foreach ($matches as $match) { $this->stats['phases'][substr($match[1],1,1)]['V'] = 0 + $match[2]; $this->stats['phases'][substr($match[1],1,1)]['kW'] = 0 + $match[3]; $this->stats['phases'][substr($match[1],1,1)]['A'] = 0 + $match[4]; } } protected function getBankInfo() { //Parse info preg_match_all( '@<tr>\s*<td noWrap>&nbsp;([^<]+)</td>\s*<td noWrap>(\d+\.?\d*)\D*</td>@Uis', $this->getUrl('bankload.htm'), $matches, PREG_SET_ORDER ); // list($dummy, $bankid, $amp) = $match; // Store clean stats foreach ($matches as $match) { $this->stats['banks'][$match[1]]['A'] = 0 + $match[2]; $phase = floor(($match[1] - 1) / 2) + 1; $this->stats['banks'][$match[1]]['V'] = $this->stats['phases'][$phase]['V']; $this->stats['banks'][$match[1]]['kW'] = $this->stats['banks'][$match[1]]['A'] * $this->stats['banks'][$match[1]]['V'] / 1000; } } private function getUrl($uri) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->baseurl . $uri); curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $content = curl_exec($ch); curl_close($ch); return $content; } public function putval() { var_dump(this->host); $list = array ( array('aaa', 'bbb', 'ccc', 'dddd'), array('123', '456', '789'), array('"aaa"', '"bbb"') ); $file = "/var/lib/rrdcached/db/UcsToCsv.csv"; $fp = fopen($file , "r+"); foreach ($list as $fields) { fputcsv($fp, $fields); } fclose($fp); } /* public function putval() { foreach ($this->stats as $key1 => $pdu){ switch ($key1){ case "device": echo "PUTVAL \"".$this->host."/DEVICE/pdu_global\" ".$this->ts.":${pdu['total']}:${pdu['load']}\n"; break; case "phases": foreach ($pdu as $key2 => $type){ echo "PUTVAL \"".$this->host."/PHASE-$key2/pdu_phase\" ".$this->ts.":${type['V']}:${type['kW']} } break; case "banks": foreach ($pdu as $key2 => $type){ $idPhase = floor(($key2+1)/2); $idBank = ($key2+1)%2+1; echo "PUTVAL \"".$this->host."/PHASE-${idPhase}/pdu_bank-${idBank}\" ".$this->ts.":${type['V']}:${type[ } break; } } }*/
Output for 5.4.0 - 5.4.30
Parse error: syntax error, unexpected 'phload' (T_STRING) in /in/TiSF8 on line 104
Process exited with code 255.
Output for 5.3.0 - 5.3.28
Parse error: syntax error, unexpected T_STRING in /in/TiSF8 on line 104
Process exited with code 255.