0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DMMの商品データをAPIからPHPで取得するサンプルコード

Posted at

DMMの商品情報APIを使って商品情報を取得するPHPのサンプルコード

  • キーワード検索用で作成しています
  • API IDとアフィリエイトIDを書き換える

f8dfd64e2e5ed51774933b6da1286aa0.png

※DMMの仕様上、アフィリエイトIDの末尾は990~999にする

<?php

$api_id = "【 API ID 】";
$affiliate_id = "【アフィリエイトID】";
$keyword = urlencode("上原亜衣");

$api_url = "https://api.dmm.com/affiliate/v3/ItemList?"
    . "api_id={$api_id}"
    . "&affiliate_id={$affiliate_id}"
    . "&site=FANZA"
    . "&service=digital"
    . "&floor=videoa"
    . "&hits=10"
    . "&sort=date"
    . "&keyword={$keyword}"
    . "&output=json";

echo "APIリクエストURL: \n" . $api_url . "\n\n";  // デバッグ用

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$response = curl_exec($ch);

if ($response === false) {
    echo "cURLエラー: " . curl_error($ch) . "\n";
}

curl_close($ch);

var_dump($response);  // レスポンスの確認

$data = json_decode($response, true);

if (!$data || !isset($data["result"]["items"])) {
    echo "APIリクエストに失敗しました。\n";
    exit;
}

echo "取得商品一覧:\n";
foreach ($data["result"]["items"] as $item) {
    echo "-----------------------------\n";
    echo "タイトル: " . $item["title"] . "\n";
    echo "URL: " . $item["affiliateURL"] . "\n";
    echo "価格: " . ($item["prices"]["price"] ?? "不明") . "円\n";
    echo "発売日: " . $item["date"] . "\n";
    echo "画像URL: " . $item["imageURL"]["large"] . "\n";
}

?>
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?