0

I have been facing this problem that i have 4 apis to process and get output to final web page. When i does this my web page loading time is more than one minute. After long thinking i figured out that i was calling then synchronously. I need to call then asynchronously to avoid lag. Then i ended up with the code here which isn't returning any data on web page . Help me out! . I got this code from http://php.net/manual/en/function.curl-multi-exec.php

    // set URL and other appropriate options
    curl_setopt($ch1, CURLOPT_URL, "http://example1.com");
    curl_setopt($ch1, CURLOPT_POST, 1);
    curl_setopt($ch1, CURLOPT_POSTFIELDS,"x=a&y=b");
    curl_setopt($ch1, CURLOPT_HEADER, 0);
    curl_setopt($ch2, CURLOPT_URL, "http://www.example2.com"."m=c&n=d");
    curl_setopt($ch2, CURLOPT_HEADER, 0);
    curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);

    //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);

        echo $mrc;


    ?>

I have 4 apis , of which 2 are POST requests and 2 are GET. Thanks in advance

abdul riyaz
  • 80
  • 1
  • 1
  • 10
  • 1
    Possible duplicate of [Non-blocking HTTP requests in object-oriented PHP?](http://stackoverflow.com/questions/1463652/non-blocking-http-requests-in-object-oriented-php) – SOFe Mar 10 '16 at 16:41
  • Try Guzzle if you want to do async requests. [https://github.com/guzzle/guzzle] – Ravindra Bhalothia Mar 10 '16 at 16:46

0 Answers0