目前分類:PHP+MySQL+jQuery (4)

瀏覽方式: 標題列表 簡短摘要

此範例是參考http://blog.davidou.org/archives/423並配合Plurk測試工具寫成
作用在於取得目前噗浪使用者資料

plurk.php

<?php
set_time_limit(0);

function do_action($url, $parm_array, $new_parms=array())
{
global $oauth_token, $oauth_token_secret, $oauth_consumer_key, $oauth_consumer_secret;

$parm_array = array_merge($parm_array,$new_parms);
$base_string = sort_data($parm_array);
$base_string = "POST&".rawurlencode($url)."&".rawurlencode($base_string);

$key = rawurlencode($oauth_consumer_secret)."&".rawurlencode($oauth_token_secret);
$oauth_signature = rawurlencode(base64_encode(hash_hmac("sha1",$base_string,$key,true)));
$parm_array = array_merge($parm_array,array("oauth_signature" => $oauth_signature));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS,sort_data($parm_array));
curl_setopt($ch, CURLOPT_FAILONERROR,1);
$data = curl_exec($ch);

curl_close($ch);
return $data;
}

function sort_data($data)
{
ksort($data);
$string="";
foreach($data as $key=>$val)
{
if($string=="")
{
$string = $key."=".$val;
}
else
{
$string .= "&".$key."=".$val;
}
}
return $string;
}
?>

處理頁面

<?php

session_start();

include("plurk.php");

//初期參數
//oauth_token及oauth_token_secret沒有也先空著,不然驗證會出錯

$url = "http://www.plurk.com/OAuth/request_token";
$oauth_consumer_key = ""; //你的consumer_key talk
$oauth_consumer_secret = ""; //你的consumer_secret
$oauth_token = "";
$oauth_token_secret = "";

//Step.1----取得oauth_token及oauth_token_secret

//參數設置
$oauth_nonce = rand(10000000,99999999);
$oauth_timestamp = time();
$parm_array = array
(
"oauth_consumer_key" => $oauth_consumer_key,
"oauth_nonce" => $oauth_nonce,
"oauth_signature_method" => "HMAC-SHA1",
"oauth_timestamp" => $oauth_timestamp,
"oauth_version" => "1.0"
);

//函數
$post_info = do_action($url,$parm_array, array());

$array = explode('&', $post_info);
$array2 = [];
foreach($array as $v)
{
$temp = explode('=', $v);
$array2[] = $temp[1];
}

$oauth_token = $array2[1];
$oauth_token_secret = $array2[0];
$oauth_callback_confirmed = $array2[2];

$_SESSION["oauth_token"] = $oauth_token;
$_SESSION["oauth_token_secret"] = $oauth_token_secret;

//Step.2----取得授權
//將會導去設定在APP裡的callback頁面

$url = "http://www.plurk.com/OAuth/authorize?oauth_token=".$oauth_token;
header("Location: $url");

?>

callback頁面

 

<?php
session_start();

include("plurk.php");

//Step.3
//初期參數
$url = "http://www.plurk.com/OAuth/access_token";
$oauth_consumer_key = ""; //你的consumer_key talk
$oauth_consumer_secret = ""; //你的consumer_secret
$oauth_token = $_SESSION["oauth_token"];
$oauth_token_secret = $_SESSION["oauth_token_secret"];
$oauth_verifier = $_GET["oauth_verifier"];

 

$oauth_nonce = rand(10000000,99999999);
$oauth_timestamp = time();
$parm_array = array
(
"oauth_consumer_key" => $oauth_consumer_key,
"oauth_nonce" => $oauth_nonce,
"oauth_signature_method" => "HMAC-SHA1",
"oauth_timestamp" => $oauth_timestamp,
"oauth_version" => "1.0",
"oauth_token" => $oauth_token,
"oauth_verifier" => $oauth_verifier,
);

 

//函數
$post_info = do_action($url,$parm_array, array());

 

$array = explode('&', $post_info);
$array2 = [];
foreach($array as $v)
{
$temp = explode('=', $v);
$array2[] = $temp[1];
}

$oauth_token = $array2[1];
$oauth_token_secret = $array2[0];

$_SESSION["oauth_token"] = $oauth_token;
$_SESSION["oauth_token_secret"] = $oauth_token_secret;

//Step.4 //取得使用者資訊
$url = "http://www.plurk.com/APP/Users/me";

//函數
$oauth_nonce = rand(10000000,99999999);
$oauth_timestamp = time();
$parm_array = array
(
"oauth_consumer_key" => $oauth_consumer_key,
"oauth_nonce" => $oauth_nonce,
"oauth_signature_method" => "HMAC-SHA1",
"oauth_timestamp" => $oauth_timestamp,
"oauth_version" => "1.0",
"oauth_token" => $oauth_token,
"oauth_verifier" => $oauth_verifier,
);

$post_info = do_action($url,$parm_array, array());
$result = json_decode($post_info,TRUE);
?>
<pre><?php print_r($result); ?></pre>


如果已經取得oauth_token及oauth_token_secret就毋須再取得驗證,直接帶入oauth_token即可

<?php
header("Content-Type:text/html; charset=utf-8");
include("plurk.php");

//初期參數
$url = "http://www.plurk.com/APP/Users/me";
$oauth_consumer_key = "";
$oauth_consumer_secret = "";
$oauth_token = "";
$oauth_token_secret = "";

$oauth_nonce = rand(10000000,99999999);
$oauth_timestamp = time();
$parm_array = array
(
"oauth_consumer_key" => $oauth_consumer_key,
"oauth_nonce" => $oauth_nonce,
"oauth_signature_method" => "HMAC-SHA1",
"oauth_timestamp" => $oauth_timestamp,
"oauth_version" => "1.0",
"oauth_token" => $oauth_token,
);

//函數
$post_info = do_action($url,$parm_array, array());
$result = json_decode($post_info,TRUE);
?>
<pre>
<?php
if(!empty($result))
{
print_r($result);
}
else
{
?>
<script>
alert("驗證掉了喔!!需重新驗證。");
SetTimeOut();
</script>
<?php
}
?>
</pre>

 

七爺 發表在 痞客邦 留言(0) 人氣()

為了奇怪的要求,特定去查了一下...

方法一:

直接使用.scroll事件

缺點:當使用者點擊捲軸使其滾動時,程式仍會判斷該動作為scoll事件

$(window).scroll(function(){return false;})

 

方法二:

下載jquery.mousewheel

jquery.mousewheel為某程式設計師所寫的一個JS套件,可以只針對"滑鼠滾動"之事件做處理,而不影響點擊捲軸的動作。

HTML:<script src="../../scripts/jquery.mousewheel.js"></script>

JS:$(window).mousewheel(function(){return false;});

以上程式會停止捲軸的動作,使捲軸無法往下。

 

附註:

$('html,body').scrollTop(0); //html及body標籤底下的所有捲軸固定在最上方scrollTop(x),x為捲軸固定位置
$("html,body").animate({scrollTop:0},1000);  //此程式為依照設定的速度,將scrollTop歸至指定位置

七爺 發表在 痞客邦 留言(0) 人氣()

為了因應FaceBook在今年所做出的改版,爬了許多文,終於找出折衷的辦法.....

主要的困擾是FB在改版後,之後創建的APP將針對每一位使用者所使用的每一個APP給予不同的UID。

也就是說,以前FB使用者無論點擊哪一個APP,FB回傳的都是同一個UID。但是之後會變成一個使用者會因為不同的APP回傳不同的UID過來。

變得說無法用以前給的UID去跟新的APP做比對。

 

幸好,FB下面有個Business Manager(企業管理平台)。

它可以將你所指定的APP組成一個群組,再去使用FB API Ver2.0提供的程式碼做處理即可。

詳細請參閱FB說明:https://www.facebook.com/help/113163272211510/

Business API:https://developers.facebook.com/docs/apps/for-business/

 

PHP SDK

原本的程式為 $facebook -> api("/me/")

改成

$response = $facebook -> api("/me/ids_for_business");
$data = $response["data"];

其中,$data為一個陣列,陣列組合為使用者的ID+APP的資料

array() { [0]=>  { ["id"]=> string ["app"]=> array(3) { ["name"]=> string ["namespace"]=> string() ["id"]=> string()} } }

再用foreach做處理或比對即可。

如果怕出先用戶驗證錯誤的話,建議寫沒有使用者資訊則回到頁面重跑,直到有資料出現為止。(好暴力的做法....) 

 

 

2014.05.23 更新

在跑程式的過程中,我是先一口氣加了將近100個APP下去。

其中大約有63個是我按過的。

跑完發現......OMGD陣列只給25個啊!!!!!

也就是說,FB回傳陣列一次只會給你大約25筆.....

最簡單的方法就是直接帶參數GET過去

$response = $facebook -> api("/me/ids_for_business?limit=1000");即可

 

以下為回傳的正確模樣

{
"data": [{"id": "", "app": {"name": "", "namespace": "", "id": ""}}, ],
"paging": {"previous" : "","next": "", "cursors": {"before": "", "after": ""}}
}

七爺 發表在 痞客邦 留言(0) 人氣()

jQuery部分

//設定兩個function控制使用者拖曳過去及拖曳後的動作

//滑鼠拖曳過去--停止預設動作

function dragoverHandler(evt) {

evt.preventDefault();
}

//拖曳後動作
function dropHandler(evt) {
evt.preventDefault();

//取得檔案物件
var files = evt.dataTransfer.files;

var formData = new FormData();

for(var i = 0 ; i < files.length ; i++)
{
formData.append('file[]',files[i]);
}

$.ajax({
url:"img_l.php",
data:formData,
type:'post',
processData: false,
contentType: false,
cache: false,
success:function(d){$("#up_progress").html(d);},
error:function(){$("#up_progress").html('ajax err');}
});
}

CSS部分

#dropDIV{
text-align: center;
width: 100%;
height: 500px;
margin: auto;
margin-left:10px;
border: dashed 2px gray;
}
#up_progress{
text-align:left;
vertical-align:text-top;
}
img{
max-height:200px;
max-width:300px;
}

HTML部分

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

<div id="dropDIV" ondragover="dragoverHandler(event)" ondrop="dropHandler(event)">

拖曳圖片到此處上傳
<div id="up_progress"></div>
</div>

PHP部分--處理檔案(img_l.php)

//判斷$_FILE是否有變數
if(isset($_FILES['file']['error']))
{
echo "<table><tr>";

foreach($_FILES['file']['error'] as $key => $error)
{
$total = $total + 1;

if($total > 3 && $totale % 3 == 0)
{
echo '</tr><tr>';
}

$upload_dir = '../images/news_img/';
$upload_name = $_FILES['file']['name'][$key];
$upload_d = $upload_dir.$upload_name;

//檢查資料夾&檔案是否存在
if(is_dir($upload_dir))
{
if($error==UPLOAD_ERR_OK)
{
move_uploaded_file($_FILES['file']['tmp_name'][$key],$upload_d);


echo "<td>檔案:".$upload_name."上傳成功<br />";
}
else if($error!=UPLOAD_ERR_OK)
{
if($error == 1)
{
echo "<td>檔案:".$upload_name."已超過2MB限制,請重新選擇圖片或壓縮圖片在上傳。</td>";
}
else
{
echo sprintf('<td>上傳失敗,%d</td>',$error);
}
}
}
else
{
echo '資料夾不存在<br />';
}
}

echo "</tr></table>";
}

七爺 發表在 痞客邦 留言(0) 人氣()