mblog php,一个高速下载图片的php算法

发表于 2012-07-03 07:40:16 by 月小升

注明:这个下载的方法来自腾讯微博API接口算法Tencent.php

这个文件封装的算法很直接学习,今天突然发现这个算法能高速解析没有文件名的图片。

另外其他简单的php下载远程图片的算法,请见php远程下载图片显示到浏览器3种方法

header("Content-type: image/png");

$url = "http://img3.cache.netease.com/www/logo/logo_png.png";

$url = 'http://app.qlogo.cn/mbloghead/41c62e6cdde14055f484/50';

$r = Http::request($url, $params, $method, $multi);

echo $r;

/**

localhost/qq/test.php

* HTTP请求类

* @author xiaopengzhu

* @version 2.0 2012-04-20

*/

class Http

{

/**

* 发起一个HTTP/HTTPS的请求

* @param $url 接口的URL

* @param $params 接口参数 array('content'=>'test', 'format'=>'json');

* @param $method 请求类型 GET|POST

* @param $multi 图片信息

* @param $extheaders 扩展的包头信息

* @return string

*/

public static function request( $url , $params = array(), $method = 'GET' , $multi = false, $extheaders = array())

{

if(!function_exists('curl_init')) exit('Need to open the curl extension');

$method = strtoupper($method);

$ci = curl_init();

curl_setopt($ci, CURLOPT_USERAGENT, 'PHP-SDK OAuth2.0');

curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 3);

curl_setopt($ci, CURLOPT_TIMEOUT, 3);

curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);

curl_setopt($ci, CURLOPT_HEADER, false);

$headers = (array)$extheaders;

switch ($method)

{

case 'POST':

curl_setopt($ci, CURLOPT_POST, TRUE);

if (!empty($params))

{

if($multi)

{

foreach($multi as $key => $file)

{

$params[$key] = '@' . $file;

}

curl_setopt($ci, CURLOPT_POSTFIELDS, $params);

$headers[] = 'Expect: ';

}

else

{

curl_setopt($ci, CURLOPT_POSTFIELDS, http_build_query($params));

}

}

break;

case 'DELETE':

case 'GET':

$method == 'DELETE' && curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');

if (!empty($params))

{

$url = $url . (strpos($url, '?') ? '&' : '?')

. (is_array($params) ? http_build_query($params) : $params);

}

break;

}

curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE );

curl_setopt($ci, CURLOPT_URL, $url);

if($headers)

{

curl_setopt($ci, CURLOPT_HTTPHEADER, $headers );

}

$response = curl_exec($ci);

curl_close ($ci);

return $response;

}

}

This entry was posted in PHP. Bookmark the permalink.

月小升QQ 2651044202, 技术交流QQ群 178491360

首发地址:月小升博客 – https://java-er.com/blog/php-high-img/

无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值