Saving Mikrotik Simple Queue Statistic PHP API - php

I want to save the statistics of the Mikrotik /simple queue using the PHP API. I have been able to pull the data but seems my implementation on the PHP side is a problem. The following is the code and the resulting array object.
foreach ($util->setMenu('/queue simple')->getAll() as $queueEntry) {
// $lasArray = $queueEntry;
print_r($queueEntry);
}
Excerpt for Result since its returning for all users in the office, I have choosen just to display for one user. Take it that PEAR2\Net\RouterOS\Response Object is retuned for all users, i.e all in this case over 50 users. I would like to save this data to database but only the relevant ones like [.id], [name], [target], [limit-at], [max-limit] and [bytes], any assistance here would be highly regarded.
PEAR2\Net\RouterOS\Response Object
(
[unrecognizedWords:protected] => Array
(
)
[_type:PEAR2\Net\RouterOS\Response:private] => !re
[attributes:protected] => Array
(
[.id] => *12
[name] => GikundaPhone
[target] => 192.168.1.108/32
[parent] => none
[packet-marks] =>
[priority] => 8/8
[queue] => default-small/default-small
[limit-at] => 128000/384000
[max-limit] => 384000/384000
[burst-limit] => 0/0
[burst-threshold] => 0/0
[burst-time] => 0s/0s
[bucket-size] => 0.1/0.1
[bytes] => 16515474/129310087
[total-bytes] => 0
[packets] => 127812/133712
[total-packets] => 0
[dropped] => 76/8667
[total-dropped] => 0
[rate] => 0/0
[total-rate] => 0
[packet-rate] => 0/0
[total-packet-rate] => 0
[queued-packets] => 0/0
[total-queued-packets] => 0
[queued-bytes] => 0/0
[total-queued-bytes] => 0
[invalid] => false
[dynamic] => false
[disabled] => false
)
[_tag:PEAR2\Net\RouterOS\Message:private] =>
)

Have found and answer to my own question. This is what I have done. The
foreach ($util->setMenu('/queue simple')->getAll() as $queueEntry) {
// $lasArray = $queueEntry;
print_r($queueEntry);
}
provided alot of information thats unnecessary, so I have found about the routeros_api.class.php downloadable from here and followed but modified information from here. Then just used
$address = 'IPV4_Address_of_router';
$user = 'username_of_router';
$pass = 'password_of_router';
require('routeros_api.class.php');
$API = new routeros_api();
$API->debug = false;
// router credentials and after including the routeros_api.cass.php
if ($API->connect($address, $user, $pass)) {
$results = $API->comm("/queue/simple/print");
foreach ($results as $row) {
$clientName = $row['name'];
$clientIP = $row['target'];
$clientMaxDown = $row['limit-at'];
$clientMaxUp = $row['max-limit'];
$clientDownloads = $row['bytes'];
}
}
Only thing remaining was to save to database which is simple. Maybe someone may get helped someday by this.

Related

PHP SQLITE3 randomly not inserting data into database

I'm parsing a json file to add data to a sqlite database. It works fine apart from the fact that some entries I retrieve from the json file are just not added completely. As an example see this array I got from the json file:
(
[0] => Array
(
[Id] => 4215717
[Guid] => a2fd45b0-c602-4c82-bb30-19aba8bfacdf
[IsRawData] =>
[Name] => test 1
[State] => Released
[Created] => 2015-10-25T10:21:13Z
[CreatorId] => 65edfb59-8087-4f20-81d8-1dc7b94c5624
[Modified] => 2020-02-19T13:08:07Z
[ModifierId] => 65edfb59-8087-4f20-81d8-1dc7b94c5624
[EntryId] => 377794
[LanguageId] => Array
(
[PrimaryLangId] => 7
[SubLangId] => 1
)
)
[1] => Array
(
[Id] => 4215718
[Guid] => 4609ace8-8e6f-457d-a0ae-faa29950cdcb
[IsRawData] =>
[Name] => test 2
[State] => Released
[Created] => 2015-10-25T10:21:41Z
[CreatorId] => 65edfb59-8087-4f20-81d8-1dc7b94c5624
[Modified] => 2017-01-18T13:30:05Z
[ModifierId] => 65edfb59-8087-4f20-81d8-1dc7b94c5624
[EntryId] => 377794
[LanguageId] => Array
(
[PrimaryLangId] => 9
[SubLangId] => 1
)
)
)
For some entries I add to the db using the code below, I randomly only have the first entry from the array written to the db and I can't figure out a pattern for which entries it worked and for which ones it didn't.
Is there something wrong with my code? Or do you see any issues that could cause this behaviour?
// Prepare statement to propagate terms table
$termStmt = $o_db->prepare("INSERT INTO terms
(entryID, termID, term, status, wordclass, usage, type,
primaryLangID, secondaryLangID )
VALUES (:entryID, :termID, :term, :status, :wordclass, :usages, :types,
:primaryLangID, :secondaryLangID)");
// Bind term parameters
$termStmt->bindParam(':entryID', $i_entryID);
$termStmt->bindParam(':termID', $i_termID);
$termStmt->bindParam(':term', $s_term);
$termStmt->bindParam(':status', $s_status);
$termStmt->bindParam(':wordclass', $s_wordclass);
$termStmt->bindParam(':usages', $s_usage);
$termStmt->bindParam(':types', $s_type);
$termStmt->bindParam(':project', $s_project);
$termStmt->bindParam(':primaryLangID', $i_primaryLangID);
$termStmt->bindParam(':secondaryLangID', $i_secondaryLangID);
foreach ($a_entryData as $entry){
$entryID = $entry['entryID']; // entryID comes from another table in the db to get the full entry
$fullEntry = getEntry($entryID); //get entry from json
foreach ($fullEntry as $term){
foreach($term as $termEntry) {
$i_entryID = $termEntry['EntryId'];
$i_termID = $termEntry['Id'];
$s_term =$termEntry['Name'];
$s_status =$termEntry['State'];
$i_primaryLangID = $termEntry['LanguageId']['PrimaryLangId'];
$i_secondaryLangID = $termEntry['LanguageId']['SubLangId'];
// Now get the term properties and the corresponding values
$a_properties = getTermProperties($i_termID);
$a_propertyValues = propertyValues($a_properties);
$a_properties = getTermProperties($i_termID);
$a_values = propertyValues($a_properties);
if (isset($a_values['Wortklasse'])){
$s_wordclass = $a_values['Wortklasse'];
}else{
$s_wordclass = "N\A";
}
if (isset($a_values['Verwendung'])){
$s_usage = $a_values['Verwendung'];
}else{
$s_usage = "N\A";
}
if (isset($a_values['Benennungstyp'])){
$s_type = $a_values['Benennungstyp'];
}else{
$s_type = "N\A";
}
// Execute term statement and add data to the table
$termStmt->execute();
}
}
}
Thanks for your help :)

Multi Array Prep for Charting

I searched for answers and could not find one. This is long and complex and looking for someone smart than me to answer.
Here is the situation. Using PHP, SQL, and trying Pchart.
1) Please do not suggest other charting tools. That is a waste of your time and mine. Other charting tool offer JAVA, JAVASCRIPT, JQUERY, and more. They are slick but what I need is simple (server side creation of data created dynamically and rendered using something on server. Why put the load on client side - at least for now while I investigate.
2) This is PHP specific question not the pchart tool.
Here is the scenario.
I have a table with results from swimming.
I can get the data formatted the way I need.
See below prints.
Array (assume this is first array)
(
[0] => Freestyle 50
[1] => Freestyle 100
[2] => Backstroke 50
[3] => Butterfly 50
[4] => Medley 100
)
Array (assume this is second array)
(
[Backstroke 50] => Array
(
[2012-11-04] => 51.08
[2012-11-10] => 52.50
[2012-11-25] => 55.48
[2012-12-01] => 47.56
[2012-12-08] => 44.51
[2012-12-15] => 49.46
[2013-01-19] => 47.12
[2013-11-03] => 42.52
[2013-11-09] => 40.94
[2013-11-10] => 42.97
[2013-11-16] => 41.36
[2013-12-01] => 45.16
[2013-12-08] => 40.59
[2014-01-12] => 37.67
[2014-02-01] => 37.70
)
[Freestyle 50] => Array
(
[2012-11-04] => 45.33
[2012-11-25] => 50.12
[2013-11-03] => 37.01
[2013-11-10] => 36.73
[2013-12-01] => 39.94
[2014-01-04] => 39.77
[2014-01-12] => 34.22
[2014-02-01] => 34.93
)
[Freestyle 100] => Array
(
[2012-11-04] => 137.73
[2012-11-10] => 126.86
[2012-12-08] => 133.65
[2013-11-03] => 124.14
[2013-11-10] => 121.94
[2013-11-16] => 121.10
[2013-12-01] => 130.99
[2013-12-07] => 118.27
[2013-12-08] => 122.44
[2014-01-04] => 131.38
[2014-01-11] => 115.95
[2014-01-18] => 120.06
)
[Medley 100] => Array
(
[2012-12-01] => 146.66
[2013-01-19] => 143.88
[2013-11-03] => 137.37
[2013-11-09] => 133.05
[2013-11-10] => 134.69
[2013-12-01] => 145.29
[2013-12-07] => 130.15
[2013-12-08] => 130.92
[2014-01-12] => 129.33
[2014-01-18] => 130.81
[2014-02-01] => 128.57
)
[Butterfly 50] => Array
(
[2013-12-08] => 46.82
[2014-01-12] => 43.66
)
)
Array (assume this is third array)
(
[2] => 2012-11-04
[14] => 2012-11-10
[1] => 2012-11-25
[31] => 2012-12-01
[15] => 2012-12-08
[26] => 2012-12-15
[24] => 2013-01-19
[7] => 2013-11-03
[28] => 2013-11-09
[4] => 2013-11-10
[11] => 2013-11-16
[3] => 2013-12-01
[9] => 2013-12-07
[12] => 2013-12-08
[5] => 2014-01-04
[10] => 2014-01-11
[0] => 2014-01-12
[13] => 2014-01-18
[6] => 2014-02-01
)
To line up the charting tool I need to have it in a string like this.
============== Backstroke 50 =================
51.08,52.50,55.48,47.56,44.51,49.46,47.12,42.52,40.94,42.97,41.36,45.16,40.59,37.67,37.70Create or Render Chart
"2012-11-04","2012-11-10","2012-11-25","2012-12-01","2012-12-08","2012-12-15","2013-01-19","2013-11-03","2013-11-09","2013-11-10","2013-11-16","2013-12-01","2013-12-08","2014-01-12","2014-02-01"
============== Freestyle 50 =================
45.33,50.12,37.01,36.73,39.94,39.77,34.22,34.93
"2012-11-04","2012-11-25","2013-11-03","2013-11-10","2013-12-01","2014-01-04","2014-01-12","2014-02-01"
============== Freestyle 100 =================
137.73,126.86,133.65,124.14,121.94,121.10,130.99,118.27,122.44,131.38,115.95,120.06
"2012-11-04","2012-11-10","2012-12-08","2013-11-03","2013-11-10","2013-11-16","2013-12-01","2013-12-07","2013-12-08","2014-01-04","2014-01-11","2014-01-18"
============== Medley 100 =================
146.66,143.88,137.37,133.05,134.69,145.29,130.15,130.92,129.33,130.81,128.57
"2012-12-01","2013-01-19","2013-11-03","2013-11-09","2013-11-10","2013-12-01","2013-12-07","2013-12-08","2014-01-12","2014-01-18","2014-02-01"
============== Butterfly 50 =================
46.82,43.66
"2013-12-08","2014-01-12"
What I am trying to achieve is to get the array #3 assumed. To be inserted in all of these list above. Example: butterfly has two dates. But I want to show all dates, and insert void times for the dates. This will allow the line getting rendered on the chart to be full use of the dates being displayed.
Here is the code:
First Array: Plus third array below this while:
while($result->fetchInto($data,DB_FETCHMODE_ASSOC)) {
$count ++;
$stroke = $data['stroke'];
$distance = $data['distance'];
$start = mysql2date($data['start']);
$dateARR[] = $data['start'];
$stroke = trim($c->getStrokeName($stroke));
$strokestr = $stroke . " " . $distance;
if($strokestr != $lastEvent) {
$strokeARR[] = $strokestr;
$lastEvent = $strokestr;
}
$lastEvent = $strokestr;
}
$dateARR = array_unique($dateARR);
asort($dateARR);
Second Array:
while($result->fetchInto($data,DB_FETCHMODE_ASSOC)) {
$stroke = $data['stroke'];
$distance = $data['distance'];
$start = mysql2date($data['start']);
$time = $c->integer2time($data['time']);
$time = str_replace(':', '', $time);
$meet = $data['meet'];
$stroke = trim($c->getStrokeName($stroke));
$strokestr = $stroke . " " . $distance;
$timeARR[$strokestr][$data['start']] = $time;
$count ++;
}
Should or how to make use of the while loop two in the second array and insert missing dates. Should I prefill the dates then compare during the loop to place the time or the VOID of time while building timeARR?
Any ideas out there?
Figured it out now.
// Loop through Stroke and add all dates.
foreach ($strokeARR as $key1 => $val1) {
print "$key1 = $val1\n";
foreach ($dateARR as $key2 => $val2) {
print "$key2 = $val2\n";
$TestARR[$val1][$val2] = "VOID";
}
}
I had to loop through and prefill with full date range then pre fill with VOID.
When I enter the second while loop I end up inserting time where needed. worked out. I wish I can do this in one pass but I am not that good.

