Curl - PHP; How to make a looping in code?

Asked

Viewed 446 times

4

I need that at the end of the code it deletes the downloads that were done and download again, which is repeated every 4 min. Every 4 min it runs this script of "download, delete and download again".
How to do it?! I thank from my heart to those who help, this code has already been very laborious.

The code it:

  • Access the site
  • Find the files . CSV and download everything to the downloads folder that is next to the script
<?php

$x = new Nulogy();
$retorno = $x->iniciar();
echo $retorno;

class Nulogy{

public $u;
public $s;
public $url;
public $post;
public $resposta;
public $cookie;
public $token;
public $urlDownload;
public $dir;
public $nome;
public $ds;
public $arquivo;
public function __construct(){

  $this->u = 'login aqui';
  $this->s = 'senha aqui';
  $this->dir = dirname(__FILE__).DIRECTORY_SEPARATOR.'download'.DIRECTORY_SEPARATOR;
  $this->cookie = dirname(__FILE__).DIRECTORY_SEPARATOR. '/cookie/' .'NULOGY[COOKIE]' . rand(0, 999999) . '.txt';
}
public function iniciar(){
  $this->url = 'https://packmanager.nulogy.net/sign_in';
  $this->loginPlayLoad();
  $this->post();
  $validarLogin = $this->validarLogin();
  if($validarLogin === 'logado'){
  return $this->linkDownload();
  }else{
    return $validarLogin;
  }
}
public function loginPlayLoad(){
  $this->getTokenLogin();
  if($this->token){
    $data['utf8'] = '✓';
    $data['authenticity_token'] = $this->token;
    $data['return'] = '';
    $data['uv_login'] = '';
    $data['user[login]'] = $this->u;
    $data['user[password']= $this->s;
    $data['commit'] = 'Sign in';
    $data['authenticity_token'] = $this->token;
    $data['authenticity_token'] =$this->token;
  }else{
    return false;
  }
  $this->post = http_build_query($data);
  return true;
}
public function getTokenLogin(){
  $this->url = 'https://packmanager.nulogy.net/sign_in';
  $this->get();
  preg_match_all('/meta content="(.*?)" name="csrf-token"/', $this->resposta, $matches, PREG_SET_ORDER, 0);
  $token = $matches[0][1];
  if($token){
    return $this->token = $token;
  }else{
    return array('status' => false,'resposta' => 'Não foi possível encontrar CSRF-TOKEN' );
  }
  return false;
}
public function validarLogin(){
  if(preg_match_all('/title>(.*?)<.title>/', $this->resposta, $matches, PREG_SET_ORDER, 0)){
    $titlo = $matches[0][1];
  }else{
  $titlo = false;
  }

  if((isset($titlo) && ($titlo === 'Sign in to PackManager') )) {
    return $titlo . ' Login ou senha inválidos.';
  }else{
    return 'logado';
  }
}
public function linkDownload(){
  $url = [
  'https://packmanager.nulogy.net/canned_reports/job_productivity?utf8=%E2%9C%93&generate_flag=true&filter_choice=project_customer&query%5B0%5D%5Bactual_job_start_at%5D%5Boperator%5D=between&query%5B0%5D%5Bactual_job_start_at%5D%5Bfrom_threshold%5D=2017-Jul-2+9%3A00+PM&query%5B0%5D%5Bactual_job_start_at%5D%5Bto_threshold%5D=2017-Jul-3+11%3A46+PM&columns%5B0%5D=project_code&columns%5B1%5D=item_code&columns%5B2%5D=actual_job_start_at&columns%5B3%5D=actual_job_end_at&columns%5B4%5D=line_name&columns%5B5%5D=performance&columns%5B6%5D=availability&columns%5B7%5D=line_efficiency&columns%5B8%5D=units_produced&columns%5B9%5D=units_expected&columns%5B10%5D=unit_of_measure&columns%5B11%5D=standard_units_per_hour&columns%5B12%5D=duration&order_by=actual_job_start_at&order_direction=desc&authenticity_token='.urlencode($this->token).'&authenticity_token='.urlencode($this->token).'&format=csv',
  'https://packmanager.nulogy.net/jobs.csv?authenticity_token='.urlencode($this->token).'&customer_name=&from_scheduled_start_at=2017-Jul-2+00%3A00&has_production=all&item_code=&line_leader=all&line_leader_id=all&line_name=&page=1&project_code=&reconciliation_status=all&signed_off=no&status=all&to_scheduled_end_at=2017-Jul-3+23%3A59&utf8=%E2%9C%93',
  'https://packmanager.nulogy.net/canned_reports/job_downtime?utf8=%E2%9C%93&generate_flag=true&filter_choice=item_class_name&query%5B0%5D%5Bactual_job_start_at%5D%5Boperator%5D=between&query%5B0%5D%5Bactual_job_start_at%5D%5Bfrom_threshold%5D=2017-Jul-2+9%3A00+PM&query%5B0%5D%5Bactual_job_start_at%5D%5Bto_threshold%5D=2017-Jul-3+11%3A55+PM&columns%5B0%5D=job_id&columns%5B1%5D=line_name&columns%5B2%5D=downtime_reason_code&columns%5B3%5D=downtime_reason_name&columns%5B4%5D=downtime_duration&columns%5B5%5D=downtime_notes&order_by=actual_job_start_at&order_direction=desc&authenticity_token='.urlencode($this->token).'&authenticity_token='.urlencode($this->token).'&format=csv'];
  $nome = [0 => 'produtividade do trabalho.csv',1 => 'Trabalhos.csv' , 2 => 'inatividade do trabalho.csv'];
  $i = 0;

  foreach($url as $u):
    $this->urlDownload = $u;
    $this->nome = $nome[$i++];
    echo $this->dw();
  endforeach;
}
public function header(){
  $header = [
      "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
      "Accept-Language: en-US,en;q=0.5",
      "Accept-Encoding: gzip, deflate, br",
      "Connection: keep-alive",
      "Upgrade-Insecure-Requests: 1",
      "Content-Type: application/x-www-form-urlencoded"
  ];
  return $header;
}
public function dw(){
    $this->url = $this->urlDownload;
    $this->get();
    $dw = $this->resposta;
    $this->arquivo = $this->dir.$this->nome;
    if($this->arquivosExiste()){

    file_put_contents($this->arquivo , $dw);
    $ret =  'Arquivo salvo na pasta: ' . $this->dir . '<br>';
    }else{
      $ret = 'Algum problema foi encontrado :/';
    }
    return $ret;
}
public function arquivosExiste(){
  if (is_file($this->arquivo)){
      unlink($this->arquivo);
   }
  return true;
}
public function post(){
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $this->url);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $this->post);
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36');
  curl_setopt($ch, CURLOPT_REFERER, "https://packmanager.nulogy.net/sign_in");
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION,2);
  curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
  curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header());
  curl_setopt($ch, CURLOPT_ENCODING, '');
  $this->resposta = curl_exec($ch);
}
public function get(){
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $this->url);
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36');
  curl_setopt($ch, CURLOPT_REFERER, "https://packmanager.nulogy.net/sign_in");
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_HEADER, false);
  curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
  curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header());
  curl_setopt($ch, CURLOPT_ENCODING, '');
  $this->resposta = curl_exec($ch);
}
}
  • 2

    I would recommend implementing as if it were to run only once the script, but call it via scheduled task every 4 minutes. Files that will be overwritten do not even need to be deleted. And the files that were created before the start of the script (watch the time) can be deleted at the end of it.

  • 1

    Set up your cronjob to run it every 4 minutes, if each file goes to a specific user, it must run cronjob searching for all users, or you must have a specific file for each user

  • I believe the best way is to set up a cronjob in your hosting. If cPanel is even easier, you will only put the time of 4 minutes and will put the path of the php file that erases the files from the server and download again. It’s really easy to do that way. Hug!

  • Well, the best option in my view is to increase this script so that it does the whole process from start to finish, that is, it creates and deletes everything, at the end of it, it has to be as if it had not done. So you create a scheduled task, CRON JOB, and put a repeat every 4 minutes, which will do exactly what you want.

  • Another option is to run in a loop in bash (I use when the repeat loop needs to be less than 1 minute): $ while true; php script.php;;

  • There are some options like cron job from the linux server itself, or you can use queues like SQS or Redis, but if you want something simple, take a look at Reactphp, which is precisely for you to make loopings of actions: https://reactphp.org/

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.