php下载微信头像

php下载微信头像

<?php


$openid=99999;
$headimg = "https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoO3sVmQibfr5WvtAc9vIe4J8l2F0X94ibLicUBs5jKEjhybVKEXY1f1a9iaDgFxZcbvokcMiaCAKY9Syw/132";
$route="uploads/".date("Y-m-d",time());

/**
 * [down_wxhead description]
 * @param  [type] $url   图片链接
 * @param  [type] $route 下载路径
 * @param  [type] $filename 保存的文件名
 * @return [type]        [description]
 */
function down_wxhead($url,$route,$filename){
    //创建保存目录
    if(!is_dir($route))
    {
        mkdir($route);
    }
    $header = array('User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0', 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3', 'Accept-Encoding: gzip, deflate', );
    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    $data = curl_exec($curl);
    $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close($curl);
    if ($code == 200) { //把URL格式的图片转成base64_encode格式的!    
        $imgBase64Code = "data:image/jpeg;base64,".base64_encode($data);
    }
    $img_content = $imgBase64Code; //图片内容
    //echo $img_content;exit;
    if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $img_content, $result)) {
        $type = $result[2]; //得到图片类型png?jpg?gif? 
        $new_file = "$route/$filename.{$type}";
        if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $img_content)))) {
            // echo '新文件保存成功:', $new_file;
        }
    }   
}

down_wxhead($headimg,$route,$openid)
?>

php下载微信头像

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是杂牌军

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值