LoginSignup
4
5

More than 3 years have passed since last update.

phpでauカブコム証券のkabuステーションAPIをたたく!

Last updated at Posted at 2020-08-27

トークン取得のところまで作成。

パスワードをpostするのだけど、JSONで送るのがミソ。ここを普通にクエリパラメータで送ると4001005パラメータ変換エラーになるので注意。

        $url = "http://localhost:18081/kabusapi/token";
        $data = ['APIPassword' => 'mypass'];
        $opts = [
            'http' => [
                'method' => "POST",
                'header'=> "Content-type: application/json\r\n" . "Accept: application/json\r\n",
                'content' => json_encode($data),
                'ignore_errors' => true,
                'protocol_version' => '1.1'
            ]
        ];
        $context = stream_context_create($opts);
        $json = file_get_contents($url, false, $context);
        if (isset($http_response_header)) {
            $pos = strpos($http_response_header[0], '200');
            if ($pos === false) {
                // エラー処理
                exit;
            }
        }
        if (!$json) {
            // エラー処理
            exit;
        }

        $response = json_decode($json, true);
        if ($response['ResultCode'] === 0) {
            $apikey = $response['Token'];
        } else {
            // エラー処理
            exit;
        }

API銘柄登録全解除。

Content-Length: 0 を送るのがミソ。

API銘柄が一件も登録されていない状態で実行すると確かエラーになった。

portが18080だと本番だが、確か、検証環境だとそもそも銘柄登録できないんじゃないかな。

        $url = "http://localhost:18080/kabusapi/unregister/all";
        $opts = [
            'http' => [
                'method' => "PUT",
                'header'=> "Content-type: application/json\r\nContent-Length: 0\r\n"
                . "X-API-KEY: " . $apikey ,
                'ignore_errors' => true,
                'protocol_version' => '1.1'
            ]
        ];
        $context = stream_context_create($opts);
        $json = file_get_contents($url, false, $context);
        if (isset($http_response_header)) {
            $pos = strpos($http_response_header[0], '200');
            if ($pos === false) {
                // エラー処理
            }
        }
        if (!$json) {
                // エラー処理
        }

        $response = json_decode($json, true);

4
5
0

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
4
5