curl自动采撷远程服务器图片方法,CU兰德中华V

作者: 金沙澳门官网  发布:2019-09-26

 代码如下

方式1:sockets

<?php
$url = "";
$filename = 'curl.gif';

复制代码 代码如下:

getImg($url, $filename);
/*
*@通过curl方式获得制订的图片到本地
*@ 完整的图形地址
*@ 要存款和储蓄的文本名
*/
function getImg($url = "", $filename = "") {
if(is_dir(basename($filename))) {
echo "The Dir was not exits";
Return false;
}
//去除UOdysseyL连接上边也许的引号
$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_RETU奥德赛NTRANSFE昂科雷,false);//以数据流的章程赶回数据,当为false是间接彰显出来
curl_setopt($hander,CURLOPT_TIMEOUT,60);
/*$options = array(
CURLOPT_URL=> '',
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 = "";
$local = 'socket1.gif';
$aa = getImg($a,$local);
/*
*@ 完整的图片地址
*@ 要存款和储蓄的文件名
*/
function getImg( $url = "", $filename = "" ) {
if(is_dir(basename($filename))) {
echo "The Dir was not exits";
Return false;
}
//去除UPAJEROL连接上面恐怕的引号
$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.1rn";
//主机名称
$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;
}

方式2:curl

复制代码 代码如下:

<?php
$url = "";
$filename = 'curl.gif';
//
getImg($url, $filename);
/*
*@通过curl格局赢得制订的图样到地面
*@ 完整的图纸地址
*@ 要存款和储蓄的文书名
*/
function getImg($url = "", $filename = "") {
if(is_dir(basename($filename))) {
echo "The Dir was not exits";
Return false;
}
//去除ULacrosseL连接上面恐怕的引号
$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_RETUEscortNTRANSFE中华V,false);//以数据流的方法赶回数据,当为false是一直显示出来
curl_setopt($hander,CURLOPT_TIMEOUT,60);
/*$options = array(
CURLOPT_URL=> '',
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使用curl模拟post上传及接收文件的措施
  • php curl 上传文件代码实例
  • PHP基于CUCRUISERL实行POST数据上传实例
  • php完结curl模拟ftp上传的办法
  • php curl上传、下载、https登录完结代码
  • 浅析PHP7新职能及语法变化总计
  • 座谈 PHP7新增添效果与利益
  • PHP通过CUWranglerL完结定期职责的图形抓取功效示例
  • php利用curl获取远程图片完毕格局
  • PHP7基于curl完成的上传图片功效

本文由金沙澳门官网送注册58发布于金沙澳门官网,转载请注明出处:curl自动采撷远程服务器图片方法,CU兰德中华V

关键词: