Compare last GPS and current GPS data - php

I have a project where i need to send GPS coordinates via. socket in every 6 seconds. The coordinates are stored in a MYsql database. I run a query every 6 seconds and if the last position is different from the current position the application sends the data to the remote server. In the browser it works like a charm but in the terminal i can't use Sessions.
I tried apc_add but according to the PHP manual it is removed a long time ago.
What is the most common way to do a comparsion like that? Store the last coordinates into the database or a text file? Or is there a way to sotore it in run time?
**Here is my main code: **
<?php
require 'bootstrap.php';
use App\Libs\appServiceProvider;
use App\Libs\socketServiceProvider;
use Socket\Raw\Factory;
use App\Models\Koordinata;
$app = new appServiceProvider;
if (empty($_SESSION['lat']) || empty($_SESSION['lon'])) {
$_SESSION['lat'] = 0;
$_SESSION['lon'] = 0;
}
$lastLat = $_SESSION['lat'];
$lastLon = $_SESSION['lon'];
$currentLat = $app->getAllCoordinatesByFszgId($application['fszgId'])->last()->lat;
$currentLon = $app->getAllCoordinatesByFszgId($application['fszgId'])->last()->lon;
if ($lastLat != $currentLat && $lastLon != $currentLon) {
$factory = new Factory();
$socket = $factory->createClient('REMOTEADDRESSE');
echo "Kapcsolat létrehozva\n";
$socket->write("MESSAGE");
echo "Üzenet elküldve\n";
var_dump("Válasz: " . $socket->read(8192));
$socket->close();
} else {
echo "Idle";
$log->addDebug("GPS data NOT CHANGED! STATUS IDLE!");
}
$_SESSION['lat'] = $app->getAllCoordinatesByFszgId($application['fszgId'])->last()->lat;
$_SESSION['lon'] = $app->getAllCoordinatesByFszgId($application['fszgId'])->last()->lon;
?>

Okay, I did it with database and works fine. Here is the code:
<?php
require 'bootstrap.php';
use App\Libs\appServiceProvider;
use App\Libs\socketServiceProvider;
use Socket\Raw\Factory;
use App\Models\Koordinata;
use App\Models\TempKoordinata;
$app = new appServiceProvider;
$last = TempKoordinata::find(1);
if(!empty($last)) {
$lastLat = $last->lat;
$lastLon = $last->lon;
} else {
$temp = new TempKoordinata();
$temp->id = 1;
$temp->lat = 0;
$temp->lon = 0;
$temp->save();
$log->addDebug('No data to compare! Empty tempCoordinate table! Set values to ZERO!');
}
$currentLat = $app->getAllCoordinatesByFszgId($application['fszgId'])->last()->lat;
$currentLon = $app->getAllCoordinatesByFszgId($application['fszgId'])->last()->lon;
if ($lastLat != $currentLat && $lastLon != $currentLon) {
/*$factory = new Factory();
$socket = $factory->createClient('REMOTE');
echo "Kapcsolat létrehozva\n";
$socket->write("MESSAGE");
echo "Üzenet elküldve\n";
var_dump("Válasz: " . $socket->read(8192));
$socket->close();*/
echo "Sending\n";
} else {
echo "Idle\n";
$log->addDebug("GPS data NOT CHANGED! STATUS IDLE!");
}
TempKoordinata::destroy(1);
//Elmentjük a mostani GPS koordinátát
$count = TempKoordinata::all();
//Ha üres az adatbázis akkor elmentjük a koordinátákat
if ($count->count() == 0) {
$temp = new TempKoordinata();
$temp->id = 1;
$temp->lat = $currentLat;
$temp->lon = $currentLon;
$temp->save();
} else {
$log->addDebug("More than one item in the temp table!");
}
?>

Related

How to Verify if XML tag exists with PHP from external server

