theofficialhb theofficialhb - 1 year ago 191
Ajax Question

Steam inventory loading with ajax, how?

So i made this little code here:

$steamData = file_get_contents("");
$data = json_decode($steamData, true);
$items = $data['rgDescriptions'];
foreach($items as $key => $item){
$market_hash_name = $item["market_hash_name"];
$market_hash_name = htmlentities($market_hash_name, ENT_QUOTES, 'UTF-8');
$sql = "SELECT price FROM itemprice WHERE market_hash_name='".$market_hash_name."'";
$result = $conn->query($sql);
$itemprice = "-";
if ($result->num_rows > 0)
while($row = $result->fetch_assoc())
$itemprice = $row['price'];
$itemprice = floatval($itemprice);
$itemprice = $itemprice*1.05;
$itemprice = 0;
echo '<div class="items"><center><img src="' . $item["icon_url"] . '" height="192" width="256"></center><div><center><h5 style="color:#'.$item["name_color"].'">'.$item["market_name"].' - <strong><b><a style="color:green">$'.$itemprice.'</a></b></strong></h5></center></div></div>';

How can i make this script run with ajax until it gives some data back?Because its like 1/10 times working.

Answer Source

How can i make this script run with ajax until it gives some data back?

I think you're asking the wrong question here. When you make an ajax request to your server and your server fails to honor it correctly it should be assumed that your server made the mistake and that you caused the problem*. However when Steam fails to provide information, your server should attempt to hold Steam's server accountable rather than immediately propagate the problem to your client's code.

Ultimately, the problem lies here:

$steamData =     file_get_contents("");

You don't make any checks to ensure that the data you've gotten from Steam is the correct data - or even that this network call succeeded at all! For this you need a non-trival HTTP request in order to check the status, which means you have to get rid of file_get_contents() and use the curl functions instead to make sure that the request succeeded as you expect:

do {
    //If this isn't the first time in the loop, pause for a second to prevent spamming their servers

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "");
    //The curl request should return the body of the text
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //Apparently the URL provided initially returns with a 3XX response, this option follows the redirection.
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    $steamData = curl_exec($ch);

    $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    //Repeat this command until we get a successful request
    //Note that this is slightly naive - Any 2XX should be deemed successful.
} while ($statusCode !== 200);

$data = json_decode($steamData, true);

Doing this should greatly improve those 1/10 odds of success, but it doesn't guarantee success as relying on network calls is inherently out of your control. If it were to happen that Steam shut down, repeatedly sending ajax requests to your server wouldn't accomplish anything and is actually harmful for the client*. In these situations your server should respond with an HTTP 500 error (that is, indicating the the server messed up) and display an error message to your user that the request can't be completed and not attempt to rerequest the data.

*I should also mention that sending multiple ajax requests means that the client is utilizing their bandwidth cap to correct for an issue that occurred on the server, which is unideal.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download