php抓取微信文章图片保存到本地

需要注意的是微信文章的图片是webp格式的,废话不说上代码.


header("Content-type: text/html; charset=utf-8");
$url = 'http://mmbiz.qpic.cn/mmbiz_jpg/icB0yCLh6LJvy91l2dEz2LRROWF2R12sJNvqDyexhaib9rzRhjbpdWRy0tWykbjfxYSXfYlLDAVOMCMqqctdacMA/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1';
$url = 'http://mmbiz.qpic.cn/mmbiz_gif/PvKKmEwSMcHJTKMQZttfUADBuqIYNjicle24FHvTvKPZZ4wkRicibaT1gHg99xJJ8OwKjL27IHliag9BSMyHCg9HZw/0?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1';
$url = 'http://mmbiz.qpic.cn/mmbiz/cjodwNbMYa3NgcUQ9r5J060pSLKsRMHZahvvd7c0ghxHo2INadzc2bBfibUicd5mAnyXUGqMaId0uL3pj5dTrA4Q/0?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1';
$url = 'http://mmbiz.qpic.cn/mmbiz_png/lnZQE7bzTT3K4D0GyAU57qhXyaJtiaXyageBpT8qbY8DgchGmOtye53hDwG9987Ze7hBurA4H13q2jyqll9b7PQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1';


function downloadImageFromWeixin($url)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_NOBODY, 0); // 只取body头
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $package = curl_exec($ch);
    $httpinfo = curl_getinfo($ch);
    
    curl_close($ch);
    $imageAll = array_merge(array(
        'imgBody' => $package
    ), $httpinfo);
    return $imageAll;
}
$imageAll = downloadImageFromWeixin($url);
echo '

';
var_dump($imageAll);
// 保存图像
if ($imageAll['content_type'] == 'image/gif') {
    file_put_contents('php.gif', $imageAll["imgBody"]);
} elseif ($imageAll['content_type'] == 'image/webp') {
    file_put_contents('php.webp', $imageAll["imgBody"]);
    $im = imagecreatefromwebp('./php.webp');
    imagejpeg($im, './example.jpg', 100);
    imagedestroy($im);
}




























你可能感兴趣的:(php)