Mysql
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Mysql

Làm cách nào để sử dụng cURL để mở nhiều URL đồng thời với PHP?

Bạn thiết lập từng trình xử lý cURL theo cùng một cách, sau đó thêm chúng vào curl_multi_ xử lý. Các hàm cần xem là curl_multi_* các hàm được ghi lại ở đây . Tuy nhiên, theo kinh nghiệm của tôi, đã xảy ra sự cố khi cố gắng tải quá nhiều URL cùng một lúc (mặc dù tôi không thể tìm thấy ghi chú của mình trên đó vào lúc này), vì vậy, lần cuối cùng tôi đã sử dụng curl_mutli_ , Tôi thiết lập nó để thực hiện hàng loạt 5 URL cùng một lúc.

chỉnh sửa :Đây là phiên bản rút gọn của mã mà tôi sử dụng curl_multi_ :

chỉnh sửa :Được viết lại một chút và nhiều nhận xét được bổ sung, hy vọng sẽ hữu ích.

// -- create all the individual cURL handles and set their options
$curl_handles = array();
foreach ($urls as $url) {
    $curl_handles[$url] = curl_init();
    curl_setopt($curl_handles[$url], CURLOPT_URL, $url);
    // set other curl options here
}

// -- start going through the cURL handles and running them
$curl_multi_handle = curl_multi_init();

$i = 0; // count where we are in the list so we can break up the runs into smaller blocks
$block = array(); // to accumulate the curl_handles for each group we'll run simultaneously

foreach ($curl_handles as $a_curl_handle) {
    $i++; // increment the position-counter

    // add the handle to the curl_multi_handle and to our tracking "block"
    curl_multi_add_handle($curl_multi_handle, $a_curl_handle);
    $block[] = $a_curl_handle;

    // -- check to see if we've got a "full block" to run or if we're at the end of out list of handles
    if (($i % BLOCK_SIZE == 0) or ($i == count($curl_handles))) {
        // -- run the block

        $running = NULL;
        do {
            // track the previous loop's number of handles still running so we can tell if it changes
            $running_before = $running;

            // run the block or check on the running block and get the number of sites still running in $running
            curl_multi_exec($curl_multi_handle, $running);

            // if the number of sites still running changed, print out a message with the number of sites that are still running.
            if ($running != $running_before) {
                echo("Waiting for $running sites to finish...\n");
            }
        } while ($running > 0);

        // -- once the number still running is 0, curl_multi_ is done, so check the results
        foreach ($block as $handle) {
            // HTTP response code
            $code = curl_getinfo($handle,  CURLINFO_HTTP_CODE);

            // cURL error number
            $curl_errno = curl_errno($handle);

            // cURL error message
            $curl_error = curl_error($handle);

            // output if there was an error
            if ($curl_error) {
                echo("    *** cURL error: ($curl_errno) $curl_error\n");
            }

            // remove the (used) handle from the curl_multi_handle
            curl_multi_remove_handle($curl_multi_handle, $handle);
        }

        // reset the block to empty, since we've run its curl_handles
        $block = array();
    }
}

// close the curl_multi_handle once we're done
curl_multi_close($curl_multi_handle);

Cho rằng bạn không cần bất kỳ thứ gì trở lại từ các URL, có thể bạn không cần nhiều thứ ở đó, nhưng đây là cách tôi chia nhỏ các yêu cầu thành các khối BLOCK_SIZE , đợi từng khối chạy trước khi tiếp tục và gặp lỗi từ cURL.




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Chèn một danh sách đơn giản vào bảng MySQL (php)

  2. Mất độ chính xác trong khi chèn double vào MySQL thông qua PDO

  3. Hộp thả xuống PHP MySQL Điền giá trị đã chọn

  4. Nếu mysql_num_rows bằng ZERO thì KHÔNG hoạt động

  5. Lỗi xây dựng chương trình với trình kết nối mysql