Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jun 2012
    Location
    Near Chicago, USA
    Posts
    123
    Thanks
    7
    Thanked 19 Times in 19 Posts

    Asynchronous HTTP Requests

    Is there a way to create multiple HTTP connections with PHP and handle the request responses in whatever order they are received?

    Sure would appreciate pointers or links to tutorials.

    Thanks!

    Will
    Numerology API for apps - Facebook, iPad, mobile phones. No charge to use API. [info]

  • #2
    New Coder
    Join Date
    Jul 2011
    Location
    USA
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post
    You should be able to do what you want with curl, with something like this. This is only set to execute two urls but can be modified to what you want.

    PHP Code:
    <?php

    // create both cURL resources
    $ch1 curl_init();
    $ch2 curl_init();

    // set URL and other appropriate options
    curl_setopt($ch1CURLOPT_URL"http://url_one.html");
    curl_setopt($ch1CURLOPT_HEADER0);

    curl_setopt($ch2CURLOPT_URL"http://url_two.html");
    curl_setopt($ch2CURLOPT_HEADER0);

    //create the multiple cURL handle
    $mh curl_multi_init();

    //add the two handles
    curl_multi_add_handle($mh,$ch1);
    curl_multi_add_handle($mh,$ch2);
    $active null;

    //execute the handles
    do {
        
    $mrc curl_multi_exec($mh$active);
    } while (
    $mrc == CURLM_CALL_MULTI_PERFORM);
    while (
    $active && $mrc == CURLM_OK) {
        if (
    curl_multi_select($mh) != -1) {
            do {
                
    $mrc curl_multi_exec($mh$active);
            } while (
    $mrc == CURLM_CALL_MULTI_PERFORM);
        }
    }

    //close the handles
    curl_multi_remove_handle($mh$ch1);
    curl_multi_remove_handle($mh$ch2);
    curl_multi_close($mh);
    ?>

  • Users who have thanked byrondallas for this post:

    Will Bontrager (08-20-2012)

  • #3
    Regular Coder
    Join Date
    Jun 2012
    Location
    Near Chicago, USA
    Posts
    123
    Thanks
    7
    Thanked 19 Times in 19 Posts
    Exactly what I was looking for, a place to start.

    Thank you very much, byrondallas.

    Will
    Numerology API for apps - Facebook, iPad, mobile phones. No charge to use API. [info]

  • #4
    New Coder
    Join Date
    Jul 2011
    Location
    USA
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post
    Exactly what kind of responses were you hoping to to get from the urls? Maybe I or somebody else can show you how using curl if your not already familiar with curl.

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,358
    Thanks
    61
    Thanked 530 Times in 517 Posts
    Just to add, using multiple cURL connections is actually a very good way of getting around the multiple thread / fork issue on both windows and linux. I've had to do this myself and it works a treat. It will use more resources but it works very well if you're calling scripts on the localhost multiple times to execute tasks at the same time. Minimal fuss with threads, and minimal fuss with forking. You may however want to password protect those 'task scripts' and post the password to them so that external users can't abuse them etc.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    Will Bontrager (08-21-2012)

  • #6
    Regular Coder
    Join Date
    Jun 2012
    Location
    Near Chicago, USA
    Posts
    123
    Thanks
    7
    Thanked 19 Times in 19 Posts
    Quote Originally Posted by byrondallas View Post
    Exactly what kind of responses were you hoping to to get from the urls? Maybe I or somebody else can show you how using curl if your not already familiar with curl.
    Right, I have never used curl before. Knowing which functions to focus on first during my research would save me a lot of time.

    What I'm doing is grabbing a bunch of RSS feeds (all http://... URLs). Request one, wait for response, process. Request next one, wait for response, process. etc.

    My idea when I started this thread was to request additional RSS feeds while waiting for response from other requests.

    Currently, it's taking a minute or more to grab and process the list. The list of feeds will only get larger. I considered grabbing the feeds in chunks of 10 and then relaunch the script for the next 10 but, well, it's just not an elegant solution.

    I appreciate your time.

    Will
    Numerology API for apps - Facebook, iPad, mobile phones. No charge to use API. [info]

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,358
    Thanks
    61
    Thanked 530 Times in 517 Posts
    a
    Quote Originally Posted by Will Bontrager View Post
    Right, I have never used curl before. Knowing which functions to focus on first during my research would save me a lot of time.
    Until my first time I had also never used cURL before - somehow I managed

    If you want to know about the cURL functions and how to use them then you will almost certainly find this link useful: http://uk.php.net/curl

    Remember that checking out the user comments on each functions page always reveals some good code that you can use too.

    with regards to your multiple feeds then yes using multiple curl handles / execs would be a good way to go as the processeing can be done in parallel like a threaded system.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #8
    Regular Coder
    Join Date
    Jun 2012
    Location
    Near Chicago, USA
    Posts
    123
    Thanks
    7
    Thanked 19 Times in 19 Posts
    byrondallas and tangoforce and anyone else who may be following this thread, this seems to do a good job. Although I don't have a lot to compare it to.

    http://code.google.com/p/rolling-cur...wse/#svn/trunk

    It works. And it is well commented for those who, like me, enjoy learning how things are done.

    Will
    Numerology API for apps - Facebook, iPad, mobile phones. No charge to use API. [info]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •