>  기사  >  백엔드 개발  >  PHP 컬 일괄 처리는 제어 가능한 동시 및 비동기 작업을 실현합니다.

PHP 컬 일괄 처리는 제어 가능한 동시 및 비동기 작업을 실현합니다.

不言
不言원래의
2018-05-09 10:26:321080검색

이 글은 주로 PHP 컬 배치 처리의 제어 가능한 동시성과 비동기 작업을 소개합니다. 이제 이를 여러분과 공유합니다. 도움이 필요한 친구들이 참고할 수 있습니다. 컬 일괄 처리 동시 비동기 작업을 제어합니다. 참조를 위해 모든 사람과 공유하세요. 세부 사항은 다음과 같습니다.

일반적으로 PHP의 cURL은 차단 방식으로 실행됩니다. 즉, cURL 요청을 생성한 후 성공적으로 실행될 때까지 기다리거나 실행하기 전에 시간 초과되어야 합니다. 다음 요청: 일반적으로 API 인터페이스 액세스는 CURL이 첫 번째 선택입니다

실제 프로젝트를 진행하거나 자신만의 가젯을 작성하는 과정(예: 뉴스 수집, 상품 가격 모니터링, 가격 비교)에서는 일반적으로 제3자로부터 데이터를 얻어야 합니다. 웹사이트 또는 API 인터페이스를 처리해야 하는 경우 성능을 향상하려면 cURL에서 제공하는

계열 기능을 사용하여 간단한 동시성을 달성할 수 있습니다.

<?php
include &#39;curl.class.php&#39;;
function callback($response, $info, $error, $request)
{
 echo &#39;response:<br>&#39;;
 print_r($response);
 echo &#39;<br>&#39; . date("Y-m-d H:i:s") . &#39;   <br>&#39;;
 echo &#39;<br>&#39; . str_repeat("-", 100) . &#39;<br>&#39;;
}
$USER_COOKIE = (!empty($_REQUEST[&#39;cookie&#39;])) ? $_REQUEST[&#39;cookie&#39;] : file_get_contents("cookie.txt");
$curl = new Curl ("callback");
$data = array(
 array(
  &#39;url&#39; => &#39;http://dyactive2.vip.xunlei.com/com_sign/?game=qmr&type=rec_gametime&referfrom=&rt=0.42521539455332336&#39;, //秦美人
  &#39;method&#39; => &#39;POST&#39;,
  &#39;post_data&#39; => &#39;&#39;,
  &#39;header&#39; => null,
  &#39;options&#39; => array(
   CURLOPT_REFERER => "http://niu.xunlei.com/entergame/?gameNo=qmr&fenQuNum=3",
   CURLOPT_COOKIE => $USER_COOKIE,
  )
 ),
 array(
  &#39;url&#39; => &#39;http://dyactive2.vip.xunlei.com/com_sign/?game=sq&type=rec_gametime&referfrom=&rt=0.42521539455332336&#39;, //神曲
  &#39;method&#39; => &#39;POST&#39;,
  &#39;post_data&#39; => &#39;&#39;,
  &#39;header&#39; => null,
  &#39;options&#39; => array(
   CURLOPT_REFERER => "http://niu.xunlei.com/entergame/?gameNo=sq&fenQuNum=41",
   CURLOPT_COOKIE => $USER_COOKIE,
  )
 ),
 array(
  &#39;url&#39; => &#39;http://dyactive2.vip.xunlei.com/com_sign/?game=frxz&type=rec_gametime&referfrom=&rt=0.42521539455332336&#39;, //凡人修真
  &#39;method&#39; => &#39;POST&#39;,
  &#39;post_data&#39; => &#39;&#39;,
  &#39;header&#39; => null,
  &#39;options&#39; => array(
   CURLOPT_REFERER => "http://niu.xunlei.com/entergame/?gameNo=frxz&fenQuNum=3",
   CURLOPT_COOKIE => $USER_COOKIE,
  )
 ),
 array(
  &#39;url&#39; => &#39;http://dyactive2.vip.xunlei.com/com_sign/?game=smxj&type=rec_gametime&referfrom=&rt=0.42521539455332336&#39;, //神魔仙界
  &#39;method&#39; => &#39;POST&#39;,
  &#39;post_data&#39; => &#39;&#39;,
  &#39;header&#39; => null,
  &#39;options&#39; => array(
   CURLOPT_REFERER => "http://niu.xunlei.com/entergame/?gameNo=smxj&fenQuNum=2",
   CURLOPT_COOKIE => $USER_COOKIE,
  )
 ),
 array(
  &#39;url&#39; => &#39;http://dyactive2.vip.xunlei.com/com_sign/?game=qsqy&type=rec_gametime&referfrom=&rt=0.42521539455332336&#39;, //倾世情缘
  &#39;method&#39; => &#39;POST&#39;,
  &#39;post_data&#39; => &#39;&#39;,
  &#39;header&#39; => null,
  &#39;options&#39; => array(
   CURLOPT_REFERER => "http://niu.xunlei.com/entergame/?gameNo=qsqy&fenQuNum=11",
   CURLOPT_COOKIE => $USER_COOKIE,
  )
 ),
);
foreach ($data as $val) {
 $request = new Curl_request ($val [&#39;url&#39;], $val [&#39;method&#39;], $val [&#39;post_data&#39;], $val [&#39;header&#39;], $val [&#39;options&#39;]);
 $curl->add($request);
}
$curl->execute();
echo $curl->display_errors();
curl_multi_* 사용 시 매우 잘 작동하며 부작용이 없습니다. 동시성 수는 제어 가능하며 많은 응용 프로그램이 있습니다. 상상력을 발휘하십시오.

<?php
/**
 * cURL批量处理 工具类
 * 
 * @since Version 1.0
 * @author Justmepzy <justmepzy@gmail.com>
 * @link http://t.qq.com/JustPzy
 */
/**
 *单一的请求对象
 */
class Curl_request {
 public $url   = &#39;&#39;;
 public $method   = &#39;GET&#39;;
 public $post_data  = null;
 public $headers  = null;
 public $options  = null;
 /**
  * 
  * @param string $url
  * @param string $method
  * @param string $post_data
  * @param string $headers
  * @param array $options
  * @return void
  */
 public function __construct($url, $method = &#39;GET&#39;, $post_data = null, $headers = null, $options = null) {
  $this->url = $url;
  $this->method = strtoupper( $method );
  $this->post_data = $post_data;
  $this->headers = $headers;
  $this->options = $options;
 }
 /**
  * @return void
  */
 public function __destruct() {
  unset ( $this->url, $this->method, $this->post_data, $this->headers, $this->options );
 }
}
/**
 * 包含请求列队处理
 */
class Curl {
 /**
  * 请求url个数
  * @var int
  */
 private $size    = 5;
 /**
  * 等待所有cURL批处理中的活动连接等待响应时间
  * @var int
  */
 private $timeout   = 5;
 /**
  * 完成请求回调函数
  * @var string
  */
 private $callback   = null;
 /**
  * cRUL配置
  * @var array
  */
 private $options   = array (CURLOPT_SSL_VERIFYPEER => 0,CURLOPT_RETURNTRANSFER => 1,CURLOPT_CONNECTTIMEOUT => 30 );
 /**
  * 请求头
  * @var array
  */
 private $headers   = array ();
 /**
  * 请求列队
  * @var array
  */
 private $requests   = array ();
 /**
  * 请求列队索引
  * @var array
  */
 private $request_map  = array ();
 /**
  * 错误
  * @var array
  */
 private $errors   = array ();
 /**
  * @access public
  * @param string $callback 回调函数
  * 该函数有4个参数($response,$info,$error,$request)
  * $response url返回的body
  * $info  cURL连接资源句柄的信息
  * $error  错误
  * $request  请求对象
  */
 public function __construct($callback = null) {
  $this->callback = $callback;
 }
 /**
  * 添加一个请求对象到列队
  * @access public
  * @param object $request
  * @return boolean
  */
 public function add($request) {
  $this->requests [] = $request;
  return TRUE;
 }
 /**
  * 创建一个请求对象并添加到列队
  * @access public
  * @param string $url
  * @param string $method
  * @param string $post_data
  * @param string $headers
  * @param array $options
  * @return boolean
  */
 public function request($url, $method = &#39;GET&#39;, $post_data = null, $headers = null, $options = null) {
  $this->requests [] = new Curl_request ( $url, $method, $post_data, $headers, $options );
  return TRUE;
 }
 /**
  * 创建GET请求对象
  * @access public
  * @param string $url
  * @param string $headers
  * @param array $options
  * @return boolean
  */
 public function get($url, $headers = null, $options = null) {
  return $this->request ( $url, "GET", null, $headers, $options );
 }
 /**
  * 创建一个POST请求对象
  * @access public
  * @param string $url
  * @param string $post_data
  * @param string $headers
  * @param array $options
  * @return boolean
  */
 public function post($url, $post_data = null, $headers = null, $options = null) {
  return $this->request ( $url, "POST", $post_data, $headers, $options );
 }
 /**
  * 执行cURL
  * @access public
  * @param int $size 最大连接数
  * @return Ambigous <boolean, mixed>|boolean
  */
 public function execute($size = null) {
  if (sizeof ( $this->requests ) == 1) {
   return $this->single_curl ();
  } else {
   return $this->rolling_curl ( $size );
  }
 }
 /**
  * 单个url请求
  * @access private
  * @return mixed|boolean
  */
 private function single_curl() {
  $ch = curl_init ();
  $request = array_shift ( $this->requests );
  $options = $this->get_options ( $request );
  curl_setopt_array ( $ch, $options );
  $output = curl_exec ( $ch );
  $info = curl_getinfo ( $ch );
  // it&#39;s not neccesary to set a callback for one-off requests
  if ($this->callback) {
   $callback = $this->callback;
   if (is_callable ( $this->callback )) {
    call_user_func ( $callback, $output, $info, $request );
   }
  } else
   return $output;
  return true;
 }
 /**
  * 多个url请求
  * @access private
  * @param int $size 最大连接数
  * @return boolean
  */
 private function rolling_curl($size = null) {
  if ($size)
   $this->size = $size;
  else 
   $this->size = count($this->requests);
  if (sizeof ( $this->requests ) < $this->size)
   $this->size = sizeof ( $this->requests );
  if ($this->size < 2)
   $this->set_error ( &#39;size must be greater than 1&#39; );
  $master = curl_multi_init ();
  //添加cURL连接资源句柄到map索引
  for($i = 0; $i < $this->size; $i ++) {
   $ch = curl_init ();
   $options = $this->get_options ( $this->requests [$i] );
   curl_setopt_array ( $ch, $options );
   curl_multi_add_handle ( $master, $ch );
   $key = ( string ) $ch;
   $this->request_map [$key] = $i;
  }
  $active = $done = null;
  do {
   while ( ($execrun = curl_multi_exec ( $master, $active )) == CURLM_CALL_MULTI_PERFORM )
    ;
   if ($execrun != CURLM_OK)
    break;
   //有一个请求完成则回调
   while ( $done = curl_multi_info_read ( $master ) ) {
    //$done 完成的请求句柄
    $info = curl_getinfo ( $done [&#39;handle&#39;] );//
    $output = curl_multi_getcontent ( $done [&#39;handle&#39;] );//
    $error = curl_error ( $done [&#39;handle&#39;] );//
    $this->set_error ( $error );
    //调用回调函数,如果存在的话
    $callback = $this->callback;
    if (is_callable ( $callback )) {
     $key = ( string ) $done [&#39;handle&#39;];
     $request = $this->requests [$this->request_map [$key]];
     unset ( $this->request_map [$key] );
     call_user_func ( $callback, $output, $info, $error, $request );
    }
    curl_close ( $done [&#39;handle&#39;] );
    //从列队中移除已经完成的request
    curl_multi_remove_handle ( $master, $done [&#39;handle&#39;] );
   }
   //等待所有cURL批处理中的活动连接
   if ($active)
    curl_multi_select ( $master, $this->timeout );
  } while ( $active );
  //完成关闭
  curl_multi_close ( $master );
  return true;
 }
 /**
  * 获取没得请求对象的cURL配置
  * @access private
  * @param object $request
  * @return array
  */
 private function get_options($request) {
  $options = $this->__get ( &#39;options&#39; );
  if (ini_get ( &#39;safe_mode&#39; ) == &#39;Off&#39; || ! ini_get ( &#39;safe_mode&#39; )) {
   $options [CURLOPT_FOLLOWLOCATION] = 1;
   $options [CURLOPT_MAXREDIRS] = 5;
  }
  $headers = $this->__get ( &#39;headers&#39; );
  if ($request->options) {
   $options = $request->options + $options;
  }
  $options [CURLOPT_URL] = $request->url;
  if ($request->post_data && strtolower($request->method) == &#39;post&#39; ) {
   $options [CURLOPT_POST] = 1;
   $options [CURLOPT_POSTFIELDS] = $request->post_data;
  }
  if ($headers) {
   $options [CURLOPT_HEADER] = 0;
   $options [CURLOPT_HTTPHEADER] = $headers;
  }
  return $options;
 }
 /**
  * 设置错误信息
  * @access public
  * @param string $msg
  */
 public function set_error($msg) {
  if (! empty ( $msg ))
   $this->errors [] = $msg;
 }
 /**
  * 获取错误信息
  * @access public
  * @param string $open
  * @param string $close
  * @return string
  */
 public function display_errors($open = &#39;<p>&#39;, $close = &#39;</p>&#39;) {
  $str = &#39;&#39;;
  foreach ( $this->errors as $val ) {
   $str .= $open . $val . $close;
  }
  return $str;
 }
 /**
  * @access public
  * @param string $name
  * @param string $value
  * @return boolean
  */
 public function __set($name, $value) {
  if ($name == &#39;options&#39; || $name == &#39;headers&#39;) {
   $this->{$name} = $value + $this->{$name};
  } else {
   $this->{$name} = $value;
  }
  return TRUE;
 }
 /**
  * 
  * @param string $name
  * @return mixed
  * @access public
  */
 public function __get($name) {
  return (isset ( $this->{$name} )) ? $this->{$name} : null;
 }
 /**
  * @return void
  * @access public
  */
 public function __destruct() {
  unset ( $this->size, $this->timeout, $this->callback, $this->options, $this->headers, $this->requests, $this->request_map, $this->errors );
 }
}
// END Curl Class
/* End of file curl.class.php */

이상 이 기사의 전체 내용은 PHP 중국어 웹 사이트를 참조하십시오.

관련 권장사항:

php 컬이 가짜 요청을 보냅니다

php CURL은 로그인 방법 코드 예제를 시뮬레이션하기 위해 쿠키를 얻습니다

위 내용은 PHP 컬 일괄 처리는 제어 가능한 동시 및 비동기 작업을 실현합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.