Run url in background in php ← (PHP)

Solution:

Well this depends on what you mean with background I'm asuming however that you mean that the user won't be redirected to that page.

If I were you I'd go with cURL if you have it installed, since the only thing you seem to want to do is make an ordinary request, and maybe, read the response. The code below is untested but should give you a hint.

$req = curl_init();
curl_setopt($req, CURLOPT_URL,"theaddress_and_params");
curl_exec($req);

Answer



Solution:

public function get_url($url)
{
    $cmd  = "curl --max-time 60 ";
    $cmd .= "'" . $url . "'";
    $cmd .= " > /dev/null 2>&1 &";
    exec($cmd, $output, $exit);
    return $exit == 0;
}

it will call curl via cli. it will run in background.

Answer



Solution:

If you did that you would be exposing usernames and passwords in the URL (or headers). Have the user login in advance and use a session variable.

Answer



Solution:

Don't send this from the client side since every user would easily be able to "fake" the data by just loading your URL with some (potentially malicious) parameters. "username" and "password" are not protected at all and I'm sure your service would be down very quickly.

Instead, you could easily do this in the background (server-side) with PHPs curl functions:

http://www.php.net/manual/en/curl.examples-basic.php

Answer



Solution:

$url = 'http://yoursmsgateway.com/WebSMS/SMSAPI.jsp?username='.$smsuser.'&password='.$smspwd.'&sendername='.$smssender.'&mobileno='.$number.'&message='.urlencode($message);

echo $url;

   $mystring = get_data($url);

//echo "hi!";

echo $mystring;

function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

You can try this code, But you have to install cUrl DLL file. Process to install CURL is given below:--

1.)open php.ini file

2.)Find this dll file---> ;extension=php_curl.dll

3.)Remove ; (semicolon)

4.)such as---> extension=php_curl.dll

5.)Save it (Ctrl+s)

Answer



Solution:

you could fork a child-process with the pcntl php extension.

(library that implements this: https://github.com/kriswallsmith/spork)

Answer



Solution:

Make an AJAX call as the user hits the submit button. This would cause the script at that URL to run in the background while your current PHP inserts the record in the database.

Answer



Solution:

Ajax? Load that url inside a div with ajax while you save the record calling another php file with ajax.

Answer



Solution:

I think that there is no such concept as multithreading (which in essence is what you are asking for), as everything in a PHP code runs incrementally, but you can get to a solution. See this and this questions and their answers.

The reason that there is no multithreading in PHP is because everything is processed in the server, and you, as a client, already receive a finished response, so "running on background" in PHP is the same as "running sequentially".

Source