Home > php教程 > php手册 > body text

使用ThinkPHP自带的Http类下载远程图片到本地的实现代码

WBOY
Release: 2016-06-13 12:06:33
Original
1492 people have browsed it

Http类在目录ThinkPHP/Lib/ORG/Net下面。接下来看看是如何调用的。

复制代码 代码如下:


import("Com.Buyback.QueryAmazon");
import("ORG.Net.Http");
class Image {
public static function getImage($isbn) {
$bookInformModel = D("bookinform");
$result = $bookInformModel->where("isbn='$isbn'")->select();
if($result[0]['image'] == ""){
$data['inform_id'] = $result[0]['inform_id'];
$remoteUrl = QueryAmazon::getImage($isbn);
if(!empty($remoteUrl['ImageURL'])){
$localUrl = "Public/bookcover/".$isbn.".jpg";
Http::curl_download($remoteUrl['ImageURL'], "./".$localUrl);
}else{
$localUrl = "Public/bookcover/unknownbook.png";
}
$data['image'] = $localUrl;
$bookInformModel->save($data);
return $localUrl;
}
return $result[0]['image'];
}
}
?>


这里我先判断数据库中有无该图片信息,没有就调用amazon的webservice从远程获取到图片地址,再使用

复制代码 代码如下:


Http::curl_download($remoteUrl['ImageURL'], "./".$localUrl);


其中curl_download方法第一个参数是远程图片地址,第二个参数是保存到本地的地址。
OK,就这么简单~~
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template