標題:使用dump顯示Google Plus的個人資料
HTML (index.html):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-TW" xml:lang="zh-TW">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Jsonp dynamic</title>
<script type="text/javascript">
function googleplus(json) {
document.getElementById("gplus").innerHTML = dump(json);
}
function getGplus() {
var removeScript = document.getElementById('jsonScript');
if(removeScript)
removeScript.parentNode.removeChild(removeScript);
var uid = document.getElementById('uid').value;
var key = "Your key"; //Get your Google Plus API KEY
var script = document.createElement('script');
script.type = 'text/javascript';
script.id = 'jsonScript';
script.src = "https://www.googleapis.com/plus/v1/people/"+uid+"?key="+key+"&callback=googleplus";
//script.src = "https://www.googleapis.com/plus/v1/people/"+uid+"/activities/public?key="+key+"&callback=googleplus";
document.getElementsByTagName('head')[0].appendChild(script);
}
function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += " ";
if(typeof(arr) == 'object') {
for(var item in arr) {
var value = arr[item];
if(typeof(value) == 'object') {
dumped_text += level_padding + "'" + item + "' ...\n";
dumped_text += dump(value,level+1);
} else {
dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
}
}
} else {
dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
}
</script>
</head>
<body>
<input type="text" id="uid" value="109023257393424023484" />
<input type="button" onClick="getGplus()" value="Get" />
<pre id="gplus"></pre>
</body>
</html>
範例:
說明:
請先
取得Google Plus API Key並填入Your key,
使用dump讓JavaScript也有PHP print_r的功能,並使用jsonp取得Google Plus的個人資料。
參考:
http://www.openjs.com/scripts/others/dump_function_php_print_r.php