- <?php
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- * Description of CUrl
- *
- * @author Administrator
- */
- class CUrl {
- private static $curl = null;
- private static $mcurl = null;
- public static function start() {
- self::$curl = curl_init();
- curl_setopt(self::$curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt(self::$curl, CURLOPT_HEADER, 0);
- }
- /**
- * 取得页面信息
- * @param type $url
- * @return type
- */
- public static function Info($url){
- self::start();
- curl_setopt(self::$curl, CURLOPT_URL, $url);
- $info = curl_getinfo(self::$curl);
- self::close();
- return $info;
- }
- /**
- * 下载图片
- * @param string $url
- * @param string $savepath
- */
- public static function Download($url,$savepath){
- $content = CUrl::Http($url);
- preg_match_all('/http:\/\/[^\?^\,]*?jpg/i', $content, $matches);
- list($jpgs) = $matches;
- $index = @$_GET['index'] ? @$_GET['index'] : 0;
- $count = count($jpgs);
- /*echo "<pre>";
- print_r($jpgs);
- echo "</pre>";
- die;*/
- $pic = CUrl::Http($jpgs[$index], null, $info);
- if(!in_array($info['http_code'],array('404','302')) && $pic) file_put_contents("{$savepath}/{$index}.jpg", $pic);
- $index++;
- if($index==($count-1)) exit('<br/>the End');
- exit('<meta http-equiv="refresh" content="0; url=' . urldecode("http://www.qweibo.com/test.php?index={$index}") . '"/>');
- }
- /**
- * 请求一个地址
- * @param string $url
- * @param array $post array("content" => "我是post值","upload" => "@F:/p_w_picpath/dsfdsf.rar"//要上传的本地文件地址)
- * @return 返回请求地址的页面内容
- */
- public static function Http($url, $post=null, &$info=array() , $timeout = null){
- self::start();
- curl_setopt(self::$curl, CURLOPT_URL, $url);
- if(!emptyempty($post)){//POST($_POST)和上传文件($_FILES)都可
- $options = array(
- CURLOPT_POST => 1,
- CURLOPT_POSTFIELDS => $post,
- );
- curl_setopt_array(self::$curl, $options);
- }
- if(!is_array($info)) {
- @$info = curl_getinfo(self::$curl);//取得请求信息 如果传了引用的话就不再是默认值array(),引用的值是为空的
- }
- if($timeout){
- //设置超时时间
- curl_setopt ( self::$curl, CURLOPT_TIMEOUT, $timeout );
- }
- $output = curl_exec(self::$curl);
- if($output === false) {
- return 'Curl error: ' . curl_error(self::$curl);//返回一个保护当前会话最近一次错误的字符串
- }
- self::close();
- return $output;
- }
- /**
- * 一次请求多个地址
- * @param array $urls 多个地址
- * @param array $posts 每个地址对应的POST数据 二维数组
- * @return 返回所有地址的内容
- */
- public static function MultiHttp($urls, $posts=null){
- self::$mcurl = curl_multi_init();
- foreach ($urls as $i => $url) {
- $conn[$i]=curl_init($url);
- if(!emptyempty($posts[$i])){//POST($_POST)和上传文件($_FILES)都可
- $options = array(
- CURLOPT_POST => 1,
- CURLOPT_POSTFIELDS => $posts[$i],
- );
- curl_setopt_array($conn[$i], $options);
- }
- curl_setopt($conn[$i],CURLOPT_RETURNTRANSFER,1);
- curl_multi_add_handle (self::$mcurl,$conn[$i]);
- }
- do { $n=curl_multi_exec(self::$mcurl,$active); } while ($active);
- foreach ($urls as $i => $url) {
- $outputs[$i]=curl_multi_getcontent($conn[$i]);
- curl_close($conn[$i]);
- }
- return $outputs;
- }
- public function close(){
- curl_close(self::$curl);
- }
- }
- ?>
转载于:https://blog.51cto.com/6099994/1009212