此範例是參考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>

 

arrow
arrow
    全站熱搜

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