Hướng dẫn php curl multiple post requests - php curl nhiều yêu cầu bài đăng

Tôi hiện đang sử dụng Curl cho PHP rất nhiều. Phải mất rất nhiều thời gian để có được kết quả khoảng 100 trang mỗi lần. Đối với mọi yêu cầu tôi đang sử dụng mã như thế này

Show
$ch = curl_init();

// get source

curl_close($ch);

Các lựa chọn của tôi để tăng tốc mọi thứ là gì?

Làm thế nào tôi nên sử dụng multi_init() vv?

Hướng dẫn php curl multiple post requests - php curl nhiều yêu cầu bài đăng

M.A.K. Ripon

1.9203 huy hiệu vàng29 Huy hiệu bạc46 Huy hiệu đồng3 gold badges29 silver badges46 bronze badges

Hỏi ngày 10 tháng 10 năm 2010 lúc 11:44Oct 10, 2010 at 11:44

1

  • Tái sử dụng cùng một xử lý Curl ($ CH) mà không cần chạy curl_close. Điều này sẽ tăng tốc nó lên chỉ một chút.
  • Sử dụng curl_multi_init để chạy các quy trình song song. Điều này có thể có một hiệu ứng to lớn.

Đã trả lời ngày 10 tháng 10 năm 2010 lúc 11:46Oct 10, 2010 at 11:46

Emil Vikströmemil VikströmEmil Vikström

88.7K15 Huy hiệu vàng134 Huy hiệu bạc170 Huy hiệu đồng15 gold badges134 silver badges170 bronze badges

1

Lấy curl_multi - nó tốt hơn nhiều. Lưu các cái bắt tay - họ không cần thiết mỗi lần!

Đã trả lời ngày 21 tháng 11 năm 2010 lúc 18:03Nov 21, 2010 at 18:03

hoặc lấy pcntl_fork, fork Một số chủ đề mới để thực thi curl_exec. Nhưng nó không tốt bằng

//array of data to POST
$request_contents = array();
//array of URLs
$urls = array();
//array of cURL handles
$chs = array();

//first POST content
$request_contents[] = [
    'a' => 'apple',
    'b' => 'banana'
];
//second POST content
$request_contents[] = [
    'a' => 'fish',
    'b' => 'shrimp'
];
//set the urls
$urls[] = 'http://www.example.com';
$urls[] = 'http://www.example2.com';

//create the array of cURL handles and add to a multi_curl
$mh = curl_multi_init();
foreach ($urls as $key => $url) {
    $chs[$key] = curl_init($url);
    curl_setopt($chs[$key], CURLOPT_RETURNTRANSFER, true);
    curl_setopt($chs[$key], CURLOPT_POST, true);
    curl_setopt($chs[$key], CURLOPT_POSTFIELDS, $request_contents[$key]);

    curl_multi_add_handle($mh, $chs[$key]);
}
0.

Joe

3.1846 Huy hiệu vàng44 Huy hiệu bạc63 Huy hiệu Đồng6 gold badges44 silver badges63 bronze badges

Đã trả lời ngày 6 tháng 10 năm 2011 lúc 3:28Oct 6, 2011 at 3:28

Thí dụ

Đôi khi chúng ta cần thực hiện nhiều yêu cầu bài cho một hoặc nhiều điểm cuối khác nhau. Để đối phó với kịch bản này, chúng ta có thể sử dụng

//array of data to POST
$request_contents = array();
//array of URLs
$urls = array();
//array of cURL handles
$chs = array();

//first POST content
$request_contents[] = [
    'a' => 'apple',
    'b' => 'banana'
];
//second POST content
$request_contents[] = [
    'a' => 'fish',
    'b' => 'shrimp'
];
//set the urls
$urls[] = 'http://www.example.com';
$urls[] = 'http://www.example2.com';

//create the array of cURL handles and add to a multi_curl
$mh = curl_multi_init();
foreach ($urls as $key => $url) {
    $chs[$key] = curl_init($url);
    curl_setopt($chs[$key], CURLOPT_RETURNTRANSFER, true);
    curl_setopt($chs[$key], CURLOPT_POST, true);
    curl_setopt($chs[$key], CURLOPT_POSTFIELDS, $request_contents[$key]);

    curl_multi_add_handle($mh, $chs[$key]);
}
1.

Trước hết, chúng tôi tạo ra có bao nhiêu yêu cầu cần thiết chính xác theo cùng một cách của ví dụ đơn giản và đặt chúng vào một mảng.

Chúng tôi sử dụng curl_multi_init và thêm từng tay cầm vào nó.

Trong ví dụ này, chúng tôi đang sử dụng 2 điểm cuối khác nhau:

//array of data to POST
$request_contents = array();
//array of URLs
$urls = array();
//array of cURL handles
$chs = array();

//first POST content
$request_contents[] = [
    'a' => 'apple',
    'b' => 'banana'
];
//second POST content
$request_contents[] = [
    'a' => 'fish',
    'b' => 'shrimp'
];
//set the urls
$urls[] = 'http://www.example.com';
$urls[] = 'http://www.example2.com';

//create the array of cURL handles and add to a multi_curl
$mh = curl_multi_init();
foreach ($urls as $key => $url) {
    $chs[$key] = curl_init($url);
    curl_setopt($chs[$key], CURLOPT_RETURNTRANSFER, true);
    curl_setopt($chs[$key], CURLOPT_POST, true);
    curl_setopt($chs[$key], CURLOPT_POSTFIELDS, $request_contents[$key]);

    curl_multi_add_handle($mh, $chs[$key]);
}

Sau đó, chúng tôi sử dụng curl_multi_exec để gửi các yêu cầu

//running the requests
$running = null;
do {
  curl_multi_exec($mh, $running);
} while ($running);

//getting the responses
foreach(array_keys($chs) as $key){
    $error = curl_error($chs[$key]);
    $last_effective_URL = curl_getinfo($chs[$key], CURLINFO_EFFECTIVE_URL);
    $time = curl_getinfo($chs[$key], CURLINFO_TOTAL_TIME);
    $response = curl_multi_getcontent($chs[$key]);  // get results
    if (!empty($error)) {
      echo "The request $key return a error: $error" . "\n";
    }
    else {
      echo "The request to '$last_effective_URL' returned '$response' in $time seconds." . "\n";
    }

    curl_multi_remove_handle($mh, $chs[$key]);
}

// close current handler
curl_multi_close($mh);

Một lợi nhuận có thể cho ví dụ này có thể là:

Yêu cầu đến 'http://www.example.com' đã trả lại 'trái cây' trong 2 giây.

Yêu cầu 'http://www.example2.com' đã trả lại 'hải sản' trong 5 giây.




Giới thiệu

Thí dụ

Đôi khi chúng ta cần thực hiện nhiều yêu cầu bài cho một hoặc nhiều điểm cuối khác nhau. Để đối phó với kịch bản này, chúng ta có thể sử dụng

//array of data to POST
$request_contents = array();
//array of URLs
$urls = array();
//array of cURL handles
$chs = array();

//first POST content
$request_contents[] = [
    'a' => 'apple',
    'b' => 'banana'
];
//second POST content
$request_contents[] = [
    'a' => 'fish',
    'b' => 'shrimp'
];
//set the urls
$urls[] = 'http://www.example.com';
$urls[] = 'http://www.example2.com';

//create the array of cURL handles and add to a multi_curl
$mh = curl_multi_init();
foreach ($urls as $key => $url) {
    $chs[$key] = curl_init($url);
    curl_setopt($chs[$key], CURLOPT_RETURNTRANSFER, true);
    curl_setopt($chs[$key], CURLOPT_POST, true);
    curl_setopt($chs[$key], CURLOPT_POSTFIELDS, $request_contents[$key]);

    curl_multi_add_handle($mh, $chs[$key]);
}
1.

Trước hết, chúng tôi tạo ra có bao nhiêu yêu cầu cần thiết chính xác theo cùng một cách của ví dụ đơn giản và đặt chúng vào một mảng.

/**
* Webkul Software.
*
* @category Webkul
* @author Webkul
* @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
* @license https://store.webkul.com/license.html
*/

$result=array();
foreach ($ids as $id) {
  // URL from which data will be fetched
  $fetchURL = 'https://webkul.com&customerId='.$id;
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $fetchURL);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $result[] = curl_exec($ch);
  curl_close($ch);
}

Dung dịch

Nhưng nếu sử dụng nhiều yêu cầu Curl thì thời gian phản hồi sẽ giảm đáng kể.

/**
* Webkul Software.
*
* @category Webkul
* @author Webkul
* @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
* @license https://store.webkul.com/license.html
*/

// array of curl handles
$multiCurl = array();
// data to be returned
$result = array();
// multi handle
$mh = curl_multi_init();
foreach ($ids as $i => $id) {
  // URL from which data will be fetched
  $fetchURL = 'https://webkul.com&customerId='.$id;
  $multiCurl[$i] = curl_init();
  curl_setopt($multiCurl[$i], CURLOPT_URL,$fetchURL);
  curl_setopt($multiCurl[$i], CURLOPT_HEADER,0);
  curl_setopt($multiCurl[$i], CURLOPT_RETURNTRANSFER,1);
  curl_multi_add_handle($mh, $multiCurl[$i]);
}
$index=null;
do {
  curl_multi_exec($mh,$index);
} while($index > 0);
// get content and remove handles
foreach($multiCurl as $k => $ch) {
  $result[$k] = curl_multi_getcontent($ch);
  curl_multi_remove_handle($mh, $ch);
}
// close
curl_multi_close($mh);

Kết quả cho mỗi yêu cầu Curl nằm trong mảng kết quả $ một số thứ như thế này:

Bắt đầu thương mại điện tử không đầu của bạn ngay bây giờ. Đọc thêm
now.
Read More

$result[0] => {"id1":{"customerName":"John","customerAddress":...
$result[1] => {"id2":{"customerName":"JDoe","customerAddress":...
....

. . .

Trở lại đầu trang

  • Giới thiệu