lang/php

PHP cURL Multiple Processing

C/H 2010. 10. 14. 08:30
참고


$mch = curl_multi_init();

session_write_close();	// curl 세션 정보공유를 위해서 필요함.

$data = "Key=Val or Arr";
$pickup = "http://mypickup.kr/class/method/";

$cookie = Array();
foreach($_COOKIE AS $key => $val) $cookie[] = $key.'='.$val;
$cookie = implode(';', $cookie).'; path=/';

foreach($arr AS $key => $val){
	$ch_{$val} = curl_init();
	curl_setopt($ch_{$val}, CURLOPT_TIMEOUT, 30);
	curl_setopt($ch_{$val}, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch_{$val}, CURLOPT_COOKIE, $cookie);	// 세션 유지를위해서 꼭 필요한 내용 
	curl_setopt($ch_{$val}, CURLOPT_RETURNTRANSFER, 1);

	curl_setopt($ch_{$val}, CURLOPT_URL, $pickup);
	curl_setopt($ch_{$val}, CURLOPT_HEADER, 0);
	curl_setopt($ch_{$val}, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

	curl_setopt($ch_{$val}, CURLOPT_POST, 1);
	curl_setopt($ch_{$val}, CURLOPT_POSTFIELDS, $data);
	curl_multi_add_handle($mch, $ch_{$val});
}

$active = NULL;

do{
	$mrs = curl_multi_exec($mch, $active);
} while ($mrs == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrs == CURLM_OK) {
	if (curl_multi_select($mch) != -1) {
		do {
			$mrs = curl_multi_exec($mch, $active);
		} while ($mrs == CURLM_CALL_MULTI_PERFORM);
	}
}

foreach($this->data['sns'] AS $key => $val){
	if(! empty($ch_{$val})){
		$rs = curl_multi_getcontent($ch_{$val});
		curl_multi_remove_handle($mch, $ch_{$val});
		if(! empty($rs) && is_array(json_decode($rs, TRUE))){ 
			$print = array_merge($this->data, json_decode($rs, TRUE) );
		}
	}
}

curl_multi_close($mch);
반응형