Last-Modified to check feed is updated since last download

i am using PHP to read feed xml in every half an hour, as of now i am always reading whole feed file whether it is updated or not. But i want to check whether feed xml is update since last download, if updated then only read the xml otherwise not.
I am trying to achieve it using below code, but $lastmodifiedRSS is always empty. I am not sure what is going wrong in my code. If i get $lastmodifiedRSS then i can easily compare it with last loaded time and then decide what to do.
If any Expert can share some info that would be great. Thank you in advance.
//get feed information & content
$feed = new feed($rssurl);
$feed->load();
//get the last modified date (web server information)
$lastmodifiedRSS = $feed->http->get_header_value('Last-Modified');
function http($url)
{
$this->url($url);
$this->user_agent = 'posh';
$this->header = "";
$this->code = "";
$this->codeStr = "";
$this->max_d = 5;
$this->authorization = "";
$this->proxy_auth = "";
$this->url = $url;
$this->addProxy();
}
function feed($url)
{
if ($url) {
$this->url = $url;
$this->http = new http($url);
//$this->addProxy();
} else {
$this->http = new http('');
}
}
function load()
{
$content= $this->http->get();
if (!$content)
return false;
$content = $this->transcode($content);
return $content;
}
function get_header_value($name)
{
if (preg_match("/$name: ?([^\\r\\n]*)\\r\\n/m",$this->head,$matches) !== false)
{
if (count($matches) > 1)
{
return $matches[1];
}
}
return false;
}
Regards,
Mona
Make use of stat() in PHP
<?php
print_r(stat('users.json'));
OUTPUT:
Array ( [0] => 2 [1] => 0 [2] => 33206 [3] => 1 [4] => 0 [5] => 0 [6] => 2 [7] => 298 [8] => 1384073940 [9] => 1384073940 [10] => 1368626190 [11] => -1 [12] => -1 [dev] => 2 [ino] => 0 [mode] => 33206 [nlink] => 1 [uid] => 0 [gid] => 0 [rdev] => 2 [size] => 298 [atime] => 1384073940 [mtime] => 1384073940 [ctime] => 1368626190 [blksize] => -1 [blocks] => -1 )
Source
Keeping a track of the variables [atime] , [size] can help you achieve what you are trying to do.
This kind of checking are better handled with HTTP headers itself.
If the server sends Etag header in the response, you could use it with the If-None-Match header on the next request to know if the file has changed.
Similarly, if the server sends Last-Modified header in the response, you could use this timestamp with the If-Modified-Since header on the next request to know if the file got changed.
Just to let you know what you could do, I can't contribute any code cause I don't know which HTTP library you're using here.

Twitter API - Same cursor error

