php7でcurlコマンドを実行したい

前提

laravelでtwitterと連携したサービスを開発しています。
OAuth2.0でリフレッシュトークンを取得したいです。

実現したいこと

ここに実現したいことを箇条書きで書いてください。

発生している問題・エラーメッセージ

string(89) "{"error":"invalid_request","error_description":"Value passed for the token was invalid."}"

該当のソースコード

PHP

$code = htmlspecialchars($_GET["code"]); $client_id = config('twitterToken.client_id'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://api.twitter.com/2/oauth2/token?code='.$code.'&grant_type=authorization_code&client_id='.$client_id.'&code_verifier=challenge'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_USERPWD, $client_id . ':' . config('twitterToken.client_secret')); $headers = array(); $headers[] = 'Content-Type: application/x-www-form-urlencoded'; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } curl_close($ch);

試したこと

Macbookのターミナルで以下のcurlコマンドを実行したところ
トークンが返ってきました

curl

curl --location --request POST 'https://api.twitter.com/2/oauth2/token' \ --header 'Content-Type: application/x-www-form-urlencoded' \ --basic -u "client_id:client_secret" \ --data-urlencode "code=コールバックURLから取得" \ --data-urlencode 'grant_type=authorization_code' \ --data-urlencode "client_id=client_id" \ --data-urlencode 'redirect_uri=http://127.0.0.1:8000/OAuthCallback' \ --data-urlencode 'code_verifier=challenge'

補足情報(FW/ツールのバージョンなど)

Laravel v8.83.23
PHP v7.4.18

コメントを投稿

0 コメント