0

In part of my PHP application, I need to make an REST style API call and continue working. I don't care about the result of the API call (so I don't need to see the return value) but I do need to be able to just fire it off and continue working.

If I use file_get_contents() to do this, then it will wait until the url returns. I can set a timeout in CURL but the smallest timeout it allows is one second so this is not very useful. I have looked for other options in CURL but I haven't found anything yet.

Since I have written this part of the API that is being hit I am currently having it just return immediately and continue working in that code but in the future if I talk to other external APIs then I will need to know how to do this and it just seems like better practice not to depend on some external code to have your script continue working. It probably isn't very normal to request a URL and not care about what comes back but this is what I need! Anyone know how?

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
hackartist
  • 5,172
  • 4
  • 33
  • 48

4 Answers4

4

Just use curl_multi_exec(), and kill the connection once response headers are returned.

This assumes that response headers are sent before content begins processing, and that the remote script will continue to run once you disconnect.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • hmm, I hadn't thought about what would happen if I didn't hold the connection open and that the external API needs to still be connected to work. It sounds like this means I should use a fork then to keep it going but your answer is what I asked for so I will accept. Thanks. – hackartist Jun 20 '12 at 06:14
  • Some web services are built in such a way that they keep running when the client disconnects. In PHP, this is configurable. http://php.net/manual/en/function.ignore-user-abort.php – Brad Jun 20 '12 at 14:18
2

Sounds like you might want to consider moving the call to the api to a background job.

You may want to consider php resque, https://github.com/chrisboulton/php-resque

or roll your own with a db table and a cron job.

Chris Mohr
  • 3,639
  • 1
  • 13
  • 9
0

If you can try from the client side, may be this can help!

function loadNow()
{
    $.load('cURL URL');
}
setInterval('loadNow()', 500);

Hope this helps! :)

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • I don't know, @TheLQ, I'm more concerned with the `eval()`'d function call than the odds someone might not have Javascript enabled, or the fact we're talking about on the server itself. If you need Javascript to use the site at all, this is a moot point, in addition to the mooting due to server-side. But the interval should be `setInterval(loadNow, 500)` – Jared Farrish Jun 20 '12 at 05:44
  • @JaredFarrish the function name should be inside `''`. See http://www.w3schools.com/jsref/met_win_setinterval.asp – Praveen Kumar Purushothaman Jun 20 '12 at 06:11
  • 1
    Oh no, trust me. You *can* but it `eval`s the string, which [you don't need or want to do](http://bonsaiden.github.com/JavaScript-Garden/#core.eval). To quote: *The use of eval should be avoided at all costs. 99.9% of its "uses" can be achieved without it.* Use a function identifier like it's name or an anonymous function. Also, avoid w3schools. See http://w3fools.com. – Jared Farrish Jun 20 '12 at 06:16
0

If your output is HTML you may want to consider moving the call into an iframe.

Rid Iculous
  • 3,696
  • 3
  • 23
  • 28