Mitch8910
Mitch8910

Reputation: 183

file_get_contents(JSON) not working for valid URL

I'm trying to get a JSON from a ULR, but I get the error:

Warning: file_get_contents(http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=StatTrak™ Five-SeveN | Copper Galaxy (Factory New)): failed to open stream: HTTP request failed! HTTP/1.0 500 Internal Server Error in F:\Bitnami\htdocs\Dreamweaver\freehtml5streets\updateInventory.php on line 70

This is the URL I'm trying to use, as you can see if you visit it, it works(you'll have to copy the whole thing):

http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=StatTrak™ Five-SeveN | Copper Galaxy (Factory New)

I have been accessing similar URLs and getting the JSONs, e.g.:

http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=AK-47%20%7C%20Redline%20%28Field-Tested%29

These URLs are basicly the same but the first one doesn't have the HTML tags. Here is my code:

        $data = file_get_contents('http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=' . $mydata->market_hash_name);
        $json = json_decode($data);

$mydata->market_hash_name gets me the section at the end of the URL, but without the HTML tags(%20%) etc.

How can I get this to work?

Upvotes: 2

Views: 625

Answers (2)

Professor Abronsius
Professor Abronsius

Reputation: 33813

You may find for some domains that you are unable to use this method ( file_get_contents ) to retrieve a url because the remote server is expecting a valid useragent for example - to my mind it is better to use cURL to get the contents of a remote page....

Upvotes: 0

Jerbot
Jerbot

Reputation: 1168

It appears that you need to urlencode your $mydata->market_hash_name, since it uses a number of special and reserved characters. The following should work:

//Assuming $mydata->market_hash_name == "StatTrak™ Five-SeveN | Copper Galaxy (Factory New)"

$data = file_get_contents('http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=' . urlencode($mydata->market_hash_name));
$json = json_decode($data);

Upvotes: 1

Related Questions