Please apologyze my English.
I'm developing a kind of sitemap.xml system for my website to include products to my database from external websites, like Google did with his "Spiders", I just developed this whole script to verify if an specific sitemap.xml contains every required tags, and It works: If any tag is missing, the script returns an error, but if all "products" has the required tags, the script returns a success message.
But I only have one problem: if the xml external file has any error like a missing "close" tags, the script stop working and returns an error.
I need to verify the if the external xml file contais any error, and then proceed with the rest of the code that I wrote, if the result is "NO".
this is my developed script:
<?
$sitemap = htmlspecialchars(addslashes(stripslashes(strip_tags(trim($_POST['sitemap'])))));
$ref = htmlspecialchars(addslashes(stripslashes(strip_tags(trim($_POST['ver_id'])))));
error_reporting(E_ALL);
ini_set('display_errors', '1');
session_start();
include dirname(dirname(__FILE__))."/connectdb.php";
$query = "SELECT * FROM website WHERE `ver_id` = '$ref'";
$result = $mysqli->query($query);
/* array asociativo */
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$url = $row['url'];
$long_url = $url.'/'.$sitemap;
?>
<?php
$total = 0;
$xml=simplexml_load_file("$long_url") or die("Error: Cannot create object");
$count = $xml->count();
foreach($xml->children() as $producto) {
$id = $producto->id;
$titulo = $producto->titulo;
$descripcion = $producto->descripcion;
$fotografia = $producto->fotografia;
$precio = $producto->precio;
$costo = $producto->costo;
$horario = $producto->horario;
$tiempo = $producto->tiempo;
$cobertura = $producto->cobertura;
$keywords = $producto->keywords;
$destacar = $producto->destacar;
$delivery = $producto->delivery;
if (!empty($id) && !empty($titulo) && !empty($descripcion) && !empty($fotografia) && !empty($precio) && !empty($costo) && !empty($horario) && !empty($tiempo) && !empty($cobertura) && !empty($keywords) && !empty($destacar) && !empty($delivery)) {
$total = $total + 1; } else { $total = $total - 1; }
}
if($total < $count){ header("Location:/user/sitio.php?id=$ref&mensaje=1"); } else
if($total == $count){
$query = "UPDATE website SET sitemap='$sitemap',sitemap_status='active' WHERE ver_id='$ref'";
$result = $mysqli->query($query);
header("Location:/user/sitio.php?id=$ref&mensaje=2");
}
?>
Solved with this simple script!
<?
$dom = new DOMDocument;
$dom->Load($long_url);
if ($dom->validate()) {
echo "Valid File";
} else { echo "Invalid File"; }
?>
thanks to everyone.

How to download and save arrays of files to my local PC in php

I am currently using a simple html dom to scrape a website, part of the contents I scrape are : images, links, and text. Now what I need to accomplish is to save the scraped data to my local PC or database. Is there a way I can accomplish this using php?
To force download those contents at once to my PC or database.
Will upload my codes if you feel it's necessary.
Thank you in advance...
Edited: Here is my code...
include("../dom/simple_html_dom.php");
if (isset($_POST['submit']))
{
if ($_POST['word1'] != ""){
//posts of the search query
$word1 = $_POST['word1'];
$items = array('url'=>'https://www.example.com/mobile-phones/?q='.str_replace(' ','+',$word1)."/",'img'=>'.image-wrapper img','brand'=>'h2.title span.brand','name'=>'h2.title span.name','price'=>'span.price-box','link'=>'section.products a.link');
$site = new simple_html_dom();
$currentImg = '';
$currentItemBrand = '';
$currentItemName = '';
$currentLink = '';
$currentPrice = '';
$counter = 0;
$number = -1;
$site->load_file($items['url']);
$currentImg = array();
$currentItemBrand = $site->find($items["brand"]);
$currentItemName = $site->find($items["name"]);
$currentLink = $site->find($items["link"]);
$currentPrice = $site->find($items["price"]);
foreach($site->find($items["img"]) as $element) {
$counter2++;
//initializing array objects
if($counter2 % 2 == 0 && $counter2 < 42)
{
$number++;
$currentImg = $element[$number]->src;
$currentItemName[$number]->plaintext;
$currentItemBrand2[$number]->plaintext;
$currentLink[$number]->href;
$currentPrice[$number]->plaintext;
}
}
// My Database Code
$insertSQL = "INSERT INTO items (img, name, link, price, brand) VALUES ('$currentImg', '$currentItemName', '$currentItemLink','$currentPrice','$currentItemBrand')";
mysqli_select_db($elecom_connect,$database_elecom_connect);
$Result1 = mysqli_query($elecom_connect,$insertSQL) or die(mysqli_error($elecom_connect));
exit();
}
}
}
?>
The issue I have with this code is that it only save a reference of the images, to the database...What I want is to download all the arrays of files and then store them in my database....I am a bit confused on how to do that....Will appreciate your suggestions or codes block.

Is there a way to run PHP long loop code faster?

I have a code that runs a query on an external (slow) API and loops through a lot of variables and inserts data, sends email, ect'.
This is the main function as an example:
// MAIN: Cron Job Function
public function kas_alert() {
// 0. Deletes all the saved data from the `data` table 1 month+ ago.
// $this->kas_model->clean_old_rows();
// 1. Get 'prod' table
$data['table'] = $this->kas_model->prod_table();
// 2. Go through each row -
foreach ( $data['table'] as $row ) {
// 2.2. Gets all vars from the first query.
$last_row_query = $this->kas_model->get_last_row_of_tag($row->tag_id);
$last_row = $last_row_query[0];
$l_aaa_id = $last_row->prod_aaa_id;
$l_and_id = $last_row->prod_bbb_id;
$l_r_aaa = $last_row->dat_data1_aaa;
$l_r_and = $last_row->dat_data1_bbb;
$l_t_aaa = $last_row->dat_data2_aaa;
$l_t_and = $last_row->dat_data2_bbb;
$tagword = $last_row->tag_word;
$tag_id = $last_row->tag_id;
$country = $last_row->kay_country;
$email = $last_row->u_email;
$prod_name = $last_row->prod_name;
// For the Weekly report:
$prod_id = $last_row->prod_id;
$today = date('Y-m-d');
// 2.3. Run the tagword query again for today on each one of the tags and insert to DB.
if ( ($l_aaa_id != 0) || ( !empty($l_aaa_id) ) ) {
$aaa_data_today = $this->get_data1_aaa_by_id_and_kw($l_aaa_id, $tagword, $country);
} else{
$aaa_data_today['data1'] = 0;
$aaa_data_today['data2'] = 0;
$aaa_data_today['data3'] = 0;
}
if ( ($l_and_id != 0) || ( !empty($l_and_id) ) ) {
$bbb_data_today = $this->get_data1_bbb_by_id_and_kw($l_and_id, $tagword, $country);
} else {
$bbb_data_today['data1'] = 0;
$bbb_data_today['data2'] = 0;
$bbb_data_today['data3'] = 0;
}
// 2.4. Insert the new variables to the "data" table.
if ($this->kas_model->insert_new_tag_to_db( $tag_id, $aaa_data_today['data1'], $bbb_data_today['data1'], $aaa_data_today['data2'], $bbb_data_today['data2'], $aaa_data_today['data3'], $bbb_data_today['data3']) ){
}
// Kas Alert Outputs ($SEND is echoed in it's original function)
echo "<h1>prod Name: $prod_id</h1>";
echo "<h2>tag id: $tag_id</h2>";
var_dump($aaa_data_today);
echo "aaa old: ";
echo $l_r_aaa;
echo "<br> aaa new: ";
echo $aaa_data_today['data1'];
var_dump($bbb_data_today);
echo "<br> bbb old: ";
echo $l_r_and;
echo "<br> bbb new: ";
echo $bbb_data_today['data1'];
// 2.5. Check if there is a need to send something
$send = $this->check_if_send($l_aaa_id, $l_and_id, $l_r_aaa, $aaa_data_today['data1'], $l_r_and, $bbb_data_today['data1']);
// 2.6. If there is a trigger, send the email!
if ($send) {
$this->send_mail($l_aaa_id, $l_and_id, $aaa_data_today['data1'], $bbb_data_today['data1'], $l_r_aaa, $l_r_and, $tagword, $email, $prod_name);
}
}
}
This CodeIgniter controller is runs every day and I get this running (Cronjob) for too long using almost nothing from the CPU and RAM (RAM is at 400M/4.25G and CPU at ONLY 0.7%-1.3%).
I wonder if there's an option to split all foreach loops to smaller threads (and I'm not sure if forking will do here) and run all the foreach loops in parallel but in a way that it wont get my server crashing.
I'm no DevOps and really interested learning - What should I do in this case?

PHP+Apache2+Ubuntu Server: How to get all threads to work in parallel?

