-1

我试图通过 cURL 获取图像来显示图像,但没有显示任何内容。标头已正确发送,所以我接下来应该看哪里?

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

    //$imgurl = $_GET['img'];

    $query = curl_init();
    $headers[] = 'Cache-Control: maxage=. $cache_expire';
    $headers[] = 'Pragma: public';
    $headers[] = 'Accept-Encoding: None';
    //curl_setopt($query, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($query, CURLOPT_URL, "http://static4.fjcdn.com/comments/3885449+_bf7680ea243464ace7ddb3d912c7c7d6.jpg");
    curl_setopt($query, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
    curl_setopt($query, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($query, CURLOPT_HEADER, 1);
    $ret = curl_exec($query);
    if(!preg_match("/Content-Type\: image(.*)\r\n/i", $ret, $mime))
    die("die potato!");
    header($mime[0]);
    echo substr(strstr($ret, "\r\n\r\n"), 4);
?>
4

2 回答 2

1

你为什么要把事情复杂化?这种方式怎么样:

$img_url = 'http://static4.fjcdn.com/comments/3885449+_bf7680ea243464ace7ddb3d912c7c7d6.jpg';
$content = file_get_contents( $img_url );
header('Content-Type: image/png');
echo $content;
于 2013-01-17T14:41:47.880 回答
0

curl_setopt($query, CURLOPT_HEADER, 1);从您的代码中删除。CURLOPT_HEADER在响应中包含响应标头。这将使您的图像格式错误。

以下代码可以正常工作。

function curlHeaderCallback($query, $header){
    if(stripos($header, "content-type")!==false){
        header(trim($header));
    }
    return strlen($header); 
}

$query = curl_init("http://bd1.php.net/images/php.gif");
curl_setopt($query, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt($query, CURLOPT_HEADERFUNCTION, 'curlHeaderCallback'); 
curl_setopt($query, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($query);
echo $ret;
于 2013-01-17T14:42:11.700 回答