4

I have this situation...
I'm posting some variables using CURL to a remote host. Below you can see how my PHP scripts, look like:
Local PHP script:

$url = 'http://somesite.com/something.php';
data = array ('key1' => 'string1',
          'key2' => 'string2',
          'key3' => 'string3');
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
curl_exec ($ch);
curl_close ($ch);
// Some code to do other stuff.

Remote PHP script:

$string1 = $_POST['key1'];
// If this string exists in a very big folder of text files.
sleep(30);
// Do something with the file that contain the string.

My problem is that the local php script is loading until the remote script finish the execution. My question is, is there any way to POST those variables to that remote host and then just continue the script execution ? Not mandatory using CURL, but I would like a PHP solution. Thanks!

David M
  • 43
  • 5
  • 1
    Duplicate - http://stackoverflow.com/questions/26039848/php-asynchronous-curl-with-callback – adnan kamili Apr 29 '15 at 09:50
  • That's not how PHP works by default. You'll have to look into process forking or use another server side language like node.js which is asynchronous – scrowler Apr 29 '15 at 09:55
  • An example of forking process: CURL requests a page, that page does some stuff and outputs a result, that page forks itself to continue doing other stuff, that page stops executing, the forked process continues in the background unattended until it finishes – scrowler Apr 29 '15 at 09:57
  • Ok, I think I didn't put my question properly. What i mean is, is there any way to POST those variables to that remote host and then just continue the script execution ? Not mandatory using CURL, but I would like a PHP solution. I'll also update my question now. Thank you! – David M Apr 29 '15 at 10:22
  • You can open a socket connection to the host, write on that socket and close it immediately. That will just send the data and won't wait for the response so you're able to continue with the rest of the script. – N.B. Apr 29 '15 at 10:38
  • @DavidM try my answer. it will work. dunno why downvoted. – mirza Apr 29 '15 at 10:47
  • @N.B. , I'll appreciate if you could show me an example. – David M Apr 29 '15 at 11:01
  • 1
    [Here's an answer](http://stackoverflow.com/questions/962915/how-do-i-make-an-asynchronous-get-request-in-php) that depicts exactly what I was referring to. – N.B. Apr 29 '15 at 11:07
  • @N.B. Thanks so much, indeed that answer is exactly what I need. +1 – David M Apr 29 '15 at 13:39
  • I'm glad it helped, don't forget to upvote the guy who answered it though :) – N.B. Apr 29 '15 at 13:40

1 Answers1

0

Add timeout to your code:

curl_setopt($ch, CURLOPT_TIMEOUT, 1);

This will continue to execute the rest part of your code after 1 second.

CURLOPT_TIMEOUT The maximum number of seconds to allow cURL functions to execute.

Or you can use it for miliseconds:

CURLOPT_TIMEOUT_MS The maximum number of milliseconds to allow cURL functions to execute. If libcurl is built to use the standard system name resolver, that portion of the connect will still use full-second resolution for timeouts with a minimum timeout allowed of one second.

You can read further explanation from here.

Once the other script will start it will continue without have to waiting finish output.

mirza
  • 5,685
  • 10
  • 43
  • 73
  • I'm not sure how downvote works on StackOverFlow, as this is my first time using it, but I didn't downvoted your answer. Maybe someone else. I'll try your solution and I'll come back with feedback. – David M Apr 29 '15 at 10:53
  • 2
    I think the explanation for the downvote (which I didn't cast) is that the curl operation will abort/terminate after 1 second. It will not make curl call async, it'll still wait for one second and terminate the operation. http://curl.haxx.se/libcurl/c/CURLOPT_TIMEOUT.html – Ejaz Apr 29 '15 at 11:13
  • @Ejay yes but the original code is waiting 30 seconds. 1 second wait time is acceptable in many cases and there is an option TIMEOUT_MS which makes possible to less waiting time. – mirza Apr 29 '15 at 11:24
  • @motto Thank you! I have tried and it seems to work. I don't mind waiting a second but what will happen if (for any reason) the remote host takes more than a second to receive my POST variables ? – David M Apr 29 '15 at 11:33
  • So you're suggesting that if original request takes 30 seconds to complete then it should hold the script as much time after that. Keep in mind that OP's problem is not just continuing after the curl operation, it is to continue immediately. OP's code is already waiting for curl to **complete** and it **has to** complete (not abort) in all cases so aborting the request isn't an option. "It'll not abort"? then why put a timeout there. – Ejaz Apr 29 '15 at 11:37
  • @Ejay op seemed ok with the solution. he didn't mentioned that he needs immediately continue. i am not sure what is the problem. – mirza Apr 29 '15 at 11:40
  • @DavidM you can add a db check or file create function to verify your post request works fine. in your first code from the beginning // delete status check file if it is exists then make the post request. and in your second file check post variables exists then if they are there create status check file. then again in your first file after the post request check file exists if it's there then everything is ok. if it's not wait and check again or make the post again with in a loop and if you think my answer is useful for you please consider accepting answer and/or upvoting by clicking arrow. – mirza Apr 29 '15 at 11:45
  • 1
    @Ejay Please note that I modified my post. I actually do not need the script to continue immediately (1-2 sec is ok) but you're right that I don't want to abort the request (at least not until my variables are posted). Anyway, I'm not using this on a production environment so I'll upvote the motto's answer because his answer is actually doing what I need, at least for now. I'm looking forward for better solutions, with examples. Thanks everyone! – David M Apr 29 '15 at 12:21
  • 1
    @motto I was trying to up vote your answer, but it seems that I need to have at least 15 reputation... – David M Apr 29 '15 at 12:23
  • @DavidM I'll add a detailed example in few hours when i've time. btw your reputation is 16. – mirza Apr 29 '15 at 12:57