//tips
//基本情報理解
配列を2分木の構造にし、左を偶数、右を奇数、最初の根は[1]から始まる場合、配列の先頭から調べていくアプローチを取ると、左右に探索が振られる幅優先探索となる。
//shopify/unity
if (hash_equals($hmac, $computed_hmac)) で認証ずみなのでトークン作成内容についてみていく。
$query = array(
"client_id" => $api_key, // Your API key
"client_secret" => $shared_secret, // Your app credentials (secret key)
"code" => $params['code'] // Grab the access key from the URL
);
配列の中に必要な個人情報を入れているが、codeは過去に
//$shopify=$_GET;
//echo print_r($shopify);
Index.phpで得た下記のjson情報の中になく、"code" => $params['code'] はrequire_once("inc/functions.php”);で生成されるurl情報から得られるものであることがわかる。
{"products":[{"id":7115320557718,"title":"pants","body_html":"","vendor":"unitytestapp","product_type":"","created_at":"2021-09-20T15:49:49+09:00","handle":"pants","updated_at":"2021-09-20T15:49:51+09:00","published_at":"2021-09-20T15:49:51+09:00","template_suffix":"","status":"active","published_scope":"web","tags":"","admin_graphql_api_id":"gid:\/\/shopify\/Product\/7115320557718","variants":[{"product_id":7115320557718,"id":40790785130646,"title":"Default Title","price":"10000000","sku":"","position":1,"inventory_policy":"deny","compare_at_price":null,"fulfillment_service":"manual","inventory_management":"shopify","option1":"Default Title","option2":null,"option3":null,"created_at":"2021-09-20T15:49:49+09:00","updated_at":"2021-09-20T15:49:49+09:00","taxable":true,"barcode":"","grams":0,"image_id":null,"weight":0.0,"weight_unit":"kg","inventory_item_id":42888638333078,"inventory_quantity":1,"old_inventory_quantity":1,"requires_shipping":true,"admin_graphql_api_id":"gid:\/\/shopify\/ProductVariant\/40790785130646"}],"options":[{"product_id":7115320557718,"id":9121190314134,"name":"Title","position":1,"values":["Default Title"]}],"images":[],"image":null}]}
access token URLの作成は下記のように行われる。
$access_token_url = "https://" . $params['shop'] . "/admin/oauth/access_token";
ここで登場する/admin/oauth/の由来は、アプリインストールの承認部分まで遡るのでそこから簡単におさらいする。
まず、Shopifyマーチャントがアプリのインストールをアプリストアでリクエストすると、そのリクエストはショップのパラメータを付加されてアプリURLに送られる。
ストアオーナーのmyshopify.comのURLを得たら、アプリをユーザーが承認できるURLにリダイレクトする必要がある。この内容はinstall.phpの
$install_url = "https://" . $shop . "/admin/oauth/authorize?client_id=" . $api_key . "&scope=" . $scopes . "&redirect_uri=" . urlencode($redirect_uri);
部分に該当する。
$redirect_uri = "http://localhost/unitytest/generate_token.php";
リダイレクト先はgenerate_token.php。
ユーザーがインストールを承認したら、認証コードがクエリの文字列として URLに返されることになる。
http://localhost/generate_token.php?code=6a94694acf0339e9eb8068d8f4718eea&hmac=710205c27e7f780b0cd7ee58146388094be1b9e4762e3752840d1de21deeac5d&shop=johns-apparel.myshopify.com×tamp=1516399193
ここにcodeが入ることになる。codeはOAuthのプロセスで使用する認証コード。
この返されるURLが実際のShopifyからのリクエストなのか知るためにhmac認証を使っていた。
codeの値によってショップからアクセストークンを取得
Shopifyには特別なAPI呼び出しのエンドポイントがあり、APIキー、アプリのシークレットキーと組み合わせることで、アクセスコードをショップの永続的なAPIトークンに変換することができる。
/admin/oauth/access_token
下記参考:
https://www.shopify.jp/blog/partner-shopify-access-token-generate
https://qiita.com/macaron/items/c0cb8eeebdd43d993405
この後に出てくるcurlとは何なのかを確認する。
cURLとは、HTTPリクエストをすることにより、外部サイトの情報を取得することができる関数で、単純なHTTPリクエストにしない理由は色々なオプションをつけられて柔軟だからか。
curl_init():cURLのセッションを初期化し、返り値にcURLハンドルを返す。
curl_setopt():cURLの転送用オプションを設定。返り値にはboolean型の値。
curl_exec():cURLのセッションの実行時に使用。
curl_close():cURLのセッションを閉じるときに使用。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $access_token_url);
curl_setopt($ch, CURLOPT_POST, count($query));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($query));
参考:
https://reffect.co.jp/php/perfect_understanding_curl_in_php#i