基于PHP CURL用法的深入分析

yipeiwu_com6年前PHP代码库
如下所示:
复制代码 代码如下:

<?php
header('Context-Type:text/html;charset:gb2312;');
$urls = array(
 'http://www.baidu.com/',
 'http://www.pconline.com.cn/',
 'http://www.163.com/'
);
$options = array(
 CURLOPT_RETURNTRANSFER=>1,
 CURLOPT_FOLLOWLOCATION=>1, 
 CURLOPT_HEADER => false, 
 CURLOPT_HTTPHEADER => array(
  'Accept'=>' text/html, application/xhtml+xml,',
  'Accept-Encoding'=>' gzip, deflate',
  'Accept-Language'=>' zh-CN',
  'Connection'=>' Keep-Alive', 
  'User-Agent'=>' Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)',
 ),
);
function curlMultiRequest($urls,$options=array()){
 $ch = array();
 $results = array();
 $mh = curl_multi_init();
 foreach($urls as $key=>$val){
  $ch[$key] = curl_init();
  if($options){
   curl_setopt_array($ch[$key],$options);
  }  
  curl_setopt($ch[$key],CURLOPT_URL,$val);
  curl_multi_add_handle($mh,$ch[$key]);
 }

 $running = null;
 do{
  curl_multi_exec($mh,$running);
 }while($running>0); 

 foreach($ch as $key=>$val){
  //$results[$key] = iconv('gb2312','utf-8',curl_multi_getcontent($val));
  $results[$key] = curl_multi_getcontent($val);
  curl_multi_remove_handle($mh,$val);
  curl_close($val);
 } 
 curl_multi_close($mh); 
 return $results;
}
$results = curlMultiRequest($urls,$options);
print_r($results);
?>

相关文章

PHP使用PDO操作sqlite数据库应用案例

本文实例讲述了PHP使用PDO操作sqlite数据库。分享给大家供大家参考,具体如下: 1、需求: 已知: 1)、一个json文件,里面是一个二维数组,数组解析出来为: array...

php中利用explode函数分割字符串到数组

分割字符串 //利用 explode 函数分割字符串到数组 复制代码 代码如下: <?php $source = "hello1,hello2,hello3,hello4,hell...

php 文本文件的读取效率

首页大概3KB,是在本地测试的 复制代码 代码如下: file_get_contents('shadow.xml'); 耗时 0.0003 秒 复制代码 代码如下: $indexFil...

php结合正则获取字符串中数字

php结合正则获取字符串中数字 <?php $patterns = "/\d+/"; //第一种 //$patterns = "/\d/"; //第二种...

php基于ob_start(ob_gzhandler)实现网页压缩功能的方法

php基于ob_start(ob_gzhandler)实现网页压缩功能的方法

本文实例讲述了php基于ob_start('ob_gzhandler')实现网页压缩功能的方法。分享给大家供大家参考,具体如下: PHP生成网页后传送给浏览器显示 ,页面的打开速度除了与...