0

I'm relatively new to programming in all aspects, so please bear with me when asking stupid questions. I'd like to get the steam price histories of selected items through Steam's market API, which is easily accessible through my browser when I'm logged in Steam. Thing is, I'd like to do it through a server, which gets me the following error:

Warning: file_get_contents(http://steamcommunity.com/market/pricehistory/?currency=1&appid=440&market_hash_name=Specialized%20Killstreak%20Brass%20Beast): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in PATH on line 16

Basically, my problem is the same as this guy's: How to retrieve steam market price history?, but his answers are for Python, and I really don't know anything about the Python language. If would like to do this in PHP, or if it's not possible in PHP I could use JavaScript.

I'm not asking for a code I can copy-paste, I'd be quite happy with anything to start with.

The code that I am trying to get to work:

// $page is the URL above
$json = json_decode(file_get_contents($page), true);
if($json["success"] == true OR !empty($json))
{
    $results = $json["prices"];
    foreach ($results as $result)
    {
        echo $result . "<br>";
    }
Community
  • 1
  • 1
Krisz
  • 1,884
  • 12
  • 17

1 Answers1

0

The answer you linked to sets the steamLogin cookie to enable access to the Steam API. If you grab that from your browser's cookies, then you can add it to your file_get_contents request is as follows:

// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=> "Cookie: steamLogin=76561198058933558%7C%7C2553658936E891AAD\r\n"
  )
);

$context = stream_context_create($opts);

// Call the API using the HTTP headers set above
$file = file_get_contents('http://steamcommunity.com/market/pricehistory/?currency=1&appid=440&market_hash_name=Specialized%20Killstreak%20Brass%20Beast', false, $context);

(reference question)

Community
  • 1
  • 1
John C
  • 8,223
  • 2
  • 36
  • 47
  • Thank you for your answer. Although I've done as you suggested, I still get the same error as before, it doesn't seem to work. – Krisz Feb 04 '16 at 23:52
  • For steamLogin, in Chrome, settings > advanced > content settings > cookies > see all cookies and site data > find steamcommunity.com > find "steamLoginSecure" > copy the "Content" string – Bhavik Hirani Oct 07 '20 at 05:45