php
  • curl
  • 2013-06-04 27 views 4 likes 
    4

    我使用此代码在php中获取图像大小,并且它正在为我完美工作。通过CURL在PHP中获取图像大小

    $img = get_headers("http://ultoo.com/img_single.php", 1); 
    $size = $img["Content-Length"]; 
    echo $size; 
    

    但是如何通过CURL来获取? 我试过但没有效果。

    $url = 'http://ultoo.com/img_single.php'; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_HEADER, 1); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, "Content-Length"); 
    //curl_setopt($ch, CURLOPT_NOBODY, 1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $result = curl_exec($ch); 
    
    $filesize = $result["Content-Length"]; 
    
        curl_close($ch); 
    
        echo $filesize; 
    
    +0

    它不重复,当我在给定的链接上使用给定的代码时,我在Result中得到-1 。 – user2424807

    回答

    0

    我碰到在此之前,脚本我以前可以在这里找到 - >http://boolean.co.nz/blog/curl-remote-filesize/638/。与上面的帖子非常相似,但更直接一点,也不太容易理解。

    +0

    第10行的/home/dotmamat/public_html/d/dd/t1.php中出现语法错误,意外的'?',期待')' – user2424807

    +0

    如果您做了直接复制粘贴,请确保所有字符都正确,如双引号是“真实”的双引号。除此之外,不知道哪里出现了“意外?”会来自。该函数的所有支架都匹配。 –

    1

    设置curl_setopt($ch, CURLOPT_HTTPHEADER, true);,然后print_r($result),你会看到类似

    HTTP/1.1 200 OK 
    Date: Tue, 04 Jun 2013 03:12:38 GMT 
    Server: Apache/2.2.15 (Red Hat) 
    X-Powered-By: PHP/5.3.3 
    Set-Cookie: PHPSESSID=rtd17m2uig3liu63ftlobcf195; path=/ 
    Expires: Thu, 19 Nov 1981 08:52:00 GMT 
    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
    Pragma: no-cache 
    Content-Length: 211 
    Content-Type: image/png 
    

    我不认为Content-Length是获取图像的大小以正确的方式,因为我得到不同的结果curlget_header

    0

    之间也许这适用于你(假设页面总是返回一个img/png) - 我包含一个“写入文件”,只是为了能够比较屏幕输出和文件大小(页面中的png):

    $url = 'http://ultoo.com/img_single.php'; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //return the output as a variable 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); //time out length 
    $data = curl_exec($ch); 
    if (!$data) { 
        echo "<br />cURL error:<br/>\n"; 
        echo "#" . curl_errno($ch) . "<br/>\n"; 
        echo curl_error($ch) . "<br/>\n"; 
        echo "Detailed information:"; 
        var_dump(curl_getinfo($ch)); 
        die(); 
    } 
    curl_close($ch); 
    $handle = fopen("image.png", "w"); 
    fwrite($handle, $data); 
    fclose($handle); 
    $fileSize = strlen($data); 
    echo "fileSize = $fileSize\n"; 
    
    相关问题