(1) APIの内容
https://dashboard.meraki.com/api_docs
上記のリンクにAPIの説明があります。
この中で、Organizationsを見てみると、curlで取ってきているようです。
これを実施すると、OrganizationIDを取得できます。このOrganizationIDは他のAPIでも使うので最初にやるのが良さそうです。順番としては
Organizations -> Networks -> Devices -> Clients が良さそうです。
(2) Meraki DashboardでのAPI Keyの取得
Meraki Dashboard -> Profile -> API Access からAPIキーを生成しておきます。
(3) POSTMANの利用
Meraki APIを利用する時に、確認しやすいツールがPOSTMANです。
https://www.getpostman.com/apps
からダウンロードして起動します。 または
https://documenter.getpostman.com/view/897512/meraki-dashboard-prov-api/2To9xm#16dcb4fa-30f3-6ca5-d85a-0988c486eeef
でやってみます。
Organizations APIを利用する場合、
Getの部分:https://dashboard.meraki.com/api/v0/organizations/
Headersの部分
Key : X-Cisco-Meraki-API-Key Value : (2)で取得したもの
Key : Content-Type Value : application / json
Sendを押して、値を取得してみると結果が取得できます。
画面の右側にあるCodeを押すと、プログラムコードを取得できます。今回はPHPのcURLで取得しておきます。
(4) PHPから利用してみる
PHPでapiOrganization関数を作成します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function apiOrganization(){ | |
$curl = curl_init(); | |
curl_setopt_array($curl, array( | |
CURLOPT_URL => "https://dashboard.meraki.com/api/v0/organizations/", | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => "", | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 30, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => "GET", | |
CURLOPT_HTTPHEADER => array( | |
"cache-control: no-cache", | |
"content-type: application / json", | |
"postman-token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", | |
"x-cisco-meraki-api-key: yyyyyyyyyyyyyyyyyyyyyyyyyyy" | |
), | |
CURLOPT_FOLLOWLOCATION => TRUE | |
)); | |
$response = curl_exec($curl); | |
$err = curl_error($curl); | |
curl_close($curl); | |
/* | |
if ($err) { | |
echo "cURL Error #:" . $err; | |
} else { | |
echo $response; | |
} | |
*/ | |
$res = json_decode($response); | |
return $res; | |
} |
POSTMANで作成されたコードに
CURLOPT_FOLLOWLOCATION => TRUE
を追加しておきます。
参考
https://stackoverflow.com/questions/6077133/using-curl-and-avoiding-being-redirected-to-the-base-url
これをgetOrganization関数から呼び出し、処理をします。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function getOrganization() { | |
$this->Organization->recursive = 0; | |
$this->set('organizations', $this->Paginator->paginate()); | |
$result = $this->apiOrganization(); | |
$data = array( | |
'id' => $result[0]->id, | |
'name' => $result[0]->name, | |
); | |
$this->Organization->save($data); | |
return $this->redirect(array('action' => 'index')); | |
} | |
これでAPIで取得できたデータを処理できました。
0 件のコメント:
コメントを投稿