user3544504
user3544504

Reputation: 19

How to use file_get_contents() with non-ASCII characters in URL?

I currentley trying to get data from a website, and use it in my PHP script.

The link I am trying to reach is: http://steamcommunity.com/market/priceoverview/?country=US&currency=3&appid=730&market_hash_name=★%20Bayonet

Here is my code:

<?php

$skin = $_GET['market_hash_name'];
$link = "http://steamcommunity.com/market/priceoverview/?country=US&currency=3&appid=730&market_hash_name=".$skin."";
$FN = file_get_contents($link);

echo $FN;
?>

And this is how I use my link:

http://myhost.com/getPrice.php?market_hash_name=★ Bayonet

This is the error I am getting:

Warning: file_get_contents(http://steamcommunity.com/market/priceoverview/?country=US&currency=3&appid=730&market_hash_name=★ Bayonet): failed to open stream: HTTP request failed! HTTP/1.0 500 Internal Server Error in F:\xampp\htdocs\getPrice.php on line 5

EDIT:

Okay, so I have found a solution for my problem, but now a new error is comming up:

Warning: file_get_contents(http://steamcommunity.com/market/priceoverview/?country=US&currency=3&appid=730&market_hash_name=%E2%98%85+Bayonet+%7C+Stained (Factory New)): failed to open stream: HTTP request failed! HTTP/1.0 500 Internal Server Error in F:\xampp\htdocs\getPrice.php on line 7

This is how I am using my link now:

getPrice.php?market_hash_name=★ Bayonet | Stained

And this is my code:

function getPrice($string) {
$skin = urlencode($_GET['market_hash_name']);
$link = "http://steamcommunity.com/market/priceoverview/?country=US&currency=3&appid=730&market_hash_name=".$skin." ".$string;
$content = file_get_contents($link);
$data = json_decode($content, true);
return $data['lowest_price'];
}
$FN = getPrice("(Factory New)");
$MW = getPrice("(Minimal Wear)");
$FT = getPrice("(Field-Tested)");
$WW = getPrice("(Well-Worn)");
$BS = getPrice("(Battle-Scarred)");


echo "Factory New: $FN; Minimal Wear: $MW; Field-Tested: $FT; Well-Worn: $WW; Battle-Scared: $BS";

Upvotes: 0

Views: 724

Answers (1)

Matt Ball
Matt Ball

Reputation: 359826

Among other characters, non-ASCII characters must be URL-encoded (aka percent-encoded) in query strings.

$skin = url_encode($_GET['market_hash_name']);
$link = "http://steamcommunity.com/market/priceoverview/?country=US&currency=3&appid=730&market_hash_name=".$skin."";
$FN = file_get_contents($link);

Upvotes: 1

Related Questions