2

In some of my PHP scripts I am using this code to POST data to a URL:

$file = @file_get_contents();

This will only POST data, content returned by server is empty. The executed script is really unimportant and isn't needed for the main script that gets executed. It's like a log file.

Normally PHP will wait until this is ready executed.

Is there a way to call $file=@file_get_contents(); without waiting for the result? Just call it and execute the next command without taking care of $file=@file_get_contents();?

I have already searched for this problem but only found solutions for the console.

Salman A
  • 262,204
  • 82
  • 430
  • 521
David
  • 2,898
  • 3
  • 21
  • 57
  • 2
    What are you trying to achieve exactly? Maybe you want something like crons...!!? – Ionel Lupu Aug 28 '18 at 07:13
  • 2
    What about https://stackoverflow.com/questions/38204936/dont-wait-for-file-get-contents-to-finish - the second answer there? – dWinder Aug 28 '18 at 07:14
  • @boyd No, CronJobs aren't what I am looking for. `$file=@file_get_contents();` will get called every time a user opens the webpage. But it's something additional and unimportant I don't want the user to have to wait. – David Aug 28 '18 at 07:16
  • @DavidWinder Looks a tittle bit complicated but I'll take a look at it. – David Aug 28 '18 at 07:19
  • 2
    if you don't want to use CronJobs then creating threads are the way to go, but they are tricky. You can also use a message broker like RabbitMQ to do that. – Ionel Lupu Aug 28 '18 at 07:21
  • 2
    If your doing it last, you could just close the connection: https://stackoverflow.com/questions/15273570/continue-processing-php-after-sending-http-response – Lawrence Cherone Aug 28 '18 at 07:21
  • 1
    here is a simply fire-and-forget POST function, so it won't wait for the response created by gr8gonzo 2012-07-18 : https://www.experts-exchange.com/questions/27795966/Asynchronous-file-get-contents.html – SL5net Aug 28 '18 at 07:22
  • @SL5net That looks good. I'll take a look at it and try it. Thanks! – David Aug 28 '18 at 07:24
  • 1
    PHP can do threading. Loading data parallel is possibility. see: http://php.net/pthreads – SL5net Aug 28 '18 at 07:29
  • 1
    What operating system are you using? – Salman A Aug 28 '18 at 07:55

3 Answers3

1

file_get_contents is a sync method so you can't just skip it but if the method is not important a solution using PHP is create a thread and put it that log logic method, in that way you can run the process where actually the request is being attended (process / thread) and at "same time" the thread logging whatever you are doing. http://php.net/manual/es/class.thread.php

Jose Mato
  • 2,709
  • 1
  • 17
  • 18
1

This feature is with file_get_contents not really possible. But you can use fsockopen to achieve this.

Frodik
  • 14,986
  • 23
  • 90
  • 141
Markus
  • 440
  • 2
  • 10
1

You can move the asynchronous code inside a separate PHP file, then execute it using one of the program execution functions. You need to spawn the program in such a way that PHP does not wait for it to finish. For example on Unix you can use the & operator:

<?php
shell_exec("php post.php arg1 arg2 arg3 >/dev/null 2>/dev/null &");

This is tricky on Windows but not impossible.

Salman A
  • 262,204
  • 82
  • 430
  • 521