質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Twitter

Twitterは、140文字以内の「ツイート」と呼ばれる短文を投稿できるサービスです。Twitter上のほぼ全ての機能に対応するAPIが存在し、その関連サービスが多く公開されています。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

2回答

5508閲覧

TwitterOAuth ライブラリにてエラー

sola

総合スコア12

Twitter

Twitterは、140文字以内の「ツイート」と呼ばれる短文を投稿できるサービスです。Twitter上のほぼ全ての機能に対応するAPIが存在し、その関連サービスが多く公開されています。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

1クリップ

投稿2017/11/06 20:17

以下のコードを共有サーバー上で実行した際に、9行目(最後の行)にてエラーが発生しました。

PHP

1<?php 2 define( 'CONSUMER_KEY', 'hogehoge' ); 3 define( 'CONSUMER_SECRET', 'hogehoge' ); 4 define( 'OAUTH_CALLBACK', 'https://example.com/callback/' ); 5 use Abraham\TwitterOAuth\TwitterOAuth; 6 session_start(); 7 require_once( 'twitteroauth/autoload.php' ); 8 $connection = new TwitterOAuth( CONSUMER_KEY, CONSUMER_SECRET ); 9 $request_token = $connection->oauth( 'oauth/request_token', [ 'oauth_callback' => OAUTH_CALLBACK ]

エラーの内容は以下のとおりです。

Fatal error: Uncaught Abraham\TwitterOAuth\TwitterOAuthException in /home/user/html/DocumentRoot/twitteroauth/src/TwitterOAuth.php:440 Stack trace: #0 /home/user/html/DocumentRoot/twitteroauth/src/TwitterOAuth.php(357): Abraham\TwitterOAuth\TwitterOAuth->request('https://api.twi...', 'POST', 'Authorization: ...', Array) #1 /home/user/html/DocumentRoot/twitteroauth/src/TwitterOAuth.php(134): Abraham\TwitterOAuth\TwitterOAuth->oAuthRequest('https://api.twi...', 'POST', Array) #2 /home/user/html/DocumentRoot/twitter.php(12): Abraham\TwitterOAuth\TwitterOAuth->oauth('oauth/request_t...', Array) #3 {main} thrown in /home/user/html/DocumentRoot/twitteroauth/src/TwitterOAuth.php on line 440

見方が分からず苦戦しております。
アドバイス頂けますと大変助かります。

ちなみに、ローカルのサーバーで上記コードを実行した際は問題なく動作致しました。

よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

例外はTwitterOAuthExceptionにスローされますので、それを捕捉してください

<?php define( 'CONSUMER_KEY', 'hogehoge' ); define( 'CONSUMER_SECRET', 'hogehoge' ); define( 'OAUTH_CALLBACK', 'https://example.com/callback/' ); use Abraham\TwitterOAuth\TwitterOAuth; session_start(); require_once( 'twitteroauth/autoload.php' ); try{ $connection = new TwitterOAuth( CONSUMER_KEY, CONSUMER_SECRET ); $request_token = $connection->oauth( 'oauth/request_token', [ 'oauth_callback' => OAUTH_CALLBACK ] }catch (TwitterOAuthException $e){ var_dump($e->getMessage()); var_dump($e->getTrace()); }

投稿2017/11/13 07:51

39ff

総合スコア284

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sola

2017/11/13 08:39 編集

b2759 さん コメント頂きありがとうございます。 元々「Uncaught」なので TwitterOAuthException として補足出来ていなかったのだと思いますが、 > catch (TwitterOAuthException $e) を > catch (Exception $e) に変えることで以下のメッセージが取得できました。 > > string(0) "" > array(3) { > [0]=> > array(6) { > ["file"]=> > string(66) "/home/user/html/DocumentRoot/twitteroauth/src/TwitterOAuth.php" > ["line"]=> > int(357) > ["function"]=> > string(7) "request" > ["class"]=> > string(33) "Abraham\TwitterOAuth\TwitterOAuth" > ["type"]=> > string(2) "->" > ["args"]=> > array(4) { > [0]=> > string(43) "https://api.twitter.com/oauth/request_token" > [1]=> > string(4) "POST" > [2]=> > string(315) "Authorization: OAuth oauth_version="1.0", oauth_nonce="98c80a237fd9527d653b2833dd640dca", oauth_timestamp="1510562024", oauth_consumer_key="xxxxxx", oauth_callback="https%3A%2F%2Fexample.jp%2Fcallback%2F", oauth_signature_method="HMAC-SHA1", oauth_signature="xxxxxx"" > [3]=> > array(0) { > } > } > } > [1]=> > array(6) { > ["file"]=> > string(66) "/home/user/html/DocumentRoot/twitteroauth/src/TwitterOAuth.php" > ["line"]=> > int(134) > ["function"]=> > string(12) "oAuthRequest" > ["class"]=> > string(33) "Abraham\TwitterOAuth\TwitterOAuth" > ["type"]=> > string(2) "->" > ["args"]=> > array(3) { > [0]=> > string(43) "https://api.twitter.com/oauth/request_token" > [1]=> > string(4) "POST" > [2]=> > array(0) { > } > } > } > [2]=> > array(6) { > ["file"]=> > string(44) "/home/user/html/DocumentRoot/twitter.php" > ["line"]=> > int(15) > ["function"]=> > string(5) "oauth" > ["class"]=> > string(33) "Abraham\TwitterOAuth\TwitterOAuth" > ["type"]=> > string(2) "->" > ["args"]=> > array(2) { > [0]=> > string(19) "oauth/request_token" > [1]=> > array(1) { > ["oauth_callback"]=> > string(30) "https://example.jp/callback/" > } > } > } > }
39ff

2017/11/13 08:50

どこの共有サーバを使用しているか、可能であれば教えて頂けますか
sola

2017/11/13 15:28

b2759 さん 改めてコメント頂きありがとうございます。 さくらクラウドです。 OSは RHEL ベースの kusanagi を用いています。
guest

0

書いてあるとおり、

Uncaught Abraham\TwitterOAuth\TwitterOAuthException in /home/user/html/DocumentRoot/twitteroauth/src/TwitterOAuth.php:440

でexception投げられてるので、そこからだんだんと

#0 /home/user/html/DocumentRoot/twitteroauth/src/TwitterOAuth.php(357): Abraham\TwitterOAuth\TwitterOAuth->request('https://api.twi...', 'POST', 'Authorization: ...', Array) #1 /home/user/html/DocumentRoot/twitteroauth/src/TwitterOAuth.php(134): Abraham\TwitterOAuth\TwitterOAuth->oAuthRequest('https://api.twi...', 'POST', Array) ...

と、#0, #1と遡って読んでいくと分かるようになるかと思います。

ところで、共有サーバの方でCONSUMER_KEYとか取っていますか?

投稿2017/11/07 00:41

yoorwm

総合スコア1305

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sola

2017/11/07 08:28

yoorwm さん 回答頂きありがとうございます。 > と、#0, #1と遡って読んでいくと分かるようになるかと思います。 見方が分からないとお伝えしましたが、読み方が分からないに近いでしょうか。 なんとなく辿ってコードを追ったりはしましたが、自分の知識では結局どこに原因があるのか理解出来ませんでした。 > #2 /home/user/html/DocumentRoot/twitter.php(12): Abraham\TwitterOAuth\TwitterOAuth->oauth('oauth/request_t...', Array) ちなみにこの行の(12)は(9)です。 > ところで、共有サーバの方でCONSUMER_KEYとか取っていますか? いえ、https://apps.twitter.com/ から取得したKEYを静的に指定しています。 追加のアドバイス頂けますと助かります。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問