dedecms的dededown类获取远程图片的函数用如下,调用的时候传递过去的也是有效,但是发现以下两句为空,无法获取
获取远程图片的函数用如下,调用的时候传递过去的也是有效,但是发现以下两句为空,无法获取
$htd->openurl( $url );$htd->gethead( "content-type" )以上2句调用为空。
function getremoteimage( $url, $rndtrueName )
{
// echo $url;
$revalues = array( );
$ok = false;
$htd = new dedehttpdown( );
$htd->openurl( $url );
//echo $htd->openurl( $url ); $sparr = array( "image/pjpeg", "image/jpeg", "image/gif", "image/jpg", "image/png", "image/xpng", "image/wbmp" );
if ( !in_array( $htd->gethead( "content-type" ), $sparr ) )
{
echo "1";
return "";
}
make_dir( dirname( $rndtrueName ) );
$itype = $htd->gethead( "content-type" );
$ok = $htd->savetobin( $rndtrueName );
if ( $ok )
{
$data = getimagesize( $rndtrueName );
$revalues[0] = $rndtrueName;
list( , $revalues[2], $revalues[2] ) = $data;
}
$htd->close( );
if ( $ok )
{
return $revalues;
}
return "";
}
//用Http协议下载文件
function savetobin( $savefilename )
{
if ( !$this->isgetok( ) )
{
return false;
}
if ( @feof( $this->m_fp ) )
{
$this->m_error = "连接已经关闭!";
return false;
}
$fp = fopen( $savefilename, "w" );
while ( !feof( $this->m_fp ) )
{
fwrite( $fp, fread( $this->m_fp, 1024 ) );
}
fclose( $this->m_fp );
fclose( $fp );
return true;
} //打开指定网址
function openurl( $url, $requestType = "GET" )
{
$this->resetany( );
$this->JumpCount = 0;
$this->m_httphead = array( );
//print_r($this->m_httphead);
/*
$extend =explode(\".\" , $url);
$va=count($extend)-1;
$extend[$va];
$this->m_httphead[""]=
*/
$this->m_html = "";
$this->reTry = 0;
$this->close( );
//初始化系统
$this->privateinit( $url );
$this->privatestartsession();
} //开始HTTP会话
function privatestartsession( $requestType = "GET" )
{
if ( !$this->privateopenhost( ) )
{
$this->m_error .= "打开远程主机出错!";
return false;
}
++$this->reTry;
if ( $this->gethead( "http-edition" ) == "HTTP/1.1" )
{
$httpv = "HTTP/1.1";
}
else
{
$httpv = "HTTP/1.0";
}
$ps = explode( "?", $this->m_urlpath );
//echo $this->m_urlpath;/images/200910/goods_img/599_P_1255757135855.jpg
//print_r($ps);Array([0] => /images/200910/goods_img/599_P_1255757135855.jpg)
$headString = "";
//发送固定的起始请求头GET、Host信息
if ( $requestType == "GET" )
{
$headString .= "GET ".$this->m_urlpath.( " ".$httpv."\r\n" );
}
else
{
$headString .= "POST ".$ps[0].( " ".$httpv."\r\n" );
}
$this->m_puthead['Host'] = $this->m_host;
//发送用户自定义的请求头
if ( !isset( $this->m_puthead['Accept'] ) )
{
$this->m_puthead['Accept'] = "*/*";
}
if ( !isset( $this->m_puthead['User-Agent'] ) )
{
$this->m_puthead['User-Agent'] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2)";
}
if ( !isset( $this->m_puthead['Refer'] ) )
{
$this->m_puthead['Refer'] = "http://".$this->m_puthead['Host'];
}
foreach ( $this->m_puthead as $k => $v )
{
$k = trim( $k );
$v = trim( $v );
if ( !( $k != "" ) && !( $v != "" ) )
{
$headString .= "{$k}: {$v}\r\n";
}
}
//echo $this->m_fp;
fputs( $this->m_fp, $headString );
if ( $requestType == "POST" )
{
$postdata = "";
if ( 1 < count( $ps ) )
{
for($i = 1;$i < count( $ps );++$i)
{
$postdata .= $ps[$i];
//break;
}
}
else
{
$postdata = "OK";
}
$plen = strlen( $postdata );
fputs( $this->m_fp, "Content-Type: application/x-www-form-urlencoded\r\n" );
fputs( $this->m_fp, "Content-Length: ".$plen."\r\n" );
//发送固定的结束请求头
//HTTP1.1协议必须指定文档结束后关闭链接,否则读取文档时无法使用feof判断结束
if ( $httpv == "HTTP/1.1" )
{
fputs( $this->m_fp, "Connection: Close\r\n\r\n" );
}
else
{
fputs( $this->m_fp, "\r\n" );
}
if ( $requestType == "POST" )
{
fputs( $this->m_fp, $postdata );
}
//获取应答头状态信息
$httpstas = explode( " ", fgets( $this->m_fp, 256 ) );
$this->m_httphead['http-edition'] = trim( $httpstas[0] );
$this->m_httphead['http-state'] = trim( $httpstas[1] );
$this->m_httphead['http-describe'] = "";
$i = 2;
for ( ; $i < count( $httpstas ); ++$i )
{
$this->m_httphead['http-describe'] .= " ".trim( $httpstas[$i] );
}
//获取详细应答头
while ( !feof( $this->m_fp ) )
{
$line = trim( fgets( $this->m_fp, 256 ) );
if ( $line == "" )
{
break;
}
$hkey = "";
$hvalue = "";
$v = 0;
$i = 0;
for ( ; $i < strlen( $line ); ++$i )
{
if ( $v == 1 )
{
$hvalue .= $line[$i];
}
if ( $line[$i] == ":" )
{
$v = 1;
}
if ( $v == 0 )
{
$hkey .= $line[$i];
}
}
$hkey = trim( $hkey );
if ( $hkey != "" )
{
$this->m_httphead[strtolower( $hkey )] = trim( $hvalue );
}
}
//如果连接被不正常关闭,重试
if ( feof( $this->m_fp ) )
{
if ( 10 < $this->reTry )
{
return false;
}
$this->privatestartsession( $requestType );
}
//判断是否是3xx开头的应答
if ( ereg( "^3", $this->m_httphead['http-state'] ) )
{
if ( 3 < $this->JumpCount )
{
return;
}
if ( isset( $this->m_httphead['location'] ) )
{
$newurl = $this->m_httphead['location'];
if ( eregi( "^http", $newurl ) )
{
$this->jumpopenurl( $newurl );
}
else
{
$newurl = $this->fillurl( $newurl );
$this->jumpopenurl( $newurl );
}
}
else
{
$this->m_error = "无法识别的答复!";
}
}
}
//初始化系统
function privateinit( $url )
{
if ( $url == "" )
{
return;
}
//echo $url;
$urls = "";
$urls = @parse_url( $url );
$this->m_url = $url;
if ( is_array( $urls ) )
{
$this->m_host = $urls['host'];
//echo $this->m_host;
if ( !empty( $urls['scheme'] ) )
{
$this->m_scheme = $urls['scheme'];
}
if ( !empty( $urls['user'] ) )
{
$this->m_user = $urls['user'];
}
if ( !empty( $urls['pass'] ) )
{
$this->m_pass = $urls['pass'];
}
if ( !empty( $urls['port'] ) )
{
$this->m_port = $urls['port'];
}
if ( !empty( $urls['path'] ) )
{
$this->m_path = $urls['path'];
}
$this->m_urlpath = $this->m_path;
if ( !empty( $urls['query'] ) )
{
$this->m_query = $urls['query'];
$this->m_urlpath .= "?".$this->m_query;
}
print_r($urls['query']);
$this->HomeUrl = $urls['host'];
$this->BaseUrlPath = $this->HomeUrl.$urls['path'];
$this->BaseUrlPath = preg_replace( "/\\/([^\\/]*)\\.(.*)\$/", "/", $this->BaseUrlPath );
$this->BaseUrlPath = preg_replace( "/\\/\$/", "", $this->BaseUrlPath );
//echo $this->BaseUrlPath;gwww.golffactorystore.com/images/200910/goods_img
}
}
获取远程图片的函数用如下,调用的时候传递过去的也是有效,但是发现以下两句为空,无法获取
$htd->openurl( $url );$htd->gethead( "content-type" )以上2句调用为空。
function getremoteimage( $url, $rndtrueName )
{
// echo $url;
$revalues = array( );
$ok = false;
$htd = new dedehttpdown( );
$htd->openurl( $url );
//echo $htd->openurl( $url ); $sparr = array( "image/pjpeg", "image/jpeg", "image/gif", "image/jpg", "image/png", "image/xpng", "image/wbmp" );
if ( !in_array( $htd->gethead( "content-type" ), $sparr ) )
{
echo "1";
return "";
}
make_dir( dirname( $rndtrueName ) );
$itype = $htd->gethead( "content-type" );
$ok = $htd->savetobin( $rndtrueName );
if ( $ok )
{
$data = getimagesize( $rndtrueName );
$revalues[0] = $rndtrueName;
list( , $revalues[2], $revalues[2] ) = $data;
}
$htd->close( );
if ( $ok )
{
return $revalues;
}
return "";
}
//用Http协议下载文件
function savetobin( $savefilename )
{
if ( !$this->isgetok( ) )
{
return false;
}
if ( @feof( $this->m_fp ) )
{
$this->m_error = "连接已经关闭!";
return false;
}
$fp = fopen( $savefilename, "w" );
while ( !feof( $this->m_fp ) )
{
fwrite( $fp, fread( $this->m_fp, 1024 ) );
}
fclose( $this->m_fp );
fclose( $fp );
return true;
} //打开指定网址
function openurl( $url, $requestType = "GET" )
{
$this->resetany( );
$this->JumpCount = 0;
$this->m_httphead = array( );
//print_r($this->m_httphead);
/*
$extend =explode(\".\" , $url);
$va=count($extend)-1;
$extend[$va];
$this->m_httphead[""]=
*/
$this->m_html = "";
$this->reTry = 0;
$this->close( );
//初始化系统
$this->privateinit( $url );
$this->privatestartsession();
} //开始HTTP会话
function privatestartsession( $requestType = "GET" )
{
if ( !$this->privateopenhost( ) )
{
$this->m_error .= "打开远程主机出错!";
return false;
}
++$this->reTry;
if ( $this->gethead( "http-edition" ) == "HTTP/1.1" )
{
$httpv = "HTTP/1.1";
}
else
{
$httpv = "HTTP/1.0";
}
$ps = explode( "?", $this->m_urlpath );
//echo $this->m_urlpath;/images/200910/goods_img/599_P_1255757135855.jpg
//print_r($ps);Array([0] => /images/200910/goods_img/599_P_1255757135855.jpg)
$headString = "";
//发送固定的起始请求头GET、Host信息
if ( $requestType == "GET" )
{
$headString .= "GET ".$this->m_urlpath.( " ".$httpv."\r\n" );
}
else
{
$headString .= "POST ".$ps[0].( " ".$httpv."\r\n" );
}
$this->m_puthead['Host'] = $this->m_host;
//发送用户自定义的请求头
if ( !isset( $this->m_puthead['Accept'] ) )
{
$this->m_puthead['Accept'] = "*/*";
}
if ( !isset( $this->m_puthead['User-Agent'] ) )
{
$this->m_puthead['User-Agent'] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2)";
}
if ( !isset( $this->m_puthead['Refer'] ) )
{
$this->m_puthead['Refer'] = "http://".$this->m_puthead['Host'];
}
foreach ( $this->m_puthead as $k => $v )
{
$k = trim( $k );
$v = trim( $v );
if ( !( $k != "" ) && !( $v != "" ) )
{
$headString .= "{$k}: {$v}\r\n";
}
}
//echo $this->m_fp;
fputs( $this->m_fp, $headString );
if ( $requestType == "POST" )
{
$postdata = "";
if ( 1 < count( $ps ) )
{
for($i = 1;$i < count( $ps );++$i)
{
$postdata .= $ps[$i];
//break;
}
}
else
{
$postdata = "OK";
}
$plen = strlen( $postdata );
fputs( $this->m_fp, "Content-Type: application/x-www-form-urlencoded\r\n" );
fputs( $this->m_fp, "Content-Length: ".$plen."\r\n" );
//发送固定的结束请求头
//HTTP1.1协议必须指定文档结束后关闭链接,否则读取文档时无法使用feof判断结束
if ( $httpv == "HTTP/1.1" )
{
fputs( $this->m_fp, "Connection: Close\r\n\r\n" );
}
else
{
fputs( $this->m_fp, "\r\n" );
}
if ( $requestType == "POST" )
{
fputs( $this->m_fp, $postdata );
}
//获取应答头状态信息
$httpstas = explode( " ", fgets( $this->m_fp, 256 ) );
$this->m_httphead['http-edition'] = trim( $httpstas[0] );
$this->m_httphead['http-state'] = trim( $httpstas[1] );
$this->m_httphead['http-describe'] = "";
$i = 2;
for ( ; $i < count( $httpstas ); ++$i )
{
$this->m_httphead['http-describe'] .= " ".trim( $httpstas[$i] );
}
//获取详细应答头
while ( !feof( $this->m_fp ) )
{
$line = trim( fgets( $this->m_fp, 256 ) );
if ( $line == "" )
{
break;
}
$hkey = "";
$hvalue = "";
$v = 0;
$i = 0;
for ( ; $i < strlen( $line ); ++$i )
{
if ( $v == 1 )
{
$hvalue .= $line[$i];
}
if ( $line[$i] == ":" )
{
$v = 1;
}
if ( $v == 0 )
{
$hkey .= $line[$i];
}
}
$hkey = trim( $hkey );
if ( $hkey != "" )
{
$this->m_httphead[strtolower( $hkey )] = trim( $hvalue );
}
}
//如果连接被不正常关闭,重试
if ( feof( $this->m_fp ) )
{
if ( 10 < $this->reTry )
{
return false;
}
$this->privatestartsession( $requestType );
}
//判断是否是3xx开头的应答
if ( ereg( "^3", $this->m_httphead['http-state'] ) )
{
if ( 3 < $this->JumpCount )
{
return;
}
if ( isset( $this->m_httphead['location'] ) )
{
$newurl = $this->m_httphead['location'];
if ( eregi( "^http", $newurl ) )
{
$this->jumpopenurl( $newurl );
}
else
{
$newurl = $this->fillurl( $newurl );
$this->jumpopenurl( $newurl );
}
}
else
{
$this->m_error = "无法识别的答复!";
}
}
}
//初始化系统
function privateinit( $url )
{
if ( $url == "" )
{
return;
}
//echo $url;
$urls = "";
$urls = @parse_url( $url );
$this->m_url = $url;
if ( is_array( $urls ) )
{
$this->m_host = $urls['host'];
//echo $this->m_host;
if ( !empty( $urls['scheme'] ) )
{
$this->m_scheme = $urls['scheme'];
}
if ( !empty( $urls['user'] ) )
{
$this->m_user = $urls['user'];
}
if ( !empty( $urls['pass'] ) )
{
$this->m_pass = $urls['pass'];
}
if ( !empty( $urls['port'] ) )
{
$this->m_port = $urls['port'];
}
if ( !empty( $urls['path'] ) )
{
$this->m_path = $urls['path'];
}
$this->m_urlpath = $this->m_path;
if ( !empty( $urls['query'] ) )
{
$this->m_query = $urls['query'];
$this->m_urlpath .= "?".$this->m_query;
}
print_r($urls['query']);
$this->HomeUrl = $urls['host'];
$this->BaseUrlPath = $this->HomeUrl.$urls['path'];
$this->BaseUrlPath = preg_replace( "/\\/([^\\/]*)\\.(.*)\$/", "/", $this->BaseUrlPath );
$this->BaseUrlPath = preg_replace( "/\\/\$/", "", $this->BaseUrlPath );
//echo $this->BaseUrlPath;gwww.golffactorystore.com/images/200910/goods_img
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货