Home  >  Q&A  >  body text

新浪SAE php能够获取微信的头像,但是在本地运行代码获取不到?

在新浪云上建立了一个云应用SAE php标准环境,PHP获取微信的头像图片转换成base64字符串在网页上显示出来,在新浪云上代码正常执行,但是在本地用 WAMPSERVER 3.0.6 64bit启动的服务代码就报错,手欠把新浪云应用关闭了,成功页面无法贴出来,在此贴一下主要代码

其他服务器上的图片可以正常显示,只有微信图片的在本地服务器报错,新浪云上都正常显示

网上的说 完美解决failed to open stream: HTTP request failed!文章已经试过,完全无效,刚学习php,希望同行帮忙解答一下,拜谢

<?php  
$pic = 'http://wx.qlogo.cn/mmopen/1MLz0YkS76Fs13hv8TH9oafOPW6kE7Hfv0QNEYvacxw8loRwFLMUsypFVVsiaj10TkqLugcCcKZNFysmJD0PwBQ/0';
$arr = getimagesize($pic);
$pic = "data:{$arr['mime']};base64," . base64_encode(file_get_contents($pic));
$url='https://api.myjson.com/bins/w7ou';
$html = file_get_contents($url);
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>测试</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">
</head>
<body>
<img src="<?php echo $pic ?>" />
<p><?php echo $html ?></p>
<script>
    var t = <?php echo "'$pic'"?>;
    console.log(t);
</script> 
</body>
</html>

天蓬老师天蓬老师2737 days ago643

reply all(1)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-16 13:13:20

    You have failed to request the WeChat avatar file. It is recommended to use curl to obtain the remote image, and then judge whether the acquisition is successful based on the return value. After success, use the gd function to process the avatar.

    <?php
    
    $url = 'http://wx.qlogo.cn/mmopen/1MLz0YkS76Fs13hv8TH9oafOPW6kE7Hfv0QNEYvacxw8loRwFLMUsypFVVsiaj10TkqLugcCcKZNFysmJD0PwBQ/0';
    
    $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_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) {
        $imgBase64Code = "data:image/jpeg;base64," . base64_encode($data);
    }
    
    ?>
    
    <img src="<?php echo $imgBase64Code ?>" />

    reply
    0
  • Cancelreply