laravel parse large xml file to mysql - php

$app = new Container;
$document = new OrchestraDocument($app);
$reader = new OrchestraReader($document);
$xml = $reader->load($path);
$xml1 = simplexml_load_file($path);
// print_r($xml1);
$json = json_encode($xml1);
$array = json_decode($json, true);
$clien =$array['cliente'];
//controll empty value
$clien = array_map(function($i) {
$i['indirizzo'] = empty($i['indirizzo']) ? '' : $i['indirizzo'];
$i['cap'] = empty($i['cap']) ? '' : $i['cap'];
$i['citta'] = empty($i['citta']) ? '' : $i['citta'];
$i['prov'] = empty($i['prov']) ? '' : $i['prov'];
$i['piva'] = empty($i['piva']) ? '' : $i['piva'];
$i['cfisc'] = empty($i['cfisc']) ? '' : $i['cfisc'];
$i['luogo_nasc'] = empty($i['luogo_nasc']) ? '' : $i['luogo_nasc'];
$i['data_nasc'] = empty($i['data_nasc']) ? '' : $i['data_nasc'];
$i['sesso'] = empty($i['sesso']) ? '' : $i['sesso'];
$i['tele'] = empty($i['tele']) ? '' : $i['tele'];
$i['mail'] = empty($i['mail']) ? '' : $i['mail'];
$i['cell'] = empty($i['cell']) ? '' : $i['cell'];
$i['cod_card'] = empty($i['cod_card']) ? '' : $i['cod_card'];
$i['cod_card1'] = empty($i['cod_card1']) ? '' : $i['cod_card1'];
$i['punti_card'] = empty($i['punti_card']) ? '' : $i['punti_card'];
return $i;
}, $clien);
$collection = collect($clien);
$collection1 = $collection->chunk(500);
i'm try to import a large xml file into my mysql database.
i'm load my xml file into object, i'm tranform this object into array, i'm control the empty value, and ok.
Now because i have a large query i tranform my array in laravel collection and chunk for isert 500 records at a time, but now i have a new object $collection1. How i enter the query? i have to trasform again in array?
for($i=0;$i<count($collection1);$i++) {
var_dump($collection1[$i]);
$collection1[$i]= stdToArray;
DB::connection()->disableQueryLog();
DB::table('clientis')->insert($collection1[$i]);
foreach( $collection1[$i] as $k=>$v){
var_dump($v);
}
}
thi code don't work

Looking at your code you don't need to create a collection. You can just array_chunk the data:
/*
$collection = collect($clien);
$collection1 = $collection->chunk(500);
*/
$collection = array_chunk($clien, 500, true);

Related

POST API Issue - Not functioning

Good afternoon,
I'm having trouble with an api POST request - it simply isn't going anywhere, it's not showing on the API log at all - I've got various get requests which work fine but this one just doesn't - I'm unsure what I am doing wrong.
public function broadband_plan(){
$common = array();
$common['main_menu'] = 'Broadband plan';
$cli_or_postcode = isset($_GET["cli_or_postcode"]) ? $_GET["cli_or_postcode"] : "";
$district_id = isset($_GET["district_id"]) ? $_GET["district_id"] : "";
$sub_premises = isset($_GET["sub_premises"]) ? $_GET["sub_premises"] : "";
$premises_name = isset($_GET["premises_name"]) ? $_GET["premises_name"] : "";
$thoroughfare_number = isset($_GET["thoroughfare_number"]) ? $_GET["thoroughfare_number"] : "";
$thoroughfare_name = isset($_GET["thoroughfare_name"]) ? $_GET["thoroughfare_name"] : "";
$locality = isset($_GET["locality"]) ? $_GET["locality"] : "";
$postcode = isset($_GET["postcode"]) ? $_GET["postcode"] : "";
$nad_key = isset($_GET["nad_key"]) ? $_GET["nad_key"] : "";
$post_town = isset($_GET["post_town"]) ? $_GET["post_town"] : "";
$county = isset($_GET["county"]) ? $_GET["county"] : "";
$district_id = isset($_GET["district_id"]) ? $_GET["district_id"] : "";
$request = array();
$request['apiUsername'] = 'XXXXXXX';
$request['apiPassword'] = 'XXXXXXX';
$request['encryption'] = 'SHA-512';
$request['platform'] = 'LIVE';
$request['method'] = 'POST';
$address = '{
"sub_premises": $sub_premises,
"premises_name": $premises_name,
"thoroughfare_number": $thoroughfare_number,
"thoroughfare_name": $thoroughfare_name,
"post_town": $post_town,
"postcode": $postcode,
"district_id": $district_id,
"nad_key": $nad_key
}';
$address = json_encode($address);
$request['url'] = "/broadband/availability" . $address;
$broadband_plan_detail = array();
if (!empty($address)) {
$broadband_plan_detail = $this->get_plan($request);
}
return view('front.broadband_plan',compact('common','broadband_plan_detail'));
}
The $address after $address = json_encode($address); will be an object, it cannot be concatenated as a string in $request['url'] = "/broadband/availability" . $address;
Please check your API URL and ensure you follow their structure, for example if the API URL accept the parameters as GET request, you can pass the parameters likes:
$request['url'] = "/broadband/availability?sub_premises=" . $sub_premises . "&premises_name=" . $premises_name...
Base on your code, the API method is POST, so you might need to prepare the parameters as an array.

