search

Home  >  Q&A  >  body text

Sina SAE php can get the WeChat avatar, but it cannot be obtained by running the code locally?

A cloud application SAE PHP standard environment was established on Sina Cloud. PHP obtains the WeChat avatar image and converts it into a base64 string for display on the web page. The code executes normally on Sina Cloud, but WAMPSERVER 3.0.6 is used locally. The service code started by 64bit reports an error. I have closed the Sina Cloud application and the success page cannot be posted. I will post the main code here

Pictures on other servers can be displayed normally, only WeChat pictures report an error on the local server, but they are displayed normally on Sina Cloud

It is said on the Internet that it is the perfect solution to failed to open stream: HTTP request failed! The article has been tried and it is completely ineffective. I just learned PHP. I hope my colleagues can help me with the answer. Thank you

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

< /p>

天蓬老师天蓬老师2751 days ago662

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