keepa/php_api

Keepa.com 的 API 框架

2.0.19 2024-08-28 17:58 UTC

README

Test Image 5

Keepa API 框架

本框架旨在为 Keepa API 的用户使用。

要求

所有需要的依赖项(PHP 版本/外部库)可以在 composer.jsonpackagist 上找到

特性

  • 解析 API 响应为易于使用的 PHP 对象
  • 提供方便处理价格历史数据的方法

Composer

composer require keepa/php_api:*

快速示例

发送 API 请求

<?php

/* maybe required - depends if youre using a framework which automaticly loading this file
require_once "vendor/autoload.php";
*/ 

use Keepa\API\Request;
use Keepa\API\ResponseStatus;
use Keepa\helper\CSVType;
use Keepa\helper\CSVTypeWrapper;
use Keepa\helper\KeepaTime;
use Keepa\helper\ProductAnalyzer;
use Keepa\helper\ProductType;
use Keepa\KeepaAPI;
use Keepa\objects\AmazonLocale;

        $api = new KeepaAPI("YOUR_API_KEY");
        $r = Request::getProductRequest(AmazonLocale::DE, 0, "2015-12-31", "2018-01-01", 0, true, ['B001G73S50']);

        $response = $api->sendRequestWithRetry($r);

			switch ($response->status) {
                case ResponseStatus::OK:
                    // iterate over received product information
                    foreach ($response->products as $product){
                        if ($product->productType == ProductType::STANDARD || $product->productType == ProductType::DOWNLOADABLE) {

                            //get basic data of product and print to stdout
                            $currentAmazonPrice = ProductAnalyzer::getLast($product->csv[CSVType::AMAZON], CSVTypeWrapper::getCSVTypeFromIndex(CSVType::AMAZON));

							//check if the product is in stock -1 -> out of stock
							if ($currentAmazonPrice == -1) {
                                echo sprintf("%s %s is currently not sold by Amazon (out of stock) %s",$product->asin,$product->title,PHP_EOL);
                            } else {
                                echo sprintf("%s %s Current Amazon Price: %s %s",$product->asin,$product->title,$currentAmazonPrice,PHP_EOL);
                            }

							// get weighted mean of the last 90 days for Amazon
							$weightedMean90days = ProductAnalyzer::calcWeightedMean($product->csv[CSVType::AMAZON], KeepaTime::nowMinutes(),90, CSVTypeWrapper::getCSVTypeFromIndex(CSVType::AMAZON));

						} else {

                        }
                    }
					break;
				default:
					print_r($response);
			}