How to export large size json feed to MYSQL Table?

I am having a PHP script that exports a data from JSON feed to a MYSQL table below is the code I am using.
<?php
#ini_set( 'max_execution_time', 0);
require_once 'db_connect.php';
/* DEFINE TABLE NAME AND SOURCE URL HERE */
$str_tbl_name = 'table_name';
$str_source_url = 'https://www.json-feed.com/example';
/* FETCH DATA FROM RESOURCE LINK */
$str_source_content = file_get_contents($str_source_url);
$arr_data = json_decode($str_source_content);
$str_date_time = date('Y-m-d H:i:s');
$str_start_date = date('Y-m-d H:i:s', strtotime($arr_data->meta->start_date));
$str_end_date = date('Y-m-d H:i:s', strtotime($arr_data->meta->end_date));
$arr_value_sql = [];
foreach ($arr_data->data as $data) {
/* DATA CLEANSING */
$data->source_subregion = !empty($data->source_subregion) ? $con_db->real_escape_string($data->source_subregion) : null;
$data->source_country_code = !empty($data->source_country_code) ? $con_db->real_escape_string($data->source_country_code) : null;
$str_input_name = !empty($data->metadata->search_name) ? $con_db->real_escape_string($data->metadata->search_name) : null;
$data->document_url = !empty($data->document_url) ? $con_db->real_escape_string($data->document_url) : null;
$str_document_tags = !empty((array) $data->document_tags) ? $con_db->real_escape_string(serialize($data->document_tags)) : null;
$data->document_sentiment = !empty($data->document_sentiment) ? $con_db->real_escape_string($data->document_sentiment) : null;
$data->source_name = !empty($data->source_name) ? $con_db->real_escape_string($data->source_name) : null;
$data->document_publish_date = !empty($data->document_publish_date) ? $con_db->real_escape_string($data->document_publish_date) : null;
$str_document_matched_keywords = !empty((array) $data->document_matched_keywords) ? $con_db->real_escape_string(implode(',', $data->document_matched_keywords)) : null;
$data->document_language_code = !empty($data->document_language_code) ? $con_db->real_escape_string($data->document_language_code) : null;
$str_document_key_phrases = !empty((array) $data->document_key_phrases) ? $con_db->real_escape_string(implode(',', $data->document_key_phrases)) : null;
$data->document_hit_sentence = !empty($data->document_hit_sentence) ? $con_db->real_escape_string($data->document_hit_sentence) : null;
$data->document_hidden = ($data->document_hidden === true) ? 'TRUE' : 'FALSE';
$str_document_authors = !empty($data->document_authors[0]->name) ? $con_db->real_escape_string($data->document_authors[0]->name) : null;
$data->document_city = !empty($data->document_city) ? $con_db->real_escape_string($data->document_city) : null;
$str_value_sql = " ("
. "'{$data->document_publish_date}'"
. ", '{$data->document_url}'"
. ", '{$data->source_name}'"
. ", '{$str_document_authors}'"
. ", '{$data->source_country_code}'"
. ", '{$data->source_subregion}'"
. ", '{$data->document_language_code}'"
. ", '{$data->source_reach}'"
. ", '{$data->source_ave}'"
. ", '{$data->document_sentiment}'"
. ", '{$str_document_key_phrases}'"
. ", '{$str_input_name}'"
. ", '{$str_document_matched_keywords}'"
. ", '{$data->document_city}'"
. ")";
array_push($arr_value_sql, $str_value_sql);
}
Now the issue is if the JSON feed is small size data below 100MB all goes well. But one of the JSON feed I have is 1.5GB in size and the PHP runs out of memory even after I allocate 8 GB memory to it.
How do I either get this done in one script run or how to change the script so that it will break up the JSON feed and will export lets says 500 rows at a time to MYSQL table.
So regarding what I said in the comment:
Basically when you are building the str_value_sql array don't push the entire file to it, do the for loop like
for (i=1000; i<number_lines_in_feed; i+=1000) {
for (j=0; j<i; j++) {
array_push($arr_value_sql, $str_value_sql);
}
run sql query for insert
}
mind you that this is just a quick example but the idea here is that you parse the json data object 1000 items at a time and this make the arr_value_sql a lot smaller and would probably make the query memory footprint and such a lot smaller etc... I would suggest trying that ... I hope it fits the structure of your JSON

