1

I'm working with the PHP Steam Condenser library, to grab some Steam details about a user. I've successfully implemented this and everything is working. However I started to notice an error being thrown every so often, from Steam Condenser.

What's happening is, when I call the SteamId class with a param, the library generates a URL and makes a request to it, using SimpleXMLElement (as can be seen here). Now most of the time, the URL returns XML and so my application works fine, however every so often Steam throws back a 503 Service Unavailable back, causing it to fail.

try {
        return @new SimpleXMLElement($url, 0, true);
    } catch (Exception $e) {
        $errorMessage = "XML could not be parsed: " . $e->getMessage();
        if ((float) phpversion() < 5.3) {
            throw new SteamCondenserException($errorMessage, 0);
        } else {
            throw new SteamCondenserException($errorMessage, 0, $e);
        }
    }

In my cause, the PHP version is correct, so it throws the bottom custom Exception:

SteamCondenserException
XML could not be parsed: String could not be parsed as XML
/home/user/public_html/acme/vendor/koraktor/steam-condenser/lib/steam/community/XMLData.php (Line 38)

Although this is technically the correct exception, it isn't very meaningful since the request was just "Unavailable" and therefore couldn't gather the XML.

How would I go about editing this code to first check what the status code of the request is, if it's a 302 or 200 (since it redirects), then proceed to check the XML, otherwise if it's a 503, respond with a more genuine error (The Steam Community API is currently down) - or something.

I've Google'd my ass off, but can't see anything. Ideally I'd like it all to be done in the same request, since Steam can be a little slow sometimes.

Cheers

Alias
  • 2,983
  • 7
  • 39
  • 62

1 Answers1

2

Don't use SimpleXMLElement to also do the HTTP request.

Use curl (because it's fast) to fetch the XML plain-text and SimpleXML to parse it.

That way you can separate service availability from transmission errors (or XML errors).

Halcyon
  • 57,230
  • 10
  • 89
  • 128
  • Ah okay, so you're saying make the request with curl, check whether the response is correct and then just use SimpleXMLElement on the data you get back? – Alias Feb 14 '14 at 15:59
  • Yes, exactly. If it's important to separate those types of errors, and `SimpleXML` doesn't give you a good handle to do it, just split it up. – Halcyon Feb 14 '14 at 16:00
  • Please note that changing the source of Steam Condenser will probably result in problems with future versions. But also note that this change is something I'd long like to introduce (although not strictly depending on the cURL extension), so any pull request is welcome. – Koraktor Feb 14 '14 at 16:41
  • @Koraktor I can try adding something in. Anything you'd recommend not using CURL? I don't know if file_get_contents would do the job? – Alias Feb 14 '14 at 17:10
  • Currently, `WebApi` also uses `file_get_contents()` and `$http_response_header` (see [`WebApi` code](https://github.com/koraktor/steam-condenser-php/blob/1.3-stable/lib/steam/community/WebApi.php#L237-L253). I know that this isn't optimal, but works quite fine. – Koraktor Feb 14 '14 at 18:06
  • @Koraktor Got it working with the WebApi, but I can't get it working with [this line](https://github.com/koraktor/steam-condenser-php/blob/master/lib/steam/community/WebApi.php#L244) there, as it requires an API key, which I'm not using... Hmm. – Alias Feb 15 '14 at 14:01
  • I didn't mean "use the Web API", but more "do it like `WebApi` does". Or wait until I can fix the code, which might take a while. – Koraktor Feb 15 '14 at 15:14