LoginSignup
13
8

More than 3 years have passed since last update.

PHP(Laravel)からGuzzleを使用して、connpass APIを叩いてみた

Last updated at Posted at 2019-05-05

概要

Laravelからconnpassのイベント情報を取得する方法の備忘録です。
リクエスト処理はGuzzleを使用しました。

コード

Guzzleのライブラリをインストール
composer require guzzlehttp/guzzle


<?php
namespace App\Services;

class ScrapingService
{
    // evetnt_idはconnpassのURLで表示されるIDのこと
    // https://web-engineer-meetup.connpass.com/event/128855/ → 128855
    public function getConnpassData($event_id)
    {
        $client = new \GuzzleHttp\Client();
        $res = $client->request('GET', 'https://connpass.com/api/v1/event/', [
            'query' => ['event_id' => $event_id]
        ]);

        $json = json_decode($res->getBody(), true);

        return array(
            "owner_display_name" => $json['events'][0]["owner_display_name"], // 管理者の表示名
            "hash_tag" => $json['events'][0]["hash_tag"], // Twitterのハッシュタグ
            "title" => $json['events'][0]["title"], // タイトル
            "waiting" => $json['events'][0]["waiting"], // 補欠者数
            "limit" => $json['events'][0]["limit"], // 定員
            "accepted" => $json['events'][0]["accepted"], // 参加者数
            "catch" => $json['events'][0]["catch"], // キャッチ
            "place" => $json['events'][0]["place"], // 開催会場
            "address" => $json['events'][0]["address"], // 開催場所
            "started_at" => $json['events'][0]["started_at"], // イベント開催日時 (ISO-8601形式)
        );
    }
}

参考URL

connpass API リファレンス
Guzzle Documentation
今時のPHP HTTPクライアントのGuzzleを使ってみた

13
8
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
13
8