Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

702
Visualizações
execute PHP AJAX call using cron jobs

I created an HTML page for my client so that every time he goes there, a script is executed to update some data in the database. As soon as he lands on that page, there is an AJAX call made to a Google API, some results are returned and then some PHP scripts are executed to update and insert data in a database. For instance, if he goes to http://www.example.com/updateScript.html, all of the above happens.

Instead of he having to go there, I want to schedule a daily task using cron job. I don't want to write all of my logic in PHP because the JavaScript part is kind of complex. Is there any way for a cron job to open that HTML page so that my script is executed?

I checked a few questions in SO, like the following one: Using cron jobs to visit url? but this didn't work for me.

My cron job command is /usr/bin/wget https://www.example.com/updateScript.html. I also tried changing the udpateScript.html to PHP but it didn't work. Any suggestions of what I could do?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Sadly, cron cannot execute javascript on the page. However, since you said you need suggestion, you might want to look at PHP's cURL. You can access other website in PHP using cURL. There are lots of protocols you can do with it and the one you need is GET. An example would be on this site.

// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://testcURL.com/?item1=value&item2=value2',
    CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);

Also, since your website you want to access contains javascript, you might want to mimic how your browser request to your server like this or this.

I hope you got some idea, good luck with your project.

over 4 years ago · Santiago Trujillo Relatório

0

The the crons are not working for you because you need JavaScript to be executed, however, the cron simply pulls the code bur doesn't execute it.

If you care to use JavaScript to as a processor, you could consider using Phantom.js: http://phantomjs.org/

Here is an example of how its done: https://github.com/ariya/phantomjs/blob/master/examples/waitfor.js

over 4 years ago · Santiago Trujillo Relatório

0

Wget won't execute the JavaScript on the page for you: it is something that has to be done in the browser (or whatever can act as such on the server). So for you it's either rewriting the logic in PHP (which might be very painful, but very benefecial in the long term) or try and set up something on the server as per the suggestions here.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda