/** * Makes an API request to elucidat * @param $headers * @param $fields * @param $url * @param $consumer_secret * @return mixed */ function call_elucidat($headers, $fields, $method, $url, $consumer_secret) { //Build a signature $headers['oauth_signature'] = build_signature($consumer_secret, array_merge($headers, $fields), $method, $url); //Build OAuth headers $auth_headers = 'Authorization:'; $auth_headers .= build_base_string($headers, ','); //Build the request string $fields_string = build_base_string($fields, '&'); //Set the headers $header = array($auth_headers, 'Expect:'); // Create curl options if (strcasecmp($method, "GET") == 0) { $url .= '?' . $fields_string; $options = array(CURLOPT_HTTPHEADER => $header, CURLOPT_HEADER => false, CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false); } else { $options = array(CURLOPT_HTTPHEADER => $header, CURLOPT_HEADER => false, CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_POST => count($fields), CURLOPT_POSTFIELDS => $fields_string); } //Init the request and set its params $request = curl_init(); curl_setopt_array($request, $options); //Make the request $response = curl_exec($request); $status = curl_getinfo($request, CURLINFO_HTTP_CODE); curl_close($request); return array('status' => $status, 'response' => json_decode($response, true)); }
<?php require 'util.php'; // アプリケーション設定 define('CONSUMER_KEY', 'hxlPpJHDZQU28NvsFVu0WMWcf'); define('CONSUMER_SECRET', 'ho1vt4Z9YTY8qUxR7Nt7hCJNQucIkYH03c8NXmwPq0MTpm1fTB'); define('CALLBACK_URL', 'http://192.168.33.19/app/callback.php'); // URL define('RTOKEN_URL', 'https://api.twitter.com/oauth/request_token'); define('AUTH_URL', 'https://api.twitter.com/oauth/authenticate'); //-------------------------------------- // リクエストトークンの取得 //-------------------------------------- $params = array("oauth_callback" => CALLBACK_URL, "oauth_consumer_key" => CONSUMER_KEY, "oauth_nonce" => md5(microtime() . mt_rand()), "oauth_timestamp" => time(), "oauth_version" => "1.0", "oauth_signature_method" => "HMAC-SHA1"); // 署名作成 $params['oauth_signature'] = build_signature('GET', RTOKEN_URL, $params, CONSUMER_SECRET); // GET送信 $res = file_get_contents(RTOKEN_URL . '?' . http_build_query($params)); // レスポンス取得 parse_str($res, $token); if (!isset($token['oauth_token'])) { echo "エラー発生"; exit; } $request_token = $token['oauth_token']; //-------------------------------------- // 認証ページにリダイレクト //-------------------------------------- $params = array('oauth_token' => $request_token); // リダイレクト header("Location: " . AUTH_URL . '?' . http_build_query($params));