Tips for working with our API
Don't get too far ahead of yourself. Start out with something like RESTED browser extension for Google Chrome. Or Postman.
After learning to make successful API calls through the browser extension, move on and attempt writing your own code. An extension can also help you troubleshoot by separating client-code bugs from network issues.
- Always check HTTP error codes and also check our internal API error codes (described in API docs). If you get no HTTP response at all, something is wrong on client-side.
- Play with our Testing Console (launched from API docs page). It can provide a rich amount of examples on what is working and what is not working. Console shows how actual header would look.
- Observe correct formatting for JSON encoding and "urlencoded" encoding of "long_url". Also, don't forget to encode "user:password" part of the header with base64 encoding. See description of HTTP Basic authentication.
- "long_url" should be part of POST payload. And not simply attached to URL.
Use PHP exception handling to see actual error messages, like this:
try{
$surl = $client->shorten($url, $data);
}catch(\Exception $e){
print $e->getMessage();
}
Set Content-Type header for HTTP requests. So headers part would look like this:
headers: {
'Authorization': 'Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'Content-Type': 'application/json',
'Content-Length': message.length
}