从外部源PHP下载图像不在服务器

时间:2016-02-20 11:17:03

标签: php curl

我无法使用服务器中的curl,php从外部源下载图像(仅限某些图像)。

  

提供“未能打开流:连接超时”错误

function getHeaders($url)
{
   $ch = curl_init($url);
   curl_setopt( $ch, CURLOPT_NOBODY, true );
   curl_setopt( $ch, CURLOPT_RETURNTRANSFER, false );
   curl_setopt( $ch, CURLOPT_HEADER, false );
  curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
  curl_setopt( $ch, CURLOPT_MAXREDIRS, 3 );
  curl_exec( $ch );
  $headers = curl_getinfo( $ch );
  curl_close( $ch );    
  return $headers;
}

    function download($url, $path)
{
  # open file to write
  $fp = fopen ($path, 'w+');
  # start curl
  $ch = curl_init();
  curl_setopt( $ch, CURLOPT_URL, $url );
  # set return transfer to false
  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, false );
  curl_setopt( $ch, CURLOPT_BINARYTRANSFER, true );
  curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
  # increase timeout to download big file
  curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
  # write data to local file
  curl_setopt( $ch, CURLOPT_FILE, $fp );
  # execute curl
  curl_exec( $ch );
  # close curl
  curl_close( $ch );
  # close local file
  fclose( $fp );

  if (filesize($path) > 0) return true;
}

$url = 'http://81.144.197.80:8998/image-bb?imageid=190590';
$path = 'uploads/file.jpg';

$headers = getHeaders($url);

if ($headers['http_code'] === 200 and $headers['download_content_length'] < 1024*1024) {
  if (download($url, $path)){
    echo 'Download complete!'; 
  }
}

我可以从localhost

下载

请帮帮我。

提前致谢。

0 个答案:

没有答案
相关问题