I usually work with web hosting companies but I decided to start learning working with servers to expand my knowledge.
I'll better give a real example to explain my question the best:
I have a web application that gathers data from a slow API that returns JSON data of products.
I have a function running every 1AM running a lot of queries on "id"s in my database.
Crontab:
0 1 * * * cd /var/www/html/tools; php index.php aso Cli_kas kas_alert
So this creates a process for the app (please correct me here if I'm wrong) and each process creates threads, and just to be more accurate, they are multi-threads since they do more than one thing: like pulling data from the DB to get the right variables and string them to the API queries, getting the data from the API, organizing it, searching the relevant data, and then inserting new data to the database.
The main PHP functions:
// MAIN: Cron Job Function
public function kas_alert() {
// 0. Deletes all the saved data from the `data` table 1 month+ ago.
// $this->kas_model->clean_old_rows();
// 1. Get 'prod' table
$data['table'] = $this->kas_model->prod_table();
// 2. Go through each row -
foreach ( $data['table'] as $row ) {
// 2.2. Gets all vars from the first query.
$last_row_query = $this->kas_model->get_last_row_of_tag($row->tag_id);
$last_row = $last_row_query[0];
$l_aaa_id = $last_row->prod_aaa_id;
$l_and_id = $last_row->prod_bbb_id;
$l_r_aaa = $last_row->dat_data1_aaa;
$l_r_and = $last_row->dat_data1_bbb;
$l_t_aaa = $last_row->dat_data2_aaa;
$l_t_and = $last_row->dat_data2_bbb;
$tagword = $last_row->tag_word;
$tag_id = $last_row->tag_id;
$country = $last_row->kay_country;
$email = $last_row->u_email;
$prod_name = $last_row->prod_name;
// For the Weekly report:
$prod_id = $last_row->prod_id;
$today = date('Y-m-d');
// 2.3. Run the tagword query again for today on each one of the tags and insert to DB.
if ( ($l_aaa_id != 0) || ( !empty($l_aaa_id) ) ) {
$aaa_data_today = $this->get_data1_aaa_by_id_and_kw($l_aaa_id, $tagword, $country);
} else{
$aaa_data_today['data1'] = 0;
$aaa_data_today['data2'] = 0;
$aaa_data_today['data3'] = 0;
}
if ( ($l_and_id != 0) || ( !empty($l_and_id) ) ) {
$bbb_data_today = $this->get_data1_bbb_by_id_and_kw($l_and_id, $tagword, $country);
} else {
$bbb_data_today['data1'] = 0;
$bbb_data_today['data2'] = 0;
$bbb_data_today['data3'] = 0;
}
// 2.4. Insert the new variables to the "data" table.
if ($this->kas_model->insert_new_tag_to_db( $tag_id, $aaa_data_today['data1'], $bbb_data_today['data1'], $aaa_data_today['data2'], $bbb_data_today['data2'], $aaa_data_today['data3'], $bbb_data_today['data3']) ){
}
// Kas Alert Outputs ($SEND is echoed in it's original function)
echo "<h1>prod Name: $prod_id</h1>";
echo "<h2>tag id: $tag_id</h2>";
var_dump($aaa_data_today);
echo "aaa old: ";
echo $l_r_aaa;
echo "<br> aaa new: ";
echo $aaa_data_today['data1'];
var_dump($bbb_data_today);
echo "<br> bbb old: ";
echo $l_r_and;
echo "<br> bbb new: ";
echo $bbb_data_today['data1'];
// 2.5. Check if there is a need to send something
$send = $this->check_if_send($l_aaa_id, $l_and_id, $l_r_aaa, $aaa_data_today['data1'], $l_r_and, $bbb_data_today['data1']);
// 2.6. If there is a trigger, send the email!
if ($send) {
$this->send_mail($l_aaa_id, $l_and_id, $aaa_data_today['data1'], $bbb_data_today['data1'], $l_r_aaa, $l_r_and, $tagword, $email, $prod_name);
}
}
}
For #Raptor, this is the function that get's the API data:
// aaa tag Query
// Gets aaa prod dataing by ID.
public function get_data_aaa_by_id_and_tg($id, $tag, $query_country){
$tag_for_url = rawurlencode($tag);
$found = FALSE;
$i = 0;
$data = array();
// Create a stream for Json. That's how the code knows what to expect to get.
$context_opts = array(
'http' => array(
'method' => "GET",
'header' => "Accepts: application/json\r\n"
));
$context = stream_context_create($context_opts);
while ($found == FALSE) {
// aaa Query
$json_query_aaa = "https://api.example.com:443/aaa/ajax/research_tag?app_id=$id&term=$tag_for_url&page_index=$i&country=$query_country&auth_token=666";
// Get the Json
$json_query_aaa = file_get_contents($json_query_aaa, false, $context);
// Turn Json to a PHP array
$json_query_aaa = json_decode($json_query_aaa, true);
// Get the data2
$data2 = $json_query_aaa['tag']['data2'];
if (is_null($data2)){ $data2 = 0; }
// Get data3
$data3 = $json_query_aaa['tag']['phone_prod']['data3'];
if (is_null($data3)){ $data3 = 0; }
// Finally, the main prod array.
$json_query_aaa = $json_query_aaa['tag']['phone_prod']['app_list'];
if ( count($json_query_aaa) > 2 ) {
for ( $j=0; $j<count($json_query_aaa); $j++ ) {
if ( $json_query_aaa[$j]['id'] == $id ) {
$found = TRUE;
$data = $json_query_aaa[$j]['data'] + 1;
break;
}
if ($found == TRUE){
break;
}
}
$i++;
} else {
$data = 0;
break;
}
}
$data['data1'] = $data;
$data['data2'] = $data2;
$data['data3'] = $data3;
return $data;
}
All threads are stacked one after an other, and when one thread is done, only then - the second thread can proceed, ect'.
And in technical view on this, all threads wait in the RAM until the one before them is done working "inside" the CPU. (correct me if I'm wrong again :] )
This doesn't even "tickle" the servers RAM or CPU when looking at it in the process manager (I use "htop"). RAM is at 400M/4.25G and CPU at ONLY 0.7%-1.3%.
Making me feel this isn't the best I can get from my current server, and getting slow results from my web app.
How do I get things done in a way that all threads work in parallel, but not to a point that my app crashes due to lacks of CPU or RAM?

PHP + mySQL : reading 65K lines hangs mysql

I'm developping a php application which is used to process IP adresses. Thus, I'm juggling with mysql tables containing up to 4 billion rows.
I have a script that currently needs to fetch 65536 adresses from this table and the mysql query fails to give a response via PHP or even via phpMyAdmin when I try to extract these 65K lines.
The table containing the IP Adresses has 3 indexes ( 1 unique, 2 primary ) which are supposed to help it go faster but I simply cannot get past having mysql give an associative array back to PHP in order to continue my data processing.
Any tips as to how to circumvent this problem ?
Thx in advance !
$request = new Request(DB_NAME);
$request->select = '*';
$request->from = Etherwan_Adressage_Ip::TABLE_NAME;
$request->where = Etherwan_Adressage_Ip_Ressource::PRIMARY_KEY." = '".$options->Ressource_ID."'";
$request->order = " inet_aton(IP) ";
$Compteur = 0;
$Liste = array();
$result = $request->exec('select');
for ($i=0 ; $i<$result['length'] ; $i+=$CIDR->Adresses_Totales){
$Possible = TRUE;
$Selected = FALSE;
for ($j=$i ; $j<$i+$CIDR->Adresses_Totales ; $j++){
if ($result[$j]['Date_Affectation'] != '0000-00-00 00:00:00'){
if (isset($options->Include)){
if ($options->Include->Type != $result[$j]['Type_Liaison'] || $options->Include->Liaison_ID != $result[$j]['Liaison_ID'] || str_replace('/', '', $options->CIDR) != $result[$j]['Bits']){
$Possible = FALSE;
} else {
$Selected = TRUE;
}
} else {
$Possible = FALSE;
}
break;
}
}
if ($Possible){
$Liste[$Compteur]['text'] = $result[$i]['IP'] . " / " . $result[$i+$CIDR->Adresses_Totales-1]['IP'];
$Liste[$Compteur]['value'] = $result[$i]['Ressource_ID'];
$Liste[$Compteur]['selected'] = $Selected;
$Liste_IP = array();
for ($j=$i ; $j<$i+$CIDR->Adresses_Totales ; $j++){
if ($result[$j]['Nom'] != ''){
$result[$j]['Dispo'] = 0;
} else {
$result[$j]['Dispo'] = 1;
}
$Liste_IP[] = $result[$j];
}
$Liste[$Compteur]['Liste_IP'] = $Liste_IP;
$Compteur++;
}
}
$Liste['maxLength'] = $Liste['length'] = $Compteur;
return $Liste;
Link to table indexes ( JPG )
Link to data sample ( JPG )

Categories