在新浪雲上建立了一個雲端應用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>
我想大声告诉你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 ?>" />