読者です 読者をやめる 読者になる 読者になる

PHPでCKANのAPIを叩くサンプル

CKANのAPIは、HTTP経由でアクセスする。
取得に関しては、通常のGETで可能だが、更新についてはAPIキーを使ったPOSTが必要

$ cat samplh.php

<?php  
    // CKANのAPIキーとURLを設定(CKANのマイページ参照)  
    define ( "API_KEY", "00000000-0000-0000-0000-000000000000" );  
    define ( "API_URL", "http://ckan.examplh.com/api/3" );

    function getJson($url) {  
        $url = API_URL . $url;  
        $json = json_decode ( file_get_contents ( $url ), true );  
        return ($json);  
    }

    function postJson($url, $data) {  
        $url = API_URL . $url;

        // パラメータはJSONに変換後、URLエンコードする  
        $json = rawurlencode ( json_encode ( $data ) );

        // headには、APIキーを取得する  
        $headers = array (  
                'Authorization : ' . API_KEY,  
                'Content-Type: application/x-www-form-urlencoded',  
                'Content-Length: ' . strlen ( $json )  
        );

        // jsonはそのままぶち込む  
        $options = array (  
                'http' => array (  
                        'method' => 'POST',  
                        'header' => implode ( "\r\n", $headers ),  
                        'content' => $json  
                )  
        );

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

        return ($contents);  
    }

    // 色々取得してみる  
    // =====================================================

    // データセットの一覧を取得する  
    // -------------------------------------------------------  
    $url = "/action/package_list";  
    // print_r( getJson($url) ) ;

    // データセットに含まれるデータの一覧を取得する  
    // -------------------------------------------------------  
    $url = "/action/package_show?id=a00";  
    // print_r( getJson($url) ) ;

    // データの詳細を取得する  
    // -------------------------------------------------------  
    $url = "/action/resource_show?id=dca2e366-04bb-49ba-a837-556cbf0c9feb";  
    // print_r( getJson($url) ) ;

    // 投稿する  
    // =====================================================

    // データセットにデータを追加する  
    // ------------------------------------------------------  
    $data = array (  
            "package_id" => "a00",  
            "url" => "http://www.examples.com/",  
            'name' => 'my_dataset_name',  
            'description' => 'A long description of my dataset'  
    );  
    // print_r( postJson( "/action/resource_create", $data ) ) ;

    $data = array (  
            "active" => "active",  
            'name' => 'api_test_group',  
            'title' => 'APIで作ったグループ(japanese)',  
            'description' => 'A long description of my dataset'  
    );

    // print_r ( postJson ( "/action/group_create", $data ) );

データベース・スキーマ

CREATE SEQUENCE system_info_id_seq START WITH 1 INCREMENT BY 1 NO MAXVALUE NO MINVALUE CACHE 1;  
CREATE SEQUENCE system_info_revision_id_seq START WITH 1 INCREMENT BY 1 NO MAXVALUE NO MINVALUE CACHE 1;  
CREATE TABLE "group" ( id text NOT NULL, name text NOT NULL, title text, description text, created timestamp without time zone, state text, revision_id text, type text NOT NULL, approval_status text, image_url text, is_organization boolean DEFAULT false);  
CREATE TABLE "user" ( id text NOT NULL, name text NOT NULL, apikey text, created timestamp without time zone, about text, openid text, password text, fullname text, email text, reset_key text, sysadmin boolean DEFAULT false, activity_streams_email_notifications boolean DEFAULT false, state text DEFAULT 'active'::text NOT NULL);  
CREATE TABLE activity ( id text NOT NULL, "timestamp" timestamp without time zone, user_id text, object_id text, revision_id text, activity_type text, data text);  
CREATE TABLE activity_detail ( id text NOT NULL, activity_id text, object_id text, object_type text, activity_type text, data text);  
CREATE TABLE authorization_group ( id text NOT NULL, name text, created timestamp without time zone);  
CREATE TABLE authorization_group_role ( user_object_role_id text NOT NULL, authorization_group_id text);  
CREATE TABLE authorization_group_user ( authorization_group_id text NOT NULL, user_id text NOT NULL, id text NOT NULL);  
CREATE TABLE dashboard ( user_id text NOT NULL, activity_stream_last_viewed timestamp without time zone NOT NULL, email_last_sent timestamp without time zone DEFAULT ('now'::text)::timestamp without time zone NOT NULL);  
CREATE TABLE group_extra ( id text NOT NULL, group_id text, key text, value text, state text, revision_id text);  
CREATE TABLE group_extra_revision ( id text NOT NULL, group_id text, key text, value text, state text, revision_id text NOT NULL, continuity_id text, expired_id text, revision_timestamp timestamp without time zone, expired_timestamp timestamp without time zone, current boolean);  
CREATE TABLE group_revision ( id text NOT NULL, name text NOT NULL, title text, description text, created timestamp without time zone, state text, revision_id text NOT NULL, continuity_id text, expired_id text, revision_timestamp timestamp without time zone, expired_timestamp timestamp without time zone, current boolean, type text NOT NULL, approval_status text, image_url text, is_organization boolean DEFAULT false);  
CREATE TABLE group_role ( user_object_role_id text NOT NULL, group_id text);  
CREATE TABLE member ( id text NOT NULL, table_id text NOT NULL, group_id text, state text, revision_id text, table_name text NOT NULL, capacity text NOT NULL);  
CREATE TABLE member_revision ( id text NOT NULL, table_id text NOT NULL, group_id text, state text, revision_id text NOT NULL, continuity_id text, expired_id text, revision_timestamp timestamp without time zone, expired_timestamp timestamp without time zone, current boolean, table_name text NOT NULL, capacity text NOT NULL);  
CREATE TABLE migrate_version ( repository_id character varying(250) NOT NULL, repository_path text, version integer);  
CREATE TABLE package ( id text NOT NULL, name character varying(100) NOT NULL, title text, version character varying(100), url text, notes text, license_id text, revision_id text, author text, author_email text, maintainer text, maintainer_email text, state text, type text, owner_org text, private boolean DEFAULT false, metadata_modified timestamp without time zone, creator_user_id text);  
CREATE TABLE package_extra ( id text NOT NULL, package_id text, key text, value text, revision_id text, state text);  
CREATE TABLE package_extra_revision ( id text NOT NULL, package_id text, key text, value text, revision_id text NOT NULL, continuity_id text, state text, expired_id text, revision_timestamp timestamp without time zone, expired_timestamp timestamp without time zone, current boolean);  
CREATE TABLE package_relationship ( id text NOT NULL, subject_package_id text, object_package_id text, type text, comment text, revision_id text, state text);  
CREATE TABLE package_relationship_revision ( id text NOT NULL, subject_package_id text, object_package_id text, type text, comment text, revision_id text NOT NULL, continuity_id text, state text, expired_id text, revision_timestamp timestamp without time zone, expired_timestamp timestamp without time zone, current boolean);  
CREATE TABLE package_revision ( id text NOT NULL, name character varying(100) NOT NULL, title text, version character varying(100), url text, notes text, license_id text, revision_id text NOT NULL, continuity_id text, author text, author_email text, maintainer text, maintainer_email text, state text, expired_id text, revision_timestamp timestamp without time zone, expired_timestamp timestamp without time zone, current boolean, type text, owner_org text, private boolean DEFAULT false, metadata_modified timestamp without time zone, creator_user_id text);  
CREATE TABLE package_role ( user_object_role_id text NOT NULL, package_id text);  
CREATE TABLE package_tag ( id text NOT NULL, package_id text, tag_id text, revision_id text, state text);  
CREATE TABLE package_tag_revision ( id text NOT NULL, package_id text, tag_id text, revision_id text NOT NULL, continuity_id text, state text, expired_id text, revision_timestamp timestamp without time zone, expired_timestamp timestamp without time zone, current boolean);  
CREATE TABLE rating ( id text NOT NULL, user_id text, user_ip_address text, package_id text, rating double precision, created timestamp without time zone);  
CREATE TABLE related ( id text NOT NULL, type text NOT NULL, title text, description text, image_url text, url text, created timestamp without time zone, owner_id text, view_count integer DEFAULT 0 NOT NULL, featured integer DEFAULT 0 NOT NULL);  
CREATE TABLE related_dataset ( id text NOT NULL, dataset_id text NOT NULL, related_id text NOT NULL, status text);  
CREATE TABLE resource ( id text NOT NULL, resource_group_id text, url text NOT NULL, format text, description text, "position" integer, revision_id text, hash text, state text, extras text, name text, resource_type text, mimetype text, mimetype_inner text, size bigint, last_modified timestamp without time zone, cache_url text, cache_last_updated timestamp without time zone, webstore_url text, webstore_last_updated timestamp without time zone, created timestamp without time zone, url_type text);  
CREATE TABLE resource_group ( id text NOT NULL, package_id text, label text, sort_order text, extras text, state text, revision_id text);  
CREATE TABLE resource_group_revision ( id text NOT NULL, package_id text, label text, sort_order text, extras text, state text, revision_id text NOT NULL, continuity_id text, expired_id text, revision_timestamp timestamp without time zone, expired_timestamp timestamp without time zone, current boolean);  
CREATE TABLE resource_revision ( id text NOT NULL, resource_group_id text, url text NOT NULL, format text, description text, "position" integer, revision_id text NOT NULL, continuity_id text, hash text, state text, extras text, expired_id text, revision_timestamp timestamp without time zone, expired_timestamp timestamp without time zone, current boolean, name text, resource_type text, mimetype text, mimetype_inner text, size bigint, last_modified timestamp without time zone, cache_url text, cache_last_updated timestamp without time zone, webstore_url text, webstore_last_updated timestamp without time zone, created timestamp without time zone, url_type text);  
CREATE TABLE revision ( id text NOT NULL, "timestamp" timestamp without time zone, author character varying(200), message text, state text, approved_timestamp timestamp without time zone);  
CREATE TABLE role_action ( id text NOT NULL, role text, context text NOT NULL, action text);  
CREATE TABLE system_info ( id integer NOT NULL, key character varying(100) NOT NULL, value text, revision_id text);  
CREATE TABLE system_info_revision ( id integer NOT NULL, key character varying(100) NOT NULL, value text, revision_id text NOT NULL, continuity_id integer);  
CREATE TABLE system_role ( user_object_role_id text NOT NULL);  
CREATE TABLE tag ( id text NOT NULL, name character varying(100) NOT NULL, vocabulary_id character varying(100));  
CREATE TABLE task_status ( id text NOT NULL, entity_id text NOT NULL, entity_type text NOT NULL, task_type text NOT NULL, key text NOT NULL, value text NOT NULL, state text, error text, last_updated timestamp without time zone);  
CREATE TABLE term_translation ( term text NOT NULL, term_translation text NOT NULL, lang_code text NOT NULL);  
CREATE TABLE tracking_raw ( user_key character varying(100) NOT NULL, url text NOT NULL, tracking_type character varying(10) NOT NULL, access_timestamp timestamp without time zone DEFAULT now());  
CREATE TABLE tracking_summary ( url text NOT NULL, package_id text, tracking_type character varying(10) NOT NULL, count integer NOT NULL, running_total integer DEFAULT 0 NOT NULL, recent_views integer DEFAULT 0 NOT NULL, tracking_date date);  
CREATE TABLE user_following_dataset ( follower_id text NOT NULL, object_id text NOT NULL, datetime timestamp without time zone NOT NULL);  
CREATE TABLE user_following_group ( follower_id text NOT NULL, object_id text NOT NULL, datetime timestamp without time zone NOT NULL);  
CREATE TABLE user_following_user ( follower_id text NOT NULL, object_id text NOT NULL, datetime timestamp without time zone NOT NULL);  
CREATE TABLE user_object_role ( id text NOT NULL, user_id text, context text NOT NULL, role text, authorized_group_id text);  
CREATE TABLE vocabulary ( id text NOT NULL, name character varying(100) NOT NULL);