LoginSignup
22
28

More than 5 years have passed since last update.

PHPからPOSTでQiitaのauthAPIを叩く方法のメモ

Last updated at Posted at 2014-06-14
qiita_auth.php
// APIのURL
$url = 'https://qiita.com/api/v1/auth';

// Qiitaユーザ名とパスワード
$data = array(
    'url_name' => $params['uid'],
    'password' => $params['pwd'],
);

// HTTPヘッダの内容(※ここがかなり重要っぽい)
$headers = array(
    'Content-Type: application/x-www-form-urlencoded',
);

// コンテキストリソース
$options = array(
    'http' => array(
        'method' => 'POST',
        'content' => http_build_query($data),
        'header' => implode("\r\n", $headers),
    )
);

$contents = file_get_contents($url, false, stream_context_create($options));

これでいけるっぽい。
コンテキストリソースにヘッダの内容(Content-Type)を含めないと、なんかこんなエラーが起こってしまう。

file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded

Content-Typeが特定できません。的な。

これできちんとユーザ名とTokenが帰ってきてTokenが必要なAPIも叩けるようになりました。

22
28
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
28