suchen

Heim  >  Fragen und Antworten  >  Hauptteil

新浪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>

天蓬老师天蓬老师2751 Tage vor663

Antworte allen(1)Ich werde antworten

  • 我想大声告诉你

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

    你这是请求微信头像文件失败了,建议用 curl 获取远程图片,然后根据返回值判断是否获取成功,成功以后再用 gd 函数处理头像。

    <?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 ?>" />

    Antwort
    0
  • StornierenAntwort