php single curl works but multi curl doesn't work?

后端 未结 2 1698
野的像风
野的像风 2020-12-11 18:54

So I was using ampps and then switched to z-wamp thinking it would solve the issue, but it didn\'t.

I have separate \"sites\" in my localhost (localhost/site1 &

相关标签:
2条回答
  • 2020-12-11 19:15

    I had tried running both singe curl_exec() and curl_multi_exec() on the same page and got no response as well, until I did:unset($ch);, after the single request.

    0 讨论(0)
  • I just answered another question that I found about multiple curl calls.

    This is all I did to run the requests.

    do {
        $status = curl_multi_exec($mh, $running);
    } while ($status === CURLM_CALL_MULTI_PERFORM || $running);
    

    Then I fetched the info I needed by looping over my array of curl handlers.

    $returned = array();
    foreach ($requests as $identifier => $request) {
        $returned[$identifier] = curl_multi_getcontent($request);
        curl_multi_remove_handle($mh, $request);
        curl_close($request);
    }
    

    The above approach worked for me, however it seems like you want to add only a certain amount of sessions to the curl multi-handler. We could probably change the do-while loop above to the following:

    do {
        $status = curl_multi_exec($mh, $running);
        if ($running < $window) {
            for ($x = 0; $x < $window - $running; $x++) {
                $index = count($site_map) + $x -1;
                curl_multi_add_handle($mh, $this->sessions[$index]);
                $site_map[(string) $this->sessions[$index]] = $index;
            }
        }
    } while ($status === CURLM_CALL_MULTI_PERFORM || $running);
    

    After that we could modify the data fetch and replace your whole while ($running && $execrun === CURLM_OK){} section with the following since it will only run once all your curl calls have been processed:

    $returned = array();
    foreach ($this->sessions as $identifier => $request) {
        $returned[$identifier] = curl_multi_getcontent($request);
        curl_multi_remove_handle($mh, $request);
        curl_close($request);
    }
    
    0 讨论(0)
提交回复
热议问题