簡體   English   中英

如何使用cURL將GET數據同時發送到多個URL?

[英]How can I send GET data to multiple URLs at the same time using cURL?

我道歉,我實際上多次問過這個問題,但從來沒有完全理解答案。

這是我目前的代碼:

while($resultSet = mysql_fetch_array($SQL)){            
$ch = curl_init($resultSet['url'] . $fullcurl); //load the urls and send GET data
            curl_setopt($ch, CURLOPT_TIMEOUT, 2);           //Only load it for two seconds (Long enough to send the data)
            curl_exec($ch);                                 //Execute the cURL
            curl_close($ch);                                //Close it off 
} //end while loop

我在這里做的是從MySQL數據庫($ resultSet ['url'])獲取URL,向它添加一些額外的變量,只是一些GET數據($ fullcurl),並且只是請求頁面。 這將啟動在這些頁面上運行的腳本,而這個腳本需要做的就是啟動這些腳本。 它不需要返回任何輸出。 只需加載頁面足夠長的腳本即可啟動。

但是,目前它一次只加載一個URL(目前為11個)。 我需要同時加載所有這些。 我知道我需要使用curl_multi_ ,但我對cURL函數的工作方式一無所知,所以我不知道如何更改我的代碼以在while循環中使用curl_multi_

所以我的問題是:

如何更改此代碼以同時加載所有URL? 請解釋一下,而不僅僅是給我代碼。 我想知道每個函數究竟做了什么。 curl_multi_exec是否會在while循環中工作,因為while循環只是一次發送一行?

當然,任何有關cURL函數的參考資料,指南和教程都會很好。 最好不要來自php.net,因為雖然它能很好地給我語法,但它只是有點干,而且解釋不太好。

編輯:好的zaf,這是我目前的代碼:

        $mh = curl_multi_init(); //set up a cURL multiple execution handle

$SQL = mysql_query("SELECT url FROM urls") or die(mysql_error()); //Query the shell table
                    while($resultSet = mysql_fetch_array($SQL)){   

        $ch = curl_init($resultSet['url'] . $fullcurl); //load the urls and send GET data
        curl_setopt($ch, CURLOPT_TIMEOUT, 2);           //Only load it for two seconds (Long enough to send the data)
        curl_multi_add_handle($mh, $ch);
    } //No more shells, close the while loop

        curl_multi_exec($mh);                           //Execute the multi execution
        curl_multi_close($mh);                          //Close it when it's finished.

在while循環中,您需要為每個URL執行以下操作:

  • 使用curl_init()創建curl資源
  • 通過curl_setopt(..)設置資源選項

然后你需要使用curl_multi_init()創建一個多個curl句柄,並使用curl_multi_add_handle(...)添加所有以前的單個curl資源

最后你可以做curl_multi_exec(...)。

這里有一個很好的例子: http//us.php.net/manual/en/function.curl-multi-exec.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM