PHP函数getimagesize()给出了“;读取错误“;尝试获取https url时


PHP function getimagesize() gives "Read error" when trying to get https url

我正在尝试将getimagesize与URL和http一起使用,一切都很好。然而,当尝试在https url上使用函数时,我收到了"读取错误"通知,结果是false。我检查了一下,服务器上安装了OpenSSL 0.98(所以它应该也能使用https)。我知道我可以先下载图像,然后使用它,但在我看来,这应该有效,我错过了一些东西。你能给我一些解决方案吗(除了先下载图像然后打开它)?

提前谢谢。

您可以使用file_get_contents()作为备用解决方案。。

<?php
$filename='something';
file_put_contents($filename,file_get_contents($url));
$size = getimagesize($filename);
var_dump($size);

在不更改SSL的任何设置时,使用file_get_contents()只能转移问题,而不能解决问题。但您可以通过stream_context_create然后篡改SSL设置来实现这一点。一旦你做到了这一点,你就可以通过SSL想要保护你免受的攻击。

<?php
error_reporting( E_ALL );
header( 'Content-type: text/plain' );
// The remote file to check
$sPic= 'https://picture.to/check/for/details.png';
// Does it work right away?
$aInfo= @getimagesize( $sPic );
if( $aInfo=== FALSE ) {
    // No. Then a temporary file needs to be created.
    if( $sTmp= tempnam( sys_get_temp_dir(), 'sig' ) ) {
        // Tweaking the HTTPS options to weaken/ignore security
        $hCtx= stream_context_create
        ( array
            ( 'ssl'=> array
                ( 'verify_peer'=> FALSE  // Certificate verification
                , 'verify_peer_name'=> FALSE
                , 'allow_self_signed'=> TRUE
                , 'SNI_enabled'=> TRUE  // Multiple certificates on same IP address
                )
            )
        );
        // Download file
        $sPayload= file_get_contents( $sPic, FALSE, $hCtx );
        if( $sPayload!== false ) {
            // Success: write to temporary file
            if( file_put_contents( $sTmp, $sPayload ) ) {
                $aInfo= @getimagesize( $sTmp );
            } else die( 'Could not write to tempfile!' );
        } else die( 'Could not download file!' );
        // Delete temporary file
        unlink( $sTmp );
    } else die( 'Could not get tempfile name!' );
}
// Picture file details
print_r( $aInfo );

更新OpenSSL可以解决您的问题

从您报告的服务器上的OpenSSL版本来看,此问题可能是由于服务器的SSL版本比您的客户端更新所致。

Facebook服务器可能使用的是>=1.0.0版本或自定义SSL库,而您使用的是旧的0.9.8版本。

检测信号溢出问题迫使许多Web服务器更新其OpenSSL版本

一篇关于OpenSSL 1.0.0与使用0.9.8版本的客户端握手问题的随机文章:

https://groups.google.com/forum/#!topic/msysgit/jSOTOQXPnwU