Run a separated process in background to send the request. Even if you use async http request (whatever language you use), you still must wait for the request to be completed before you exit your script/program.
PHP is not Java, every time the PHP script finishes, all the resources will be destroyed, if the async http request hasn't finished, it will be canceled.
You can consider to call "nohup /path/your_script" to do some background tasks.
Hint: PHP opened files are not marked as FD_CLOEXEC, so if you have a long-run background task, you should close the inherited file descriptors first, or there will be resource leaks.
Here are some C codes I used to help to run background tasks in PHP: it closes all inherited file descriptors first, redirect stdout/stderr to /dev/null, then enter background mode (like nohup)
/*
gcc -m32 bgexec.c -o bgexec
*/
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
void close_fds()
{
char buf[256];
struct dirent *dp;
snprintf(buf, 255, "/proc/%i/fd/", getpid());
DIR *dir = opendir(buf);
while ((dp = readdir(dir)) != NULL) {
if(dp->d_name[0] && dp->d_name[0] != '.') {
//printf("fd: %s\n", dp->d_name);
close(atoi(dp->d_name));
}
}
closedir(dir);
}
int main(int argc, char *argv[])
{
int pid;
signal(SIGCLD, SIG_IGN); // no defunct when no wait();
if (argc < 2)
return fprintf(stderr, "No arguments given\n"), 1;
/* Fork it */
pid = fork();
if(pid < 0)
return fprintf(stderr, "Couldn't fork\n"), 127;
if (pid == 0) {
/* Child */
setsid();
umask ( 0 ); /* clear file mode creation mask */
close_fds();
int fd = open("/dev/null", O_RDWR);
dup2(0, 1);
dup2(0, 2);
signal(SIGHUP, SIG_IGN); // no hup
signal(SIGCLD, SIG_DFL); // keep away from 'exec' returns ECHILD
/* Execute it */
execv(argv[1], &argv[1]);
exit(127);
}
return 0;
}