基于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中文字符串截取方法实例总结

本文实例总结了php中文字符串截取方法,非常实用的技巧。分享给大家供大家参考。具体方法分析如下: 用PHP函数substr截取中文字符可能会出现乱码,主要是substr可能硬生生的将一个...

PHP实现驼峰样式字符串(首字母大写)转换成下划线样式字符串的方法示例

本文实例讲述了PHP实现驼峰样式字符串(首字母大写)转换成下划线样式字符串的方法。分享给大家供大家参考,具体如下: 1、如何在php中把驼峰样式的字符串转换成下划线样式的字符串。例:输入...

简单的PHP图片上传程序

第一种: php部分 复制代码 代码如下:<?php  if($_FILES['file']['error'] > 0){  &nbs...

php Smarty 字符比较代码

eq相等, ne、neq不相等, gt大于, lt小于, gte、ge大于等于, lte、le 小于等于, not非, mod求模。 is [not] div by是否能被某数整除, i...

php使用pclzip类实现文件压缩的方法(附pclzip类下载地址)

本文实例讲述了php使用pclzip类实现文件压缩的方法。分享给大家供大家参考,具体如下: 使用PclZIp(zip格式)压缩,首先需要下载它的包文件(可点击此处本站下载)。PclZip...