
隔離 24 小時以後,我等等要出關了,爸爸說可以認識新朋友。

這是我的腳趾,一腳五隻。

我有深邃的雙眼皮,別愛上我,你會受傷。

雖然禾馨的阿姨們都對我很好,但天下無不散的筵席,今天我就要前往月子中心了,有緣再會。

禾馨的阿姨怕我曬到太陽,所以給了我一頂小帽子。

到了賀果以後我一樣盡小嬰兒應盡的義務,水~

我媽咪剛開始餵我吃奶,所以乳汁還不太夠,賀果的阿姨怕我餓壞讓我吃了一點配方奶,我知道不是媽媽的錯,媽媽加油,我先偷吃點心。
公司有一個 case 要在網頁模仿轉盤亮燈的功能,陽春的寫法如下。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Game</title>
<link rel="stylesheet" href="game.css">
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="game.js"></script>
</head>
<body>
<button type="button" id="start">start</button>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>
ul {
margin: 0;
padding: 0;
list-style: none;
}
li {
width: 100px;
height: 100px;
border: 2px solid red;
float: left;
margin: 2px;
}
.on {
border: 2px solid pink;
}
$(function() {
var start = $('#start');
var lis = $('li');
start.on('click', function() {
var rounds = 40 + Math.floor((Math.random() * lis.size()));
$(this).prop('disabled', true);
spin(lis.filter(':first'), rounds);
});
function spin(li, rounds) {
var duration = rounds < 5 ? 1000 : 70;
lis.clearQueue();
li.delay(duration).queue(function() {
var next = $(this).next('li');
lis.removeClass('on');
if (next.size() === 0) {
next = lis.filter(':first');
}
li.addClass('on');
if (rounds - 1 >= 0) {
spin(next, rounds -1);
} else {
start.prop('disabled', false);
}
});
}
});
接到一個 case 需要把圖片轉為 base64 傳出,順手寫了個 class 做掉這件事。
<?php
class ImageToBase64
{
public function encode64($source = null, $fileName = null)
{
$source = file_get_contents($source);
$type = pathinfo($fileName, PATHINFO_EXTENSION);
$content = 'data:image/' . $type . ';base64,' . base64_encode($source);
return $content;
}
public function decode64($source = null)
{
$content = null;
if (preg_match('/data:([^;]*);base64,(.*)/', $source, $matches)) {
header('Content-Type: '.$matches[1]);
$content = base64_decode($matches[2]);
}
return $content;
}
}
之前有提到 Facebook 以及 Google 使用 PHP API 登入的寫法(旋轉門),今天來講一下 Facebook JavaScript 端的用法,JavaScript 端為人所詬病的安全性問題,可以使用另外一個 PHP 呼叫 Facebook Graph Tool 的方式去取得值,下面來示範作法。
<input type="hidden" name="appId" id="appId" value="<?php echo $appId; ?>">
我不喜歡把 PHP 結果寫在 JavaScript 裡面,所以在 html 頁面放一個 JavaScript 去抓值。
function facebookLogin() {
FB.login(function(response) {
if (response.authResponse) {
FB.getLoginStatus(function(res) {
if (res.status === 'connected') {
accessToken = res.authResponse.accessToken;
$.get('next.php', {accessToken: accessToken}, function() {
// Do something
})
}
});
}
});
}
window.fbAsyncInit = function() {
FB.init({
appId: $('#appId').val(),
version: 'v2.3',
status: true, cookie: true,
xfbml: true
});
facebookLogin();
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
} (document, 'script', 'facebook-jssdk'));
這段 code 會自動觸發 facebookLogin function,如果想要按按鈕觸發的話,就是移除 window.fbAsyncInit 下的 facebookLogin(),然後把他 bind 到某個事件上。
<?php $accessToken = $_GET['accessToken']; $graphUrl = "https://graph.facebook.com/me?access_token=" . $accessToken; $data = json_decode(file_get_contents($graphUrl)); echo '<pre>'; var_dump($data); echo '</pre>';
PHP 頁面接到值後,可以利用這個 access token 去跟 graph api 拿資料,可以活多久我沒記,因為照理說這些事情應該要在瞬間處裡完成,透過這樣的方式就不用害怕對方攔截並修改你 JavaScript 的結果了。
這次 Faceook 規定 4/30 要升級到比較新的套件,Google 則是 4/20 以後要禁止使用 openid 2.0 登入,列一下 PHP 端製作的筆記。
首先你要下載 Facebook PHP 的 SDK,安裝方法有 composer 或直接下載,PHP 版本需求為 5.4+。
{
"require" : {
"facebook/php-sdk-v4" : "4.0.*"
}
}
https://github.com/facebookarchive/facebook-php-sdk
<?php
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;
$id = 'YOUR CLIENT ID';
$secret = 'YOUR SECRET';
$url = 'RETURN URL';
// Pemission scope, type is array
// reference url: https://developers.facebook.com/docs/facebook-login/permissions/v2.3
$permissions = 'YOUR PERMISSIONS SCOPE';
FacebookSession::setDefaultApplication($id, $secret);
$helper = new FacebookRedirectLoginHelper($url);
try {
if(isset($_SESSION['access_token']) === true) {
$session = new FacebookSession($_SESSION['access_token']);
} else {
$session = $helper->getSessionFromRedirect();
}
} catch(FacebookRequestException $ex) {
// When Facebook returns an error
$ex->getMessage();
} catch(\Exception $ex) {
// When validation fails or other local issues
$ex->getMessage();
}
if ($session) {
$_SESSION['access_token'] = $session->getToken();
// Get all scope id
$request = (new FacebookRequest($session, 'GET', '/me/ids_for_business'))->execute();
$idGroup = (array)$request->getGraphObject()->getProperty('data')->asArray();
// Get user info
$request = (new FacebookRequest($session, 'GET', '/me'))->execute();
$user = $request->getGraphObject()->asArray();
} else {
// Echo login url
echo $helper->getLoginUrl($permissions);
}
安裝方法一樣有兩種,composer 跟 github,PHP 要求為 5.2+。
"require": {
"google/apiclient": "1.0.*@beta"
}
https://github.com/google/google-api-php-client
<?php
$id = 'YOUR CLIENT ID';
$secret = 'YOUR SECRET';
$url = 'RETURN URL';
$client = new Google_Client();
$client->setClientId($id);
$client->setClientSecret($secret);
$client->setRedirectUri($url);
$client->setScopes(
array(
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile',
)
);
$service = new Google_Service_Oauth2($client);
if (isset($_GET['code']) === true) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['access_token']) === true) {
$client->setAccessToken($_SESSION['access_token']);
}
if ($client->getAccessToken()) {
// User info
$me = $service->userinfo->get();
} else {
$authUrl = $client->createAuthUrl();
// Login url
echo $authUrl;
}