3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Best Buy SDK * * High level PHP client for the Best Buy API */ namespace BestBuy; use BestBuy\Exception\AuthorizationException; use BestBuy\Exception\InvalidArgumentException; use BestBuy\Exception\ServiceException; use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerInterface; /** * The main client */ class Client implements LoggerAwareInterface { /** * The endpoint for the most popular viewed products */ const RECOMMENDATIONS_MOSTVIEWED = 'mostViewed'; /** * The endpoint for the most trending viewed products */ const RECOMMENDATIONS_TRENDING = 'trendingViewed'; /** * The endpoint for also viewed products (given an input product) */ const RECOMMENDATIONS_ALSOVIEWED = 'alsoViewed'; /** * The endpoint for similar products (given an input product) */ const RECOMMENDATIONS_SIMILAR = 'similar'; /** * The beta URL */ const URL_BETA = 'https://api.bestbuy.com/beta'; /** * The v1 URL */ const URL_V1 = 'https://api.bestbuy.com/v1'; /** * The configuration for the class * * # Available keys: * * `key` - string - Your Best Buy Developer API Key * * `debug` - bool - Whether to log debug information * * `curl_options` - array - An array of options to be passed into {@see curl_setopt_array} * * `associative` - bool - Whether the response should be decoded to an associative array (default to {@see StdClass}) * * @var array */ protected $config = [ 'key' => '', 'debug' => false, 'curl_options' => [ CURLOPT_RETURNTRANSFER => false, CURLOPT_HTTPHEADER => [ 'User-Agent' => 'bestbuy-sdk-php/1.0.0;php' ] ], 'associative' => false ]; /** * The logger to log to in debug mode * * @var LoggerInterface */ protected $logger; /** * Creates new instance * * @param mixed $options * If array: Merge options into client config * If string: Used as API Key * If null: Look for $_SERVER['BBY_API_KEY'] */ public function __construct($options = null) { // If we didn't get anything, but the key is a server variable, use that // Or the `$options` is a string, use that as a key // Or the `$options` is an array, merge that into the default options if (!$options && isset($_SERVER['BBY_API_KEY'])) { $this->config['key'] = $_SERVER['BBY_API_KEY']; } else if (is_string($options)) { $this->config['key'] = $options; } else if (is_array($options)) { if (isset($options['key'])) { $this->config['key'] = $options['key']; } $this->config = array_merge($this->config, $options); } } /** * Retrieve availability of products in stores based on the criteria provided * * @param int|int[]|string $skus A SKU or SKUs to look for, or a valid product query * @param int|int[]|string $stores A Store # or Store #s to look for, or a valid store query * @param array $responseConfig The additional filters to apply to the result set (pagination, view, sort, etc.) * @return array|\StdClass * @throws ServiceException */ public function availability($skus, $stores, array $responseConfig = []) { // if it's a single SKU (either int or only digits), make it to an array (for the next block) if (is_int($skus) || ctype_digit($skus)) { $skus = [(int)$skus]; } if (is_array($skus)) { $skus = 'sku in(' . implode(',', $skus) . ')'; } // if it's a single store (either int or only digits), make it to an array (for the next block) if (is_int($stores) || ctype_digit($stores)) { $stores = [(int)$stores]; } if (is_array($stores)) { $stores = 'storeId in(' . implode(',', $stores) . ')'; } return $this->doRequest( self::URL_V1, "/products({$skus})+stores({$stores})", $responseConfig ); } /** * Retrieve categories based on the criteria provided * * @param string $search A category ID or valid query * @param array $responseConfig The additional filters to apply to the result set (pagination, view, sort, etc.) * @return array|\StdClass */ public function categories($search = '', array $responseConfig = []) { return $this->simpleEndpoint('categories', $search, $responseConfig); } /** * Retrieve open box products based on the criteria provided * * @param mixed $search int = single SKU; int[] = multiple SKUs; string = query; null = all open box * @param array $responseConfig The additional filters to apply to the result set (pagination, view, sort, etc.) * @return array|\StdClass * @throws ServiceException */ public function openBox($search = '', array $responseConfig = []) { // If the search is an int or string of digits, load the results for that SKU // Or the search is an array of SKUs, load the results for those SKUs // Or the search is a query (categoryPath.id=*******), load the results in that category // Else just get all open box products if (is_int($search) || ctype_digit($search)) { $path = "/products/{$search}/openBox"; } else if (is_array($search)) { $skus = implode(',', $search); $path = "/products/openBox(sku in({$skus}))"; } else if ($search) { $path = "/products/openBox({$search})"; } else { $path = '/products/openBox'; } return $this->doRequest( self::URL_BETA, $path, $responseConfig ); } /** * Retrieve products based on the criteria provided * * @param string|int $search A product SKU or valid query * @param array $responseConfig The additional filters to apply to the result set (pagination, view, sort, etc.) * @return array|\StdClass */ public function products($search = '', array $responseConfig = []) { return $this->simpleEndpoint('products', $search, $responseConfig); } /** * Retrieve recommendations based on the criteria provided * * If using {@see BestBuy\Client::RECOMMENDATIONS_SIMILAR} or {@see BestBuy\Client::RECOMMENDATIONS_ALSOVIEWED} * you MUST pass in a SKU. * * @param string $type One of `\BestBuy\Client::RECOMMENDATIONS_*` * @param string|int $categoryIdOrSku Either a category ID for _TRENDING & _MOSTVIEWED or a SKU for _SIMILAR & _ALSOVIEWED * @param array $responseConfig The additional filters to apply to the result set (pagination, view, sort, etc.) * @return array|\StdClass * @throws InvalidArgumentException * @throws ServiceException */ public function recommendations($type, $categoryIdOrSku = null, array $responseConfig = []) { if ($type == self::RECOMMENDATIONS_TRENDING || $type == self::RECOMMENDATIONS_MOSTVIEWED) { // Trending & Most viewed work either globally or on a category level, hence the category ID is optional $search = $categoryIdOrSku !== null ? "(categoryId={$categoryIdOrSku})" : ''; $path = "/products/{$type}{$search}"; } else if ($type == self::RECOMMENDATIONS_ALSOVIEWED || $type == self::RECOMMENDATIONS_SIMILAR) { // Similar & Also viewed work on the SKU level, hence the SKU is required if ($categoryIdOrSku === null) { throw new InvalidArgumentException( 'For `Client::RECOMMENDATIONS_SIMILAR` & `Client::RECOMMENDATIONS_ALSOVIEWED`, a SKU is required' ); } $path = "/products/{$categoryIdOrSku}/{$type}"; } else { // The argument passed in isn't a valid recommendation type throw new InvalidArgumentException('`$type` must be one of `Client::RECOMMENDATIONS_*`'); } return $this->doRequest( self::URL_BETA, $path, $responseConfig ); } /** * Retrieve reviews based on the criteria provided * * @param string|int $search A review ID or valid query * @param array $responseConfig The additional filters to apply to the result set (pagination, view, sort, etc.) * @return array|\StdClass */ public function reviews($search = '', array $responseConfig = []) { return $this->simpleEndpoint('reviews', $search, $responseConfig); } /** * Sets a logger instance on the object * * @codeCoverageIgnore * * @param LoggerInterface $logger * @return null */ public function setLogger(LoggerInterface $logger) { $this->logger = $logger; } /** * Retrieve stores based on the criteria provided * * @param string|int $search A store ID or valid query * @param array $responseConfig The additional filters to apply to the result set (pagination, view, sort, etc.) * @return array|\StdClass */ public function stores($search = '', array $responseConfig = []) { return $this->simpleEndpoint('stores', $search, $responseConfig); } /** * Builds the URL to make the request against * * @param string $root The Root URL to use ({@see BestBuy\Client::URL_V1} or {@see BestBuy\Client::URL_BETA} * @param string $path The path for the endpoint + resources * @param array $responseConfig The additional filters to apply to the result set (pagination, view, sort, etc.) * @return array|\StdClass * @throws AuthorizationException */ protected function buildUrl($root, $path, array $responseConfig = []) { // Verify the client has a key if (!$this->config['key']) { throw new AuthorizationException( 'A Best Buy developer API key is required. Register for one at ' . 'developer.bestbuy.com, call new `\BestBuy\Client(YOUR_API_KEY)`, or ' . 'specify a BBY_API_KEY system environment variable.' ); } $responseConfig['apiKey'] = $this->config['key']; // If we're loading just a single resource ({sku}.json), remove the format from the querystring--it'll 400 if (!preg_match('/\.json$/', $path)) { $responseConfig['format'] = 'json'; } $querystring = http_build_query($responseConfig); // replace whitespace with url-encoded whitespace return preg_replace('/\s+/', '%20', "{$root}{$path}?{$querystring}"); } /** * Executes a request & returns the response * * @param string $root The Root URL to use ({@see BestBuy\Client::URL_V1} or {@see BestBuy\Client::URL_BETA} * @param string $path The path for the endpoint + resources * @param array $responseConfig The additional filters to apply to the result set (pagination, view, sort, etc.) * @return array|\StdClass * @throws AuthorizationException * @throws ServiceException */ protected function doRequest($root, $path, array $responseConfig = []) { // Set up the curl request $handle = curl_init($this->buildUrl($root, $path, $responseConfig)); curl_setopt_array( $handle, // using `+` to retain indices [ CURLOPT_FAILONERROR => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_RETURNTRANSFER => true ] + $this->config['curl_options'] ); // Get the response $response = curl_exec($handle); // Log if needed if ($this->config['debug'] && $this->logger) { $this->logger->info(var_export(curl_getinfo($handle), true)); } // Check for errors & close the handle $curlErrorNumber = curl_errno($handle); $curlErrorText = curl_error($handle); curl_close($handle); // If we have an error code, log if needed & bail out if ($curlErrorNumber) { if ($this->logger) { $this->logger->error($curlErrorText); } throw new ServiceException('An error occurred when communicating with the service'); } // Return the response in the configured format return json_decode($response, $this->config['associative']); } /** * Handles standard endpoints (products, stores, categories, reviews) * * @param string $endpoint The base endpoint to retrieve data from * @param string|int $search The identifier of an object or a valid query * @param array $responseConfig The additional filters to apply to the result set (pagination, view, sort, etc.) * @return array|\StdClass * @throws ServiceException */ protected function simpleEndpoint($endpoint, $search, array $responseConfig = []) { // If it's an integer (or a string that's only digits), or a category id, load the resource directly // Or we have a valid query, load the result of that query // Else load all resources if (is_int($search) || ctype_digit($search) || preg_match('/^(cat|pcmcat|abcat)\d+$/', $search)) { $path = "/{$endpoint}/{$search}.json"; } else if ($search) { $path = "/{$endpoint}({$search})"; } else { $path = "/{$endpoint}"; } return $this->doRequest( self::URL_V1, $path, $responseConfig ); } }

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0080.00816.75
8.3.50.0260.00821.93
8.3.40.0150.00018.89
8.3.30.0070.01418.96
8.3.20.0060.00320.34
8.3.10.0050.00323.46
8.3.00.0030.00619.38
8.2.180.0150.00718.55
8.2.170.0190.00322.96
8.2.160.0120.00920.55
8.2.150.0080.00024.18
8.2.140.0090.00024.66
8.2.130.0060.00320.70
8.2.120.0000.00826.35
8.2.110.0070.00320.64
8.2.100.0060.00617.93
8.2.90.0040.00419.22
8.2.80.0040.00417.97
8.2.70.0060.00317.75
8.2.60.0000.00818.05
8.2.50.0000.00818.07
8.2.40.0060.00319.46
8.2.30.0070.00020.84
8.2.20.0060.00317.74
8.2.10.0040.00418.16
8.2.00.0060.00318.08
8.1.280.0150.00425.92
8.1.270.0060.01324.04
8.1.260.0050.00328.09
8.1.250.0120.00328.09
8.1.240.0070.01422.31
8.1.230.0100.00319.32
8.1.220.0040.00417.79
8.1.210.0080.00018.77
8.1.200.0050.00517.48
8.1.190.0040.00417.71
8.1.180.0000.00818.10
8.1.170.0050.00318.68
8.1.160.0040.00422.14
8.1.150.0050.00218.95
8.1.140.0030.00619.67
8.1.130.0000.00717.63
8.1.120.0000.00717.52
8.1.110.0000.00717.52
8.1.100.0060.00317.41
8.1.90.0000.00717.42
8.1.80.0040.00417.41
8.1.70.0000.00717.55
8.1.60.0050.00617.62
8.1.50.0030.00617.53
8.1.40.0000.00817.52
8.1.30.0000.00817.62
8.1.20.0000.00817.62
8.1.10.0040.00417.52
8.1.00.0050.00317.59
8.0.300.0050.00318.77
8.0.290.0050.00316.88
8.0.280.0050.00218.48
8.0.270.0070.00017.43
8.0.260.0030.00317.38
8.0.250.0030.00517.11
8.0.240.0060.00317.19
8.0.230.0000.00717.04
8.0.220.0040.00417.08
8.0.210.0050.00217.05
8.0.200.0030.00317.12
8.0.190.0030.00617.15
8.0.180.0040.00417.08
8.0.170.0090.00016.97
8.0.160.0050.00217.16
8.0.150.0080.00017.11
8.0.140.0080.00017.08
8.0.130.0000.00613.55
8.0.120.0000.00817.09
8.0.110.0080.00017.12
8.0.100.0000.00817.14
8.0.90.0000.00717.09
8.0.80.0090.00617.04
8.0.70.0040.00416.95
8.0.60.0000.00816.95
8.0.50.0040.00416.96
8.0.30.0070.01217.03
8.0.20.0100.00917.40
8.0.10.0030.00516.98
8.0.00.0080.01116.89
7.4.330.0000.00516.79
7.4.320.0070.00016.65
7.4.300.0030.00316.53
7.4.290.0040.00416.64
7.4.280.0040.00416.64
7.4.270.0000.00716.59
7.4.260.0070.00016.70
7.4.250.0090.00016.43
7.4.240.0050.00316.67
7.4.230.0000.00716.41
7.4.220.0130.00716.70
7.4.210.0140.00416.67
7.4.200.0070.00016.48
7.4.160.0060.01016.56
7.4.150.0040.01517.40
7.4.140.0120.00617.86
7.4.130.0080.01016.68
7.4.120.0090.00916.64
7.4.110.0100.00716.61
7.4.100.0100.00716.78
7.4.90.0110.00716.64
7.4.80.0130.00619.39
7.4.70.0090.01516.59
7.4.60.0000.01716.61
7.4.50.0000.01516.69
7.4.40.0070.01416.61
7.4.30.0040.01516.66
7.4.10.0040.01515.13
7.4.00.0050.01215.07
7.3.330.0050.00013.21
7.3.320.0050.00013.18
7.3.310.0000.00816.43
7.3.300.0040.00416.36
7.3.290.0070.00916.33
7.3.280.0080.00816.31
7.3.270.0170.00717.40
7.3.260.0140.00916.32
7.3.250.0120.00916.62
7.3.240.0100.00816.64
7.3.230.0090.00916.51
7.3.210.0120.00616.62
7.3.200.0120.00616.38
7.3.190.0030.01416.50
7.3.180.0130.00316.52
7.3.170.0040.01716.49
7.3.160.0060.01016.44
7.3.130.0100.01014.91
7.3.120.0080.00814.95
7.3.110.0060.01315.13
7.3.100.0070.01114.95
7.3.90.0030.01014.87
7.3.80.0110.00414.62
7.3.70.0110.00314.46
7.3.60.0060.00914.83
7.3.50.0040.00814.83
7.3.40.0000.01314.52
7.3.30.0030.01014.92
7.3.20.0070.00316.66
7.3.10.0040.00916.50
7.3.00.0120.00516.12
7.2.330.0110.00716.66
7.2.320.0040.01416.47
7.2.310.0100.01316.73
7.2.300.0000.01716.61
7.2.290.0090.01016.34
7.2.260.0140.00715.23
7.2.250.0140.00015.14
7.2.240.0040.01115.17
7.2.230.0060.00915.08
7.2.220.0070.01014.83
7.2.210.0110.00314.99
7.2.200.0070.01015.12
7.2.190.0060.00914.82
7.2.180.0040.01115.00
7.2.170.0160.00014.93
7.2.160.0070.01015.09
7.2.150.0040.01116.63
7.2.140.0030.01316.67
7.2.130.0100.00916.15
7.2.120.0100.00916.62
7.2.110.0050.01016.36
7.2.100.0090.00616.41
7.2.90.0100.01016.64
7.2.80.0090.01016.46
7.2.70.0130.00516.46
7.2.60.0130.00416.67
7.2.50.0110.01016.54
7.2.40.0130.00816.51
7.2.30.0110.01116.82
7.2.20.0130.00516.50
7.2.10.0110.00916.57
7.2.00.0030.01417.46
7.1.330.0040.01115.49
7.1.320.0030.01015.66
7.1.310.0030.00615.69
7.1.300.0060.00315.65
7.1.290.0030.01015.71
7.1.280.0060.00315.54
7.1.270.0030.01215.66
7.1.260.0030.00715.76
7.1.250.0090.00315.57
7.1.240.0110.00415.89
7.1.230.0030.01015.71
7.1.220.0000.01315.48
7.1.210.0060.00915.66
7.1.200.0050.00715.72
7.1.190.0070.01015.77
7.1.180.0060.01015.74
7.1.170.0040.01115.79
7.1.160.0120.00315.66
7.1.150.0080.00415.50
7.1.140.0030.01015.74
7.1.130.0030.00915.75
7.1.120.0000.00815.84
7.1.110.0060.00315.63
7.1.100.0030.00716.83
7.1.90.0000.01315.39
7.1.80.0070.00715.79
7.1.70.0110.00916.37
7.1.60.0080.00817.39
7.1.50.0080.00816.09
7.1.40.0030.01015.42
7.1.30.0040.01115.75
7.1.20.0080.00415.62
7.1.10.0040.00715.64
7.1.00.0090.03918.91
7.0.330.0000.01415.48
7.0.320.0140.00015.29
7.0.310.0080.00515.45
7.0.300.0070.00714.97
7.0.290.0100.00315.24
7.0.280.0000.01514.92
7.0.270.0030.01015.03
7.0.260.0100.00315.02
7.0.250.0070.00315.05
7.0.240.0120.00015.26
7.0.230.0000.01015.36
7.0.220.0040.01115.37
7.0.210.0030.00915.44
7.0.200.0050.00515.81
7.0.190.0060.00615.16
7.0.180.0060.00915.18
7.0.170.0070.00715.39
7.0.160.0000.01315.17
7.0.150.0030.00715.21
7.0.140.0070.00715.22
7.0.130.0060.00615.21
7.0.120.0000.01115.25
7.0.110.0090.00315.24
7.0.100.0090.02717.63
7.0.90.0170.04317.73
7.0.80.0130.04117.64
7.0.70.0060.02417.74
7.0.60.0080.02717.61
7.0.50.0080.03517.91
7.0.40.0090.02516.71
7.0.30.0100.02316.56
7.0.20.0070.02716.73
7.0.10.0090.04416.56
7.0.00.0050.02316.66
5.6.400.0070.00714.33
5.6.390.0060.00613.80
5.6.380.0000.01514.17
5.6.370.0130.00314.04
5.6.360.0030.00714.30
5.6.350.0090.00614.05
5.6.340.0070.00714.25
5.6.330.0000.01514.54
5.6.320.0070.00714.08
5.6.310.0030.01014.33
5.6.300.0030.00914.54
5.6.290.0100.00314.23
5.6.280.0050.03817.59
5.6.270.0000.01414.02
5.6.260.0070.00314.44
5.6.250.0050.04117.62
5.6.240.0050.04617.45
5.6.230.0100.03017.52
5.6.220.0070.02317.51
5.6.210.0090.04317.49
5.6.200.0060.02817.72
5.6.190.0070.04517.60
5.6.180.0080.03417.58
5.6.170.0060.03817.65
5.6.160.0030.02817.60
5.6.150.0090.02617.84
5.6.140.0100.03717.74
5.6.130.0030.03217.68
5.6.120.0050.02617.62
5.6.110.0020.03917.60
5.6.100.0050.04717.76
5.6.90.0100.03317.69
5.6.80.0080.02617.44
5.6.70.0090.04017.39
5.6.60.0080.04117.33
5.6.50.0070.04517.25
5.6.40.0070.04617.24
5.6.30.0040.03617.27
5.6.20.0000.03617.31
5.6.10.0050.04017.36
5.6.00.0090.04017.32
5.5.380.0050.02717.25
5.5.370.0040.04617.32
5.5.360.0040.03717.20
5.5.350.0080.02817.52
5.5.340.0090.03817.47
5.5.330.0060.04317.49
5.5.320.0100.04217.45
5.5.310.0070.02917.52
5.5.300.0050.04117.53
5.5.290.0120.01817.50
5.5.280.0060.02717.47
5.5.270.0100.04517.49
5.5.260.0070.02817.54
5.5.250.0040.03017.45
5.5.240.0030.03617.27
5.5.230.0100.03217.18
5.5.220.0080.04217.15
5.5.210.0080.04317.22
5.5.200.0060.02417.16
5.5.190.0120.03517.02
5.5.180.0080.04217.03
5.5.170.0060.00913.93
5.5.160.0070.02517.16
5.5.150.0070.03517.15
5.5.140.0070.04617.13
5.5.130.0060.02317.23
5.5.120.0070.04017.04
5.5.110.0050.02317.13
5.5.100.0090.03317.16
5.5.90.0050.02717.14
5.5.80.0050.02216.96
5.5.70.0050.02217.02
5.5.60.0120.01616.91
5.5.50.0050.02117.01
5.5.40.0050.04217.13
5.5.30.0050.04016.94
5.5.20.0050.04117.05
5.5.10.0060.02316.93
5.5.00.0100.03717.05

preferences:
63.59 ms | 401 KiB | 5 Q