From REST API of twitter I user get/followers function.
I pasted a code snippet below.
My problem is, modt of the time I get followers' ids successfully. But when a user has more than 5000-6000 followers then my results comes wrong.
When I check from user's profile page, I see that user has 5500 followers, but when I run following code, most of the time 5500 ids come, but sometimes 29994 followers come inside $ids variable. Now I'm logging the results having more then 29k followers. I saw some of the requests returned with 29994 followers, but I couldn't find the answer.
Do I miss something in get ids - cursor approach? Thank you
Edit: After some debugging I logged "$cursor_archieve" parameter and found out this:
* Sometimes next_cursor and previous_cursor comes same:
Array
(
[0] => -1
[1] => 1400573121087317741
[2] => 1400573121087317741
[3] => 1400573121087317741
[4] => 1400573121087317741
[5] => 1400573121087317741
[6] => 1400573121087317741
)
So in this situation, although user has 7100 followers I get only 5000 followers
Sometimes cursors come sequentially same:
Array
(
[0] => -1
[1] => 1404335879106773348
[2] => 1404341060469987526
[3] => 1404338682006540390
[4] => 1404341060469987526
[5] => 1404335879106773348
[6] => 1404338682006540390
)
My code is like this:
public function getIds($user = "someuser"){
$tmhOAuth = new tmhOAuth(array( 'consumer_key' => YOUR_CONSUMER_KEY,
'consumer_secret' => YOUR_CONSUMER_SECRET,
'user_token' => $atoken1, 'user_secret' => $atoken2, ));
$cursor = '-1';
$ids = array();
$cursor_archieve = array();
while(true):
$code=$tmhOAuth->request('GET', $tmhOAuth->url('1/followers/ids'),
array('screen_name' => $user, 'cursor' => $cursor));
if ($code == 200) {
$responseData = json_decode($tmhOAuth->response['response'],true);
$ids = array_merge($ids, $responseData['ids']);
$cursor = $responseData['next_cursor_str'];
$cursor_archieve[] = $cursor;
} else {
return 0;
}
if ( $cursor == '0' || count($ids) >= 29000 ) {
break;
}
endwhile;
return $ids;
}
edit2: Should I make "array_unique" to delete duplicate ids, or doesn't use next cursor if previous_cursor=next cursor or any other option?
In every case user has 5500-6500 followers. So If I take only one cursor, I only can get first 5000 followers.
The reason was a programatic error in my codes. I fixed it after a week's debug session

Problems with array when getting data from a database

I'm trying to get some data from my database, and then pass it into an array for later use. I am using MySQLi for my driver.
Here's my code:
// Build a query to get skins from the database
$stmt = $mysqli->prepare('SELECT id, name, description, author, timestamp, url, preview_filename FROM `skins` LIMIT 0, 5');
$stmt->execute();
$stmt->bind_result($result['id'], $result['name'], $result['desc'], $result['auth'], $result['time'], $result['url'], $result['preview']);
// The skins array holds all the skins on the current page, to be passed to index.html
$skins = array();
$i = 0;
while($stmt->fetch())
{
$skins[$i] = $result;
$i++;
}
print_r($skins);
The problem is, when this executes, the $skins array contains the last result row from the query. This is the print_r of $skins:
Array
(
[0] => Array
(
[id] => 3
[name] => sdfbjh
[desc] => isdbf
[auth] => dfdf
[time] => 1299970810
[url] => http://imgur.com/XyYxs.png
[preview] => 011e5.png
)
[1] => Array
(
[id] => 3
[name] => sdfbjh
[desc] => isdbf
[auth] => dfdf
[time] => 1299970810
[url] => http://imgur.com/XyYxs.png
[preview] => 011e5.png
)
[2] => Array
(
[id] => 3
[name] => sdfbjh
[desc] => isdbf
[auth] => dfdf
[time] => 1299970810
[url] => http://imgur.com/XyYxs.png
[preview] => 011e5.png
)
)
As you can see, the last result from the query is populating all of the array entries for some reason.
Can anyone explain this behaviour and tell me what I'm doing wrong? Thanks. :)
EDIT: Here's the solution:
while($stmt->fetch())
{
foreach($result as $key=>$value)
{
$tmp[$key] = $value;
}
$skins[$i] = $tmp;
$i++;
}
Quoting the (now) first note on the mysqli::fetch manual page, emphasis mine :
the problem is that the $row returned is reference and not data.
So, when you write $array[] = $row, the $array will be filled up with the last element of the dataset.

Categories