php获取远程图片的两种 CURL方式和sockets方式获取远程图片
程序员文章站
2023-10-17 16:15:12
方式1:sockets 复制代码 代码如下: $a = "http://jb51.net/content/uploadfile/201106/thum-f3ccdd27d2...
方式1:sockets
$a = "http://jb51.net/content/uploadfile/201106/thum-f3ccdd27d2000e3f9255a7e3e2c4880020110622095243.jpg";
$local = 'socket1.gif';
$aa = getimg($a,$local);
/*
*@ 完整的图片地址
*@ 要存储的文件名
*/
function getimg( $url = "", $filename = "" ) {
if(is_dir(basename($filename))) {
echo "the dir was not exits";
return false;
}
//去除url连接上面可能的引号
$url = preg_replace( '/(?:^[\'"]+|[\'"\/]+$)/', '', $url );
if (!extension_loaded('sockets')) return false;
//获取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;
}
//图片的相对地址
$msg = 'get ' . $matches[3] . " http/1.1\r\n";
//主机名称
$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;
}
方式2:curl
<?php
$url = "http://jb51.net/content/uploadfile/201106/thum-f3ccdd27d2000e3f9255a7e3e2c4880020110622095243.jpg";
$filename = 'curl.gif';
//http://jb51.net
getimg($url, $filename);
/*
*@通过curl方式获取制定的图片到本地
*@ 完整的图片地址
*@ 要存储的文件名
*/
function getimg($url = "", $filename = "") {
if(is_dir(basename($filename))) {
echo "the dir was not exits";
return false;
}
//去除url连接上面可能的引号
$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);//以数据流的方式返回数据,当为false是直接显示出来
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;
}
?>
复制代码 代码如下:
$a = "http://jb51.net/content/uploadfile/201106/thum-f3ccdd27d2000e3f9255a7e3e2c4880020110622095243.jpg";
$local = 'socket1.gif';
$aa = getimg($a,$local);
/*
*@ 完整的图片地址
*@ 要存储的文件名
*/
function getimg( $url = "", $filename = "" ) {
if(is_dir(basename($filename))) {
echo "the dir was not exits";
return false;
}
//去除url连接上面可能的引号
$url = preg_replace( '/(?:^[\'"]+|[\'"\/]+$)/', '', $url );
if (!extension_loaded('sockets')) return false;
//获取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;
}
//图片的相对地址
$msg = 'get ' . $matches[3] . " http/1.1\r\n";
//主机名称
$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;
}
方式2:curl
复制代码 代码如下:
<?php
$url = "http://jb51.net/content/uploadfile/201106/thum-f3ccdd27d2000e3f9255a7e3e2c4880020110622095243.jpg";
$filename = 'curl.gif';
//http://jb51.net
getimg($url, $filename);
/*
*@通过curl方式获取制定的图片到本地
*@ 完整的图片地址
*@ 要存储的文件名
*/
function getimg($url = "", $filename = "") {
if(is_dir(basename($filename))) {
echo "the dir was not exits";
return false;
}
//去除url连接上面可能的引号
$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);//以数据流的方式返回数据,当为false是直接显示出来
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;
}
?>
上一篇: PHP动态分页函数,PHP开发分页必备啦
下一篇: PHP面向对象概念
推荐阅读
-
php获取远程图片的两种 CURL方式和sockets方式获取远程图片
-
PHP高效获取远程图片尺寸和大小的实现方法
-
php获取远程图片的两种 CURL方式和sockets方式获取远程图片_PHP教程
-
php获取远程图片的两种 CURL方式和sockets方式获取远程图片
-
php获取远程图片的两种 CURL方式和sockets方式获取远程图片_PHP
-
php获取远程图片的两种 CURL方式和sockets方式获取远程图片
-
php获取远程图片的两种 CURL方式和sockets方式获取远程图片_php技巧
-
php获取远程图片的两种 CURL方式和sockets方式获取远程图片_PHP教程
-
PHP获取远程图片尺寸和大小的方法
-
PHP高效获取远程图片尺寸和大小的实现方法