Home >Backend Development >PHP Tutorial >微信开发 - php下载网络图片问题,在本地测试成功,服务器上下载回来的文件只有100多字节

微信开发 - php下载网络图片问题,在本地测试成功,服务器上下载回来的文件只有100多字节

WBOY
WBOYOriginal
2016-06-06 20:19:211519browse

<code>function getUrlImg2($url,$filename=""){
    if(!createDirs(dirname($filename))){
        die('无法创建文件夹');
    }
    if(!$url)
        die('无url');

    ob_start();
    readfile($url);
    $img = ob_get_contents();
    ob_end_clean();
    $size = strlen($img);
    if($size){
        $fp2=@fopen($filename, "a");
        fwrite($fp2,$img);
        fclose($fp2);

        return $filename;
    }else
        return false;
}
function getUrlImg3($url,$filename=""){
    if(!createDirs(dirname($filename))){
        die('无法创建文件夹');
    }
    if(!$url)
        die('无url');

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_NOBODY, 0);    //只取body头
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $package = curl_exec($ch);
    $httpinfo = curl_getinfo($ch);
    curl_close($ch);
    $imageAll = array_merge(array('header' => $httpinfo), array('body' => $package));
    //上半部是下载,下半部是保存
    if($package){
        $local_file = fopen($filename, 'w');
        if (false !== $local_file){
            if (false !== fwrite($local_file, $package)){
                fclose($local_file);
                return true;
            }
        }
    }
    return false;
}
</code>

上面两个方法在本地测试都是可以的,但是放到服务器上,下载回来的图片都只有100多字节,图片还打不开。这个方法是用在向微信服务器获取图片用的,请问是不是php.ini里的相关设置造成的?改哪些设置可以弄好?

这个问题已被关闭,原因:原来是我本地调试的时候把access_token更新了,服务器上缓存的token就没用了,所以才会出现这个问题,更新了服务器的token就行了

回复内容:

<code>function getUrlImg2($url,$filename=""){
    if(!createDirs(dirname($filename))){
        die('无法创建文件夹');
    }
    if(!$url)
        die('无url');

    ob_start();
    readfile($url);
    $img = ob_get_contents();
    ob_end_clean();
    $size = strlen($img);
    if($size){
        $fp2=@fopen($filename, "a");
        fwrite($fp2,$img);
        fclose($fp2);

        return $filename;
    }else
        return false;
}
function getUrlImg3($url,$filename=""){
    if(!createDirs(dirname($filename))){
        die('无法创建文件夹');
    }
    if(!$url)
        die('无url');

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_NOBODY, 0);    //只取body头
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $package = curl_exec($ch);
    $httpinfo = curl_getinfo($ch);
    curl_close($ch);
    $imageAll = array_merge(array('header' => $httpinfo), array('body' => $package));
    //上半部是下载,下半部是保存
    if($package){
        $local_file = fopen($filename, 'w');
        if (false !== $local_file){
            if (false !== fwrite($local_file, $package)){
                fclose($local_file);
                return true;
            }
        }
    }
    return false;
}
</code>

上面两个方法在本地测试都是可以的,但是放到服务器上,下载回来的图片都只有100多字节,图片还打不开。这个方法是用在向微信服务器获取图片用的,请问是不是php.ini里的相关设置造成的?改哪些设置可以弄好?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn