Home >Backend Development >PHP Tutorial >There are two ways to get remote pictures in PHP: CURL method and sockets method to get remote pictures_PHP tutorial

There are two ways to get remote pictures in PHP: CURL method and sockets method to get remote pictures_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:22:48881browse

Method 1: sockets

Copy code The code is as follows:

$a = "http://jb51.net/content/ uploadfile/201106/thum-f3ccdd27d2000e3f9255a7e3e2c4880020110622095243.jpg";
$local = 'socket1.gif';
$aa = getImg($a,$local);
/*
*@ Complete Image address
*@ File name to be stored
*/
function getImg( $url = "", $filename = "" ) {
if(is_dir(basename($filename))) {
echo "The Dir was not exits";
Return false;
}
//Remove possible quotes on the URL connection
$url = preg_replace( '/(?:^[ '"]+|['"/]+$)/', '', $url );
if (!extension_loaded('sockets')) return false;
//Get url related information
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 the image
$msg = 'GET ' . $matches[3] . " HTTP/1.1rn" ;
//Host name
$msg .= 'Host: ' . $matches[1] . "rn";
$msg .= 'Connection: Close' . "rnrn";
socket_write( $sock, $msg );
$bin = '';
while ( $tmp = socket_read( $sock, 10 ) ) {
$bin .= $tmp;
$tmp = '';
}
$bin = explode("rnrn", $bin);
$img = $bin[1];
$h = fopen( $filename, 'wb' );
$res = fwrite( $h, $img ) === false ? false : true;
@socket_close( $sock );
Return $res;
}

Method 2: curl
Copy code The code is as follows:

$url = "http://jb51.net/content/uploadfile/201106/thum-f3ccdd27d2000e3f9255a7e3e2c4880020110622095243.jpg";
$filename = 'curl.gif';
//http://jb51.net
getImg($url, $filename);
/*
*@Get the specified image locally through curl
*@ Complete image address
*@ File name to be stored
*/
function getImg($url = "", $filename = "") {
if(is_dir(basename($filename))) {
echo "The Dir was not exits";
Return false;
}
//Remove possible quotes on 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);//Return data in the form of data stream, when it is false, it 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;
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324605.htmlTechArticleMethod 1: sockets Copy the code as follows: $a = "http://jb51.net/content/uploadfile /201106/thum-f3ccdd27d2000e3f9255a7e3e2c4880020110622095243.jpg"; $local = 'socket1.gif'; $aa =...
Statement:
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