Advertisement
Mayumi_H

X V2 post

Jun 11th, 2025 (edited)
1,098
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.28 KB | Software | 0 0
  1. <?php
  2. //Text to X (twitter) (with oauth) 2025-06
  3. //MH+ presents, visit my blog => https://sl-memo.blogspot.com/
  4.  
  5. //X (twitter) oauth ライブラリー GitHub - abraham/twitteroauth を使用
  6. //  https://github.com/abraham/twitteroauth
  7. require __DIR__ . '/twitter_oauth/autoload.php';
  8.  
  9. //ca-bundle GitHub - composer/ca-bundle も必要
  10. // https://github.com/composer/ca-bundle
  11. require __DIR__ . '/ca-bundle/src/CaBundle.php';
  12.  
  13. // TwitterOAuthクラスをインポート
  14. use Abraham\TwitterOAuth\TwitterOAuth;
  15.  
  16. $connect = new TwitterOAuth(
  17.      '*************************'                            // API Keyをセット
  18.     ,'**************************************************'   // API Secretをセット
  19.     ,'**************************************************'   // Access Tokenをセット
  20.     ,'*********************************************'        // Access Token Secretをセット
  21. );
  22. $connect->setApiVersion('2');//V2指定
  23.  
  24. $message = "テストデータ URLテスト #TagTEST \n https://www.yahoo.com/";
  25.  
  26. $result = $connect->post(
  27.     'tweets'
  28.     ,['text' => $message]
  29.     ,['jsonPayload'=>true]
  30. );
  31.  
  32. $httpCode = $connect->getLastHttpCode();
  33. $last = $connect->getLastXHeaders();
  34.  
  35. if(($httpCode >= 200) && ($httpCode <= 299)){
  36.     //正常
  37. } else {
  38.     //エラー
  39.     print_r($result);
  40.     print_r($last);
  41. }
  42. ?>
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement