PHP显示图像(SSL PROXY)

时间:2013-01-14 01:58:54

标签: php ssl proxy

我正在尝试创建一个接受URL并显示它的PHP脚本(尝试在我的项目中保留SSL)

但是,我无法使用我的脚本输出任何内容。没有错误显示,我不知所措。我没看到什么?

<?php

//if parameter is empty
if((!isset($_GET['img'])) or ($_GET['img'] == '') or (!isset($per['scheme'])))
{
    exit;
}

$per = parse_url($_GET['img']);
print_r ($per);
$imgurl = $_GET['img'];
print_r ($imgurl);
$imgurl = str_replace(' ', "%20", $imgurl);

$aFile = getimagesize($imgurl);
print_r ($aFile);

//check file extension
if($aFile == 'jpg' or $aFile == 'jpeg'){
        header('Content-Type: image/jpeg');
        imagejpeg(getimg($file));
} elseif($aFile == 'png') {
        header('Content-Type: image/png');
        imagepng(getimg($file));
} elseif($aFile == 'gif') {
        header('Content-Type: image/gif');
        imagegif(getimg($file));
} else {
        die('not supported');
}

$imgurl = $_GET['img'];
$imgurl = str_replace(' ', "%20", $imgurl);

function getimg($imageurl) {
    $cache_expire = 60*60*24*365;
    $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg, image/png';
    $headers[] = 'Cache-Control: maxage=. $cache_expire';
    $headers[] = 'Pragma: public';
    $headers[] = 'Accept-Encoding: None';
    $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
    $user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6';
    $process = curl_init($imageurl);
    curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($process, CURLOPT_REFERER, $_GET['img']);
    curl_setopt($process, CURLOPT_HEADER, 0);
    curl_setopt($process, CURLOPT_HTTPGET, true);
    curl_setopt($process, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($process, CURLOPT_TIMEOUT, 30);
    curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
    $return = curl_exec($process);
    curl_close($process);
    return $return;
}
exit;
?>

以下是脚本的编辑版本,当我使用readfile时输出乱码文本:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

//if parameter is empty
if((!isset($_GET['img'])) or ($_GET['img'] == ''))
{
    exit;
}

$per = parse_url($_GET['img']);
$imgurl = $_GET['img'];
$imgurl = str_replace(' ', "%20", $imgurl);

$aFile = getimagesize($imgurl);

//check file extension
$checkmime = getimagesize($imgurl);

if($checkmime['mime'] != 'image/png' && $checkmime['mime'] != 'image/gif' && $checkmime['mime'] != 'image/jpeg') {
    die('not supported');
} else {
    readfile("$imgurl");
}

function getimg($imageurl) {
    $cache_expire = 60*60*24*365;
    $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg, image/png';
    $headers[] = 'Cache-Control: maxage=. $cache_expire';
    $headers[] = 'Pragma: public';
    $headers[] = 'Accept-Encoding: None';
    $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
    $user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6';
    $process = curl_init($imageurl);
    curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($process, CURLOPT_REFERER, $_GET['img']);
    curl_setopt($process, CURLOPT_HEADER, 0);
    curl_setopt($process, CURLOPT_HTTPGET, true);
    curl_setopt($process, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($process, CURLOPT_TIMEOUT, 30);
    curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
    $return = curl_exec($process);
    curl_close($process);
    return $return;
}
exit;
?>

1 个答案:

答案 0 :(得分:0)

如果这真的是您的整个脚本,那么问题首先在于$per['scheme']未被设置,因此它会触发第一个if()语句并使用脚本exit

您检查是否未在此处设置:!isset($per['scheme'])因此它将生成表达式TRUE并因此结束脚本,因此甚至不会从print_r();

输出结果

请改用:

if (empty($_GET['img'])) // Check if $_GET['img'] is not set AND is = '' (empty)
{
    exit;
}

使用:

} else {
    header('Content-Type: ' . $checkmime['mime']);
    readfile($imgurl);
}