php下载 微信头像图片_php保存微信用户头像到本地或者服务器的完美方案!
//方法一://推薦用該方法
$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',);
$url='http://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKkGpNuUhaBniatRsiaG7ksqmhUWzkk40kTRS6icQS7kJcsfxcibQo7vDFcKibr7NHb9YIXiaXsEtLcdL6A/0';
$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_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 = "./cs/cs.{$type}";
if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $img_content))))
{ echo '新文件保存成功:', $new_file; }}
//方法二://該方法比較消耗服務器資源,慎用!
$url='http://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKkGpNuUhaBniatRsiaG7ksqmhUWzkk40kTRS6icQS7kJcsfxcibQo7vDFcKibr7NHb9YIXiaXsEtLcdL6A/0';
$img_file = file_get_contents($url); $img_content= base64_encode($img_file);
$type = 'jpeg';//得到圖片類型png?jpg?gif? $new_file = "./cs/cs.{$type}";
if (file_put_contents($new_file, base64_decode($img_content)))
{
echo 'ok', $new_file;
}
?>
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的php下载 微信头像图片_php保存微信用户头像到本地或者服务器的完美方案!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python列表、字典、元组的用法及区别
- 下一篇: php的在线问卷调查_基于php技术的问