phpで、oauthで認証後、そのユーザがブロックしているユーザを表示する

※このエントリーは古いです。

oauthについて再確認したエントリーはこちら

http://blog.monqy.net/2016/05/phptwitter-api-oauth.html

http://blog.monqy.net/2016/05/php-twitteroauth.html

http://blog.monqy.net/2016/05/php-twitteroauth_2.html


-----------------------------------------------------------------------------


アブラハムさんのサンプルhttps://github.com/abraham/twitteroauthをちょこっといじったもの。

<?php
/**
* @file
* User has successfully authenticated with Twitter. Access tokens saved to session and DB.
*/

/* Load required lib files. */
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');

/* If access tokens are not available redirect to connect page. */
if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {
header('Location: ./clearsessions.php');
}
/* Get user access tokens out of the session. */
$access_token = $_SESSION['access_token'];

/* Create a TwitterOauth object with consumer/user tokens. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);

/* If method is set change API call made. Test is called by default. */

$req1 = $connection->OAuthRequest("https://api.twitter.com/1.1/account/verify_credentials.json","GET");
$req2 = $connection->OAuthRequest("https://api.twitter.com/1.1/blocks/list.json","GET");
$json1 = json_decode($req1,true);
$json2 = json_decode($req2,true);

/* Include HTML to display on the page */
include('html.inc');


html.incは

<!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" xml:lang="en" lang="en">
<head>
<title>Twitter OAuth in PHP</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style type="text/css">
img {border-width: 0}
* {font-family:'Lucida Grande', sans-serif;}
</style>
</head>
<body>
<div>
<h2>Welcome to a Twitter OAuth PHP example.</h2>

<p>This site is a basic showcase of Twitters OAuth authentication method. If you are having issues try <a href='./clearsessions.php'>clearing your session</a>.</p>

<p>
Links:
<a href='http://github.com/abraham/twitteroauth'>Source Code</a> &amp;
<a href='http://wiki.github.com/abraham/twitteroauth/documentation'>Documentation</a> |
Contact @<a href='http://twitter.com/abraham'>abraham</a>
</p>
<hr />
<?php if (isset($menu)) { ?>
<?php echo $menu; ?>
<?php } ?>
</div>
<?php if (isset($status_text)) { ?>
<?php echo '<h3>'.$status_text.'</h3>'; ?>
<?php } ?>
<p>
<pre>
<?php print_r($content); ?>
<hr>
<?php print($json1[screen_name])." is blocking <br><br>";
foreach($json2[users] as $key => $value){
print $value[screen_name].'<br>';
}
?>
</pre>
</p>

</body>
</html>


いろいろとダサいところはあるでしょうが、

とりあえずこれでブロックユーザ一覧が表示できる。

ハマったのは、$contentのprint_rを消すと、ユーザに認証させるためのボタンが消えてしまうことだった。

どうしてそうなるのかはいまだによくわからないが、とりあえずこいつを消してはいけない。

つぎはblockの解除だ。

ここまでくればあとはいけるだろう。