Is it possible to pass variables by name in Laravel

I am coming from CF to PHP, specifically Laravel, so I apologize for such a basic question.
I have a function in a controller :
public function search(
$surveyId = 0
, $sampleId = 0
, $data = []
, $dataSeg = 0
, $returnLimit = 1000
, $returnStartRow = 1
, $sortOn = ""
, $sortDir = ""
, $previousData = []
){
// bunch of code
}
I am wondering if it is possible to pass in only specific variables that are needed like I can in cf? Trying to do something like this :
$myData = [
"State" => "AZ"
];
$allRecords = (new MyController)->search($surveyId=3762,$data=$myData,$sortOn="name");
I know this is a simple process in cf if the only three variable I need for a specific call are, in this case, the surveyId and the data array and the sortOn but I can't find the correct way to do this in Laravel (php) or even if this is a possibility at all. BTW I am using Laravel 5.7
RESULT :
Based on #Nguyen's answer here is what the beginning of my controller ended up looking like, incase it is helpful to someone else :
public function search($params){
// parameters that should be passed in
$surveyId = key_exists("surveyId", $params) ? $params['surveyId'] : 0;
$sampleId = key_exists("sampleId", $params) ? $params['sampleId'] : 0;
$data = key_exists("data", $params) ? $params['data'] : [];
$dataSeg = key_exists("dataSeg", $params) ? $params['dataSeg'] : 0;
$returnLimit = key_exists("returnLimit", $params) ? $params['returnLimit'] : 1000;
$returnStartRow = key_exists("returnStartRow", $params) ? $params['returnStartRow'] : 1;
$sortOn = key_exists("sortOn", $params) ? $params['sortOn'] : "";
$sortDir = key_exists("sortDir", $params) ? $params['sortDir'] : "";
$previousData = key_exists("previousData", $params) ? $params['previousData'] : [];
// end parameters that should be passed in
## logic code
}
You can't pass parameter by name. If you want, change parameter to array like this:
public function search($param){
// your code here
}
$allRecords = (new MyController)->search(["surveyId"=>3762,"data"=>$myData,"sortOn"=>"name"]);

Laravel Request does not return result

I am using AWS and laravel 5.1.
I have two issues:
Most of the time API returns the expected result but sometimes server
doesn't return result for longer time. It might be due to the server
load(Although we don't have too much load on server).
Clients wait for few minutes and after that application crashes. Any
Advice.
If Client made an API request and before getting result, if device
loses internet connection. Server has process the data but unable to
deliver it to the client. how to handle this (Both in get as well as
post request)
Sample Code for first Part
public function psychometricResponses(Request $request)
{
$questionId=$request->get('questionId');
$answerId=$request->get('answerId');
$from = $request->has('from');
$userId = $request->has('id') ? $request->get('id') : 0;
$name = $request->has('name') ? $request->get('name') : '';
$email = $request->has('email') ? $request->get('email') : '';
$gender = $request->has('gender') ? $request->get('gender') : '';
$relationshipStatus = $request->has('relationshipStatus') ? $request->get('relationshipStatus') : '';
if($userId!=0){
$status = 1;
User::where('id',$userId)->update(['status'=>1]);
}else{
$status = 0;
}
$id = $this->calculatePersonality($questionId, $answerId, $userId, $from, $name, $email, $gender, $relationshipStatus);
return json_encode(['code'=>200,'message'=>"success",'response'=>array('personality'=>$this->_getPersonality($id),'psychometricId'=>$id,'status'=>$status)]);
}
public function calculatePersonality($questionId, $answerId, $userId, $from, $name, $email, $gender, $relationshipStatus)
{
//
$questionArray = explode(',', $questionId); //arraySize is 60
$answerArray = explode(',', $answerId); // //arraySize is 60
$this->questionAll = $this->_questionAll();
$personality = array('O'=>0,'C'=>0,'E'=>0,'A'=>0,'N'=>0);
for($i=0; $i<sizeof($answerArray);$i++){
$type = $this->_getQuestionType($questionArray[$i]);
if($type=='O'){
$personality['O'] = $answerArray[$i]=='1' ? $personality['O']+1 : $personality['O']-1;
}else if($type=='OR'){
$personality['O'] =$answerArray[$i]=='1' ? $personality['O']-1 : $personality['O']+1;
}else if($type=='C'){
$personality['C'] = $answerArray[$i]=='1' ? $personality['C']+1 : $personality['C']-1;
}else if($type=='CR'){
$personality['C'] = $answerArray[$i]=='1' ? $personality['C']-1 : $personality['C']+1;
}else if($type=='E'){
$personality['E'] = $answerArray[$i]=='1' ? $personality['E']+1 : $personality['E']-1;
}else if($type=='ER'){
$personality['E'] = $answerArray[$i]=='1'?$personality['E']-1 : $personality['E']+1;
}else if($type=='A'){
$personality['A'] = $answerArray[$i]=='1' ? $personality['A']+1 : $personality['A']-1;
}else if($type=='AR'){
$personality['A'] = $answerArray[$i]=='1' ? $personality['A']-1 : $personality['A']+1;
}else if($type=='N'){
$personality['N'] = $answerArray[$i]=='1'? $personality['N']+1 : $personality['N']-1;
}else if($type=='NR'){
$personality['N'] = $answerArray[$i]=='1' ? $personality['N']-1 : $personality['N']+1;
}
}
$mod = array();
$mod['O'] = abs($personality['O']);
$mod['C'] = abs($personality['C']);
$mod['E'] = abs($personality['E']);
$mod['A'] = abs($personality['A']);
$mod['N'] = abs($personality['N']);
arsort($mod);
$values = array_keys($mod);
$pTypeOne = $personality[$values[0]] < 0 ? $values[0].'R' : $values[0];
$pTypeTwo = $personality[$values[1]] < 0 ? $values[1].'R' : $values[1];
$pTypeThree = $personality[$values[2]] < 0 ? $values[2].'R' : $values[2];
$pTypeFour = $personality[$values[3]] < 0 ? $values[3].'R' : $values[3];
$pTypeFive = $personality[$values[4]] < 0 ? $values[4].'R' : $values[4];
$psychometricResponse = PsychometricResponse::create(array('typeA'=>$pTypeOne, 'valueA'=>$personality[$values[0]],'typeB'=>$pTypeTwo, 'valueB'=>$personality[$values[1]],
'typeC'=>$pTypeThree, 'valueC'=>$personality[$values[2]],'typeD'=>$pTypeFour, 'valueD'=>$personality[$values[3]],
'typeE'=>$pTypeFive, 'valueE'=>$personality[$values[4]],'questionId'=>$questionId,'answerId'=>$answerId, 'userId'=>$userId, 'from'=>$from,'name'=>$name,'email'=>$email, 'gender'=>$gender,'relationshipStatus'=>$relationshipStatus));
return $psychometricResponse->id;
}
private function _questionAll(){
$question = PsychometricQuestion::where('isActive',1)->get(['id','type']);
$result =array();
for($i=0;$i<sizeof($question);$i++){
$result[$question[$i]->id] = $question[$i]->type;
}
return $result;
}
private function _getQuestionType($id){
return $this->questionAll[$id];
}
private function _getPersonality($id){
$personality = PsychometricResponse::where('id',$id)->first(['typeA','typeB','typeC','typeD','typeE','valueA','valueB']);
$combined = array($personality->typeA,$personality->typeB);
sort($combined);
$combinedPersonality = PsychometricResult::where('type',$combined[0]."-".$combined[1])->first(['type','keyword','description','colorCode']); //Table PsychometricResult has 40 rows only.
$primaryPersonality = PsychometricResult::where('type',$personality->typeA)->first(['type','keyword','description','colorCode']);
$secondaryPersonality = PsychometricResult::where('type',$personality->typeB)->first(['type','keyword','description','colorCode']);
$permutation = $this->_getAllPermutation($personality);
$stylePersonality = PsychometricStyle::whereIn('type',$permutation)->get(['type','aspect','style','description']);
$primaryPercentage = round(abs($personality->valueA)/(abs($personality->valueA)+abs($personality->valueB)), 2);
$secondaryPercentage = 1 - $primaryPercentage;
return array('primaryPersonality'=>$primaryPersonality,'secondaryPersonality'=>$secondaryPersonality,'combinedPersonality'=>$combinedPersonality,
'styles'=>$stylePersonality,'primaryPercentage'=>$primaryPercentage,'secondaryPercentage'=>$secondaryPercentage);
}
private function _getAllPermutation($personality){
$combined = array($personality->typeA,$personality->typeB,$personality->typeC,$personality->typeD,$personality->typeE);
sort($combined);
$permutation = array();
for($i=0;$i<sizeof($combined);$i++){
for($j=$i+1;$j<5;$j++){
array_push($permutation,$combined[$i]."-".$combined[$j]);
}
}
return $permutation;
}

Search based on URL varibles

I want my search to focus on variables in the URL string. Currently it uses a form based search.
It uses this
<?php
include('db.php'); // include your code to connect to DB.
$tbl_name="mobile"; //your table name
$whereClauses = array();
if (! empty($_POST['Model'])) $whereClauses[] ="model='".mysql_real_escape_string($_POST['Model'])."'";
if (! empty($_POST['Mins'])) $whereClauses[] ="minutes='".mysql_real_escape_string($_POST['Mins'])."'";
if (! empty($_POST['Texts'])) $whereClauses[] ="texts='".mysql_real_escape_string($_POST['Texts'])."'";
if (! empty($_POST['Freegifts'])) $whereClauses[] ="free_gift='".mysql_real_escape_string($_POST['Freegifts'])."'";
if (! empty($_POST['Network'])) $whereClauses[] ="network_name='".mysql_real_escape_string($_POST['Network'])."'";
if (! empty($_POST['Merchant'])) $whereClauses[] ="merchant_category='".mysql_real_escape_string($_POST['Merchant'])."'";
$where = '';
if (count($whereClauses) > 0) { $where = ' WHERE '.implode(' AND ',$whereClauses); }
$sql = mysql_query("SELECT * FROM $tbl_name".$where);
?>
However, I have added this to my page:
<?php
$model = $_GET['model']; //gets model from URL
$mins = $_GET['mins']; //gets mins from URL
$texts = $_GET['texts']; //gets mins from URL
$freegift = $_GET['free-gift']; //gets mins from URL
$network = $_GET['network']; //gets mins from URL
$plan = $_GET['plan']; //gets mins from URL
?>
It needs to be so not all variables are required. Any help would be appreciated.
Thanks in advance :)
There are a number of options that will return value or NULL:
<?php
$model = (isset($_GET['model']) ? $_GET['model'] : NULL);
$mins = (isset($_GET['mins']) ? $_GET['mins'] : NULL);
$texts = (isset($_GET['texts']) ? $_GET['texts'] : NULL);
$freegift = (isset($_GET['free-gift']) ? $_GET['free-gift'] : NULL);
$network = (isset($_GET['network']) ? $_GET['network'] : NULL);
$plan = (isset($_GET['plan']) ? $_GET['plan'] : NULL);
?>
This will get the variables from their correponding $_GET element, only if there is one set.
Alternative syntax (shorter):
<?php
$model = ($_GET['model'] ? $_GET['model'] : NULL);
$mins = ($_GET['mins'] ? $_GET['mins'] : NULL);
$texts = ($_GET['texts'] ? $_GET['texts'] : NULL);
$freegift = ($_GET['free-gift'] ? $_GET['free-gift'] : NULL);
$network = ($_GET['network'] ? $_GET['network'] : NULL);
$plan = ($_GET['plan'] ? $_GET['plan'] : NULL);
?>
Alternative (untested though):
<?php
$vars = array('model','mins','texts','free-gift','network','plan');
foreach($vars as $value) {
$$value = (isset($_GET[$value]) ? $_GET[$value] : NULL);
}
?>
EDIT:
In order to set a WHERE clause based on it, I suggest:
<?php
$vars = array('model','mins','texts','free-gift','network','plan');
foreach($vars as $value) {
$$value = (isset($_GET[$value]) ? $_GET[$value] : NULL);
unset $vars[$value]; //sweeping the NULL ones
}
$where_clause = $vars[0]; //the only remaining value after previous cleanup
?>

Categories