使用 Basecamp API 在 PHP 中处理身份验证
我正在构建一个应用程序,使用其 API 和我构建的一些 PHP 类导入 Basecamp 数据。那里没有问题。但是,由于应用程序将根据用户输入的凭据通过 Basecamp API 进行身份验证,因此我并不总是知道他们使用的 Basecamp 凭据是否正确。现在我的逻辑只是假设信用良好并进行相应的处理(我的工作完美无缺)。如果不是,脚本就会出错。
我想做的是能够捕获并处理来自 API 的响应,如果用户的信用因任何原因失败(信用不良、URL 错误、帐户的 Basecamp API 访问权限不正确),则向用户返回一条友好的错误消息启用等)我真的不在乎罪魁祸首是什么,我只是想告诉他们它失败了并且他们可以检查一些可能的原因。
根据 Basecamp API,“如果请求失败,将返回非 200 状态代码”,
我使用 CURL 发送凭据并从 API 取回 XML。如果这真的很简单,请原谅我——我是一名不错的 PHP 程序员,但在与 API 交互方面有点菜鸟。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
成功
curl_exec
后,您可以通过curl_getinfo
:其中
$ch
是您通过curl_init(...)
。After a successful
curl_exec
you can retrieve additional information viacurl_getinfo
:Where
$ch
is the "handle" you retrieved bycurl_init(...)
.您需要在curl调用后查找响应代码,看看它是200(根据您发布的内容,这意味着它有效)还是非200状态代码,在这种情况下它不起作用。
如果您可以发布您的 CURL 片段,我可能可以提供更多帮助,但这通常是您需要做的。
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.