forked from waylaidwanderer/PHP-SteamCommunity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarket.php
108 lines (97 loc) · 4.14 KB
/
Market.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
* Created by PhpStorm.
* User: Joel
* Date: 2015-10-06
* Time: 1:57 PM
*/
namespace waylaidwanderer\SteamCommunity;
use waylaidwanderer\SteamCommunity\Market\Listings;
use waylaidwanderer\SteamCommunity\Market\PriceHistory;
use waylaidwanderer\SteamCommunity\Market\PriceOverview;
class Market
{
private $steamCommunity;
public function __construct(SteamCommunity $steamCommunity = null)
{
$this->steamCommunity = is_null($steamCommunity) ? new SteamCommunity() : $steamCommunity;
}
public function getRecentCompleted()
{
try {
$url = "http://steamcommunity.com/market/recentcompleted";
$response = $this->steamCommunity->cURL($url, 'http://steamcommunity.com/market');
$json = json_decode($response, true);
return $json;
} catch (\Exception $ex) {
throw new SteamException("Failed to retrieve /recentcompleted from market.");
}
}
public function getPriceOverview($appId, $marketHashName)
{
$marketHashName = str_replace('%2F', '%252F', rawurlencode($marketHashName));
$url = "http://steamcommunity.com/market/priceoverview/?currency=1&appid={$appId}&market_hash_name={$marketHashName}";
try {
$response = $this->steamCommunity->cURL($url);
$json = json_decode($response, true);
if (isset($json['success']) && $json['success']) {
return new PriceOverview($json);
} else {
throw new SteamException("Could not retrieve price overview for item {$marketHashName} ({$appId}) from Steam (1).");
}
} catch (\Exception $ex) {
throw new SteamException("Could not retrieve price overview for item {$marketHashName} ({$appId}) from Steam (2).");
}
}
public function getPriceHistory($appId, $marketHashName)
{
$marketHashName = str_replace('%2F', '%252F', rawurlencode($marketHashName));
$url = "http://steamcommunity.com/market/pricehistory/?appid={$appId}&market_hash_name={$marketHashName}";
try {
$response = $this->steamCommunity->cURL($url);
$json = json_decode($response, true);
if (isset($json['success']) && $json['success']) {
return new PriceHistory($json);
} else {
throw new SteamException("Could not retrieve price history for item {$marketHashName} ({$appId}) from Steam (1).");
}
} catch (\Exception $ex) {
throw new SteamException("Could not retrieve price history for item {$marketHashName} ({$appId}) from Steam (2).");
}
}
public function getListings($appId, $marketHashName)
{
$marketHashName = str_replace('%2F', '%252F', rawurlencode($marketHashName));
$url = "http://steamcommunity.com/market/listings/{$appId}/{$marketHashName}/render?currency=1";
try {
$response = $this->steamCommunity->cURL($url);
$json = json_decode($response, true);
if (isset($json['success']) && $json['success']) {
return new Listings($json);
} else {
throw new SteamException("Could not retrieve listings for item {$marketHashName} ({$appId}) from Steam (1).");
}
} catch (\Exception $ex) {
throw new SteamException("Could not retrieve listings for item {$marketHashName} ({$appId}) from Steam (2).");
}
}
public function getWalletBalance()
{
if ($this->steamCommunity->isLoggedIn()) {
$url = 'http://steamcommunity.com/market/';
$response = $this->steamCommunity->cURL($url);
$pattern = '/<span id=\"marketWalletBalanceAmount\">(.*)<\/span>/i';
preg_match($pattern, $response, $matches);
if (!isset($matches[1])) {
throw new SteamException('Unexpected response from Steam.');
}
$balance = $matches[1];
if (substr($balance, -1) == '.') {
$balance = substr($balance, 0, -1);
}
return Helper::getAmount($balance);
} else {
return 0;
}
}
}