Use the Http class that comes with ThinkPHP to download remote images to a local implementation code _ PHP Tutorial

Source: Internet
Author: User
Use the Http class that comes with ThinkPHP to download the remote image to the local implementation code. The Http class is under the ThinkPHPLibORGNet directory. Next, let's take a look at how to call it. Copy the code as follows :? Phpimport (Com. Buyback. QueryAmazon); import (ORG. Net. Http); classIm Http class is under the ThinkPHP/Lib/ORG/Net directory. Next, let's take a look at how to call it.

The code is as follows:


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'];
}
}
?>


Here, I first determine whether the image information is in the database. if not, I call amazon's webservice to remotely obtain the image address and then use

The code is as follows:


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


The first parameter of the curl_download method is the remote image address, and the second parameter is the local address.
OK, that's simple ~~

Bytes. Next, let's take a look at how to call it. The code is as follows :? Php import ("Com. Buyback. QueryAmazon"); import ("ORG. Net. Http"); class Im...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.