Question

I'm building an application that imports Basecamp data using their API and some PHP classes I've constructed. No problems there. However, since the app will be authenticating via the Basecamp API based on credentials the user enters, I won't always know if the Basecamp creds they use are correct. Right now my logic just assumes the creds are good and processes accordingly (which I have working flawlessly). If they aren't, the scripts error out.

What I'd like to do is be able to capture and process the response from the API, and give the user back a friendly error message if their creds failed for any reason (bad creds, wrong URL, Basecamp API access for the account not enabled, etc.) I don't really care what the culprit is, I just want to tell them it failed and that they can check on a few possible reasons.

According to the Basecamp API, "If a request fails, a non-200 status code will be returned"

I'm using CURL to send the creds and get back the XML from the API. Forgive me if this is something really simple--I'm a decent PHP programmer, but a bit of a noob when it comes to interfacing with APIs.

Was it helpful?

Solution

After a successful curl_exec you can retrieve additional information via curl_getinfo:

$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

Where $ch is the "handle" you retrieved by curl_init(...).

OTHER TIPS

You need to look for the response code after your curl call, and see if it's a 200 (which, according to what you posted, means that it worked) or a non-200 status code, in which case it didn't work.

If you can post your CURL snippet I can probably help more, but that's generally speaking what you need to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top