PHP get remote picture of two curl way and sockets way to get remote picture _php tutorial

Source: Internet
Author: User
Way 1:sockets
Copy CodeThe code is as follows:
$a = "http://jb51.net/content/uploadfile/201106/thum-f3ccdd27d2000e3f9255a7e3e2c4880020110622095243.jpg";
$local = ' socket1.gif ';
$AA = getimg ($a, $local);
/*
*@ Full Picture Address
*@ the file name to store
*/
function getimg ($url = "", $filename = "") {
if (Is_dir (basename ($filename))) {
echo "The Dir is not exits";
Return false;
}
Remove the possible quotes above the URL connection
$url = Preg_replace ('/(?: ^[\ ' "]+|[ \ ' "\/]+$)/', ', $url);
if (!extension_loaded (' Sockets ')) return false;
Get information about the URL
Preg_match ('/http:\/\/' ([^\/\:]+ (\:\d{1,5})?) (. *)/I ', $url, $matches);
if (! $matches) return false;
$sock = Socket_create (Af_inet, Sock_stream, sol_tcp);
if (! @socket_connect ($sock, $matches [1], $matches [2]? substr ($matches [2], 1): 80)) {
return false;
}
Relative address of picture
$msg = ' GET '. $matches [3]. "Http/1.1\r\n";
Host Name
$msg. = ' Host: '. $matches [1]. "\ r \ n";
$msg. = ' Connection:close '. "\r\n\r\n";
Socket_write ($sock, $msg);
$bin = ";
while ($tmp = Socket_read ($sock, 10)) {
$bin. = $tmp;
$tmp = ";
}
$bin = Explode ("\r\n\r\n", $bin);
$img = $bin [1];
$h = fopen ($filename, ' WB ');
$res = fwrite ($h, $img) = = = False? False:true;
@socket_close ($sock);
Return $res;
}

Way 2:curl
Copy CodeThe code is as follows:
$url = "Http://jb51.net/content/uploadfile/201106/thum-f3ccdd27d2000e3f9255a7e3e2c4880020110622095243.jpg";
$filename = ' curl.gif ';
Http://jb51.net
Getimg ($url, $filename);
/*
*@ through curl to get the developed picture to the local
*@ Full Picture Address
*@ the file name to store
*/
function getimg ($url = "", $filename = "") {
if (Is_dir (basename ($filename))) {
echo "The Dir is not exits";
Return false;
}
Remove the possible quotes above the URL connection
$url = Preg_replace ('/(?: ^[\ ' "]+|[ \ ' "\/]+$)/', ', $url);
$hander = Curl_init ();
$fp = fopen ($filename, ' WB ');
curl_setopt ($hander, Curlopt_url, $url);
curl_setopt ($hander, Curlopt_file, $fp);
curl_setopt ($hander, curlopt_header,0);
curl_setopt ($hander, curlopt_followlocation,1);
curl_setopt ($hander, curlopt_returntransfer,false);//returns data as a data stream, when false is displayed directly
curl_setopt ($hander, curlopt_timeout,60);
/* $options = Array (
Curlopt_url=> ' Http://jb51.net/content/uploadfile/201106/thum-f3ccdd27d2000e3f9255a7e3e2c4880020110622095243.jpg ',
Curlopt_file = $fp,
Curlopt_header = 0,
Curlopt_followlocation = 1,
Curlopt_timeout = 60
);
Curl_setopt_array ($hander, $options);
*/
Curl_exec ($hander);
Curl_close ($hander);
Fclose ($FP);
Return true;
}
?>

http://www.bkjia.com/PHPjc/324605.html www.bkjia.com true http://www.bkjia.com/PHPjc/324605.html techarticle the way 1:sockets copy code code is as follows: $a = "http://jb51.net/content/uploadfile/201106/ Thum-f3ccdd27d2000e3f9255a7e3e2c4880020110622095243.jpg "; $local = ' socket1.gif '; $aa = ...

  • 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.