Created
April 8, 2010 18:41
-
-
Save ritou/360377 to your computer and use it in GitHub Desktop.
TOWRAP! PHP Sample Code. http://r-weblife.sakura.ne.jp/php-oauth-wrap-webapp-profile/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class OAuthWRAPConfig{ | |
const WRAP_CLIENT_ID = "dXNlcj0xNDE5NzI1MSZkPXItd2VibGlmZS5zYWt1cmEubmUuanAmcmVnPTEyNzA3NDkxNDguMTMwOA--"; | |
const WRAP_CLIENT_SECRET = "dd5cc1e652a7d96b1972"; | |
const WRAP_CALLBACK = "http://r-weblife.sakura.ne.jp/php-oauth-wrap-webapp-profile/"; | |
} | |
class OAuthWRAPUtil{ | |
public function get( $url ){ | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 5); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE); | |
$response = curl_exec($ch); | |
curl_close ($ch); | |
if(is_bool($response) && !$response) { | |
return NULL; | |
} | |
parse_str( $response, $response_array ); | |
return $response; | |
} | |
public function post( $url, $params ){ | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_POST,1); | |
$poststr = http_build_query($params); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $poststr); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 5); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE); | |
$response = curl_exec($ch); | |
curl_close ($ch); | |
if( is_bool($response) && !$response ) { | |
return NULL; | |
} | |
return $response; | |
} | |
public function parseQueryString( $str ){ | |
$status = parse_str($str,$query_array); | |
return $query_array; | |
} | |
} | |
session_name(PHP_WRAP_SAMPLE); | |
session_start(); | |
if( $_SESSION['wrap_access_token'] ){ | |
$url = "https://r-weblife.sakura.ne.jp/towrap/home_timeline.json?wrap_access_token=".urlencode($_SESSION['wrap_access_token']); | |
$home_feed = json_decode(OAuthWRAPUtil::get( $url )); | |
if( !$home_feed->error ){ | |
print "APIアクセス成功!"; | |
var_dump($home_feed); | |
exit; | |
} | |
$url = "https://r-weblife.sakura.ne.jp/oauthwrap/v0/refresh_token"; | |
$params = array ( "wrap_client_id" => OAuthWRAPConfig::WRAP_CLIENT_ID, | |
"wrap_client_secret" => OAuthWRAPConfig::WRAP_CLIENT_SECRET, | |
"wrap_refresh_token" => $_SESSION['wrap_refresh_token'] ); | |
$response = OAuthWRAPUtil::parseQueryString( OAuthWRAPUtil::post( $url, $params ) ); | |
if( $response["wrap_access_token"] ){ | |
$_SESSION["wrap_access_token"] = $response["wrap_access_token"]; | |
header("Location: ".OAuthWRAPConfig::WRAP_CALLBACK ); | |
exit; | |
} | |
}elseif( $_GET["wrap_verification_code"] ){ | |
$url = "https://r-weblife.sakura.ne.jp/oauthwrap/v0/get_token"; | |
$params = array ( "wrap_client_id" => OAuthWRAPConfig::WRAP_CLIENT_ID, | |
"wrap_client_secret" => OAuthWRAPConfig::WRAP_CLIENT_SECRET, | |
"wrap_verification_code" => $_GET["wrap_verification_code"], | |
"wrap_callback" => OAuthWRAPConfig::WRAP_CALLBACK ); | |
$response = OAuthWRAPUtil::parseQueryString( OAuthWRAPUtil::post( $url, $params ) ); | |
if( $response["wrap_refresh_token"] ){ | |
$_SESSION["wrap_refresh_token"] = $response["wrap_refresh_token"]; | |
$_SESSION["wrap_access_token"] = $response["wrap_access_token"]; | |
header("Location: ".OAuthWRAPConfig::WRAP_CALLBACK ); | |
exit; | |
} | |
} | |
$url = "https://r-weblife.sakura.ne.jp/oauthwrap/v0/get_auth". | |
"?wrap_client_id=".OAuthWRAPConfig::WRAP_CLIENT_ID. | |
"&wrap_callback=".urlencode( OAuthWRAPConfig::WRAP_CALLBACK ); | |
header( "Location: ".$url ); | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment