PHP get remote picture of two curl ways and sockets ways to get remote picture _php tips

Source: Internet
Author: User
Tags curl
Way 1:sockets
Copy Code code as follows:

$a = "yun_qi_img/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 all the relevant 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 Code code as follows:

<?php
$url = "Yun_qi_img/thum-f3ccdd27d2000e3f9255a7e3e2c4880020110622095243.jpg";
$filename = ' curl.gif ';
Http://jb51.net
Getimg ($url, $filename);
/*
*@ through the curl way to obtain 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 data flow, 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;
}
?>

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.