1

I am trying to integrate with a third party API using REST. I want to make a few calls to the API without waiting for the result to be back (The calls are action calls ie they perform some operation in the third party software and hence I don't need to wait in Drupal).

In short I want to make a non blocking http call in php. (similar to asynchronous call in java-script.)

Please let me know if this is possible.

Note : I know of httprl module. But it is not of much use as I do not want to make calls in parallel. I rather want to make a non-blocking call and then carry on with my Drupal specific actions.

3 Answers 3

5

No, that's not possible with drupal_http_request().

You'll need to use httprl, curl_multi, or something like that.

2
  • as I mentioned I am not making multiple requests in parallel. Do you think even when making a single call with httprl I will be able to make a non blocking http call?
    – Gokul N K
    Commented Apr 17, 2014 at 12:34
  • 2
    You'd have to try it - bear in mind that PHP does not have the concept of asynchronous requests, though. You might be able to find an extension that provides it, but I've yet to come across one
    – Clive
    Commented Apr 17, 2014 at 12:44
3

Yes, you can use the Queue API

e.g hook_cron_queue_info

Basically add the item to a queue to be processed later via cron.

Note, just on mobile at the moment but can update with more details later.

8
  • Deferral != Non-blocking
    – Clive
    Commented Apr 17, 2014 at 12:36
  • It's non blocking in that the request happens asynchronously in a separate process. Add the item to the queue and continue the current page request. No need to wait for remote http response. Commented Apr 17, 2014 at 12:38
  • 1
    @DavidThomas Thanks for the answer. I know of Queue API, but that wouldn't suit now as those actions needs to happen in real time and cannot wait for the next cron. What I am looking for is php equivalent of ajax call.
    – Gokul N K
    Commented Apr 17, 2014 at 12:49
  • 1
    @GokulNK Exactly my point - this method is neither non-blocking nor async in the true sense of either word...but, it could potentially work if you have something like Elysia cron installed, you're pinging it every second, and you have a job set up to process this queue as often as physically possible. It might be the best solution out there for what you need to do
    – Clive
    Commented Apr 17, 2014 at 12:49
  • 1
    @DavidThomas Yeah, definitely worth a mention (will probably end up being the only way the OP can do it). I'm pointing out the shortcomings in PHP, not in your answer as such
    – Clive
    Commented Apr 17, 2014 at 12:52
2

Seems like Background Process module address this problem..

Project page clearly says

Background process way: non-blocking/a-synchronous (limited to 5 concurrent connections)

<?php
$r = array();
for ($i = 0; $i < 10; $i++) {
  $r[] = background_process_http_request('http://www.example.com/stuff/' . $i, array('postpone' => TRUE));
}
background_process_http_request_process($r, array('limit' => 5));
print_r($r);
?>
2
  • 1
    That module is limited by what PHP can physically do - it can only wrap around curl_multi or httprl, or do something very, very similar
    – Clive
    Commented Apr 17, 2014 at 12:58
  • Agree with @Clive
    – Gokul N K
    Commented Apr 17, 2014 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.