- Why are you doing it from your local machine in the first place? Maybe we can suggest a better way
- If you can show your code we can also try and recommend optimizations
- How are you connected to the database? Direct connection to the server or using VPN?
Include data from local server on remote server too slow
I'm using php artisan serve on my computer. However, I want to send the data directly from my pc to my remote server. I made the connection to the remote database and so far everything is right.
However, the speed of adding the data (which comes from a csv file) is very slow. It is sent one by one and I often get the error: Maximum execution time of 300 seconds exceeded
When I do the same directly on my server, the process is very fast, getting to include 20 records every second.
Why is it so slow when I do this on my local computer?
How can I resolve this?
My csv files with the logs contain urls from another site, from which I get the image through file_get_contents() . It turns out that when I do this from my server, file_get_contents() doesn't work because cloudflare blocks it. However, from my local computer it works fine. Hence the reason to do it locally.
My code basically takes the csv file, which contains 3,000 records each, and sends it, see:
class LivrosController extends Controller
{
public function importar() {
function csv_to_array($file_name) {
$data = $header = array();
$i = 0;
$file = fopen($file_name, 'r');
while (($line = fgetcsv($file)) !== FALSE) {
if( $i==0 ) {
$header = $line;
} else {
$data[] = $line;
}
$i++;
}
fclose($file);
foreach ($data as $key => $_value) {
$new_item = array();
foreach ($_value as $key => $value) {
$new_item[ $header[$key] ] =$value;
}
$_data[] = $new_item;
}
return $_data;
}
$csvFile = 'livros.csv';
$csv = csv_to_array($csvFile);
foreach ($csv as $key => $livro){
$check = Livros::where('slug', 'baixar-livro-'.str_slug($livro['titulo']).'-pdf')->where('autor_slug', str_slug($livro['autor']))->count();
if($check <= 0){
$numbercode = mt_rand(0, 22);
$titlechecker = Livros::where('slug', 'baixar-livro-'.str_slug($livro['titulo']).'-pdf')->count();
$slugImagem = str_slug($livro['titulo']);
$imgExtensao = pathinfo($livro['imagem'], PATHINFO_EXTENSION);
$ignoreError = stream_context_create(array(
'http' => array('ignore_errors' => true),
));
if($imgExtensao !== ""){
if($titlechecker <= 0){
file_put_contents('capas/07/download-'.$slugImagem.'-ler-online-pdf.'.$imgExtensao, file_get_contents(stripslashes($livro['imagem']), false, $ignoreError));
}else{
file_put_contents('capas/07/download-'.$slugImagem.'-ler-online-pdf-'.$numbercode.'.'.$imgExtensao, file_get_contents(stripslashes($livro['imagem']), false, $ignoreError));
}
}
$novo = new Livros;
$novo->titulo = $livro['titulo'];
if($titlechecker <= 0){
$novo->slug = 'baixar-livro-'.str_slug($livro['titulo']).'-pdf';
}else{
$novo->slug = 'baixar-livro-'.str_slug($livro['titulo']).'-'.$numbercode.'-pdf';
}
$novo->autor = $livro['autor'];
$novo->autor_slug = str_slug($livro['autor']);
$novo->categoria = $livro['categoria'];
$novo->link_afiliado = $livro['link_afiliado'];
$novo->sinopse = $livro['descricao_livro'];
$novo->asin = $livro['asin'];
if($titlechecker <= 0){
$novo->imagem = '/capas/07/download-'.$slugImagem.'-ler-online-pdf.'.$imgExtensao;
}else{
$novo->imagem = '/capas/07/download-'.$slugImagem.'-ler-online-pdf-'.$numbercode.'.'.$imgExtensao;
}
$novo->save();
$url = "<url>
<loc><![CDATA[". route('livro', str_slug($livro['titulo'])) ."]]></loc>
<lastmod><![CDATA[". Carbon::now()->format('Y-m-d') ."]]></lastmod>
<changefreq><![CDATA[weekly]]></changefreq>
<priority><![CDATA[0.7]]></priority>
<image:image>
<image:loc><![CDATA[https://www.saraivaconteudo.com.br". $novo->imagem ."]]></image:loc>
<image:title><![CDATA[".$livro['titulo']." pdf]]></image:title>
</image:image>
</url>";
//Save string to log, use FILE_APPEND to append.
file_put_contents('./sitemaps/post-sitemap07.xml', $url, FILE_APPEND);
if ($key === array_key_last($csv)) {
file_put_contents('./sitemaps/post-sitemap07.xml', '</urlset>', FILE_APPEND);
}
}
}
}
}
I'm connecting using my vps IP, and database data, like this:
DB_CONNECTION=mysql
DB_HOST=162.240.48.70
DB_PORT=3306
DB_DATABASE=wwsara_a
DB_USERNAME=wwsara_u
DB_PASSWORD=********
Please or to participate in this conversation.