How to receive data from a Dropbox API Webhook with PHP

Have you set up a Dropbox API webhook and are having trouble receiving the data sent by Dropbox to your app?

The reason for this is likely to be the encoding of the data. Dropbox doesn’t send the data as form data as you would expect (ie application/x-www-form-urlencoded or multipart/form-data) and therefore you can’t simply fetch the data using $_POST (which is designed as a wrapper for form encoded data).

The solution is to get the raw request (minus the headers) using the following code:

$raw_data = file_get_contents('php://input');

You’ll then need to json_decode the data, as Dropbox sends your app user IDs of users with changes in a JSON format.

If you haven’t used webhooks yet – they’re great for when you have an app that is monitoring a Dropbox folder.  There is no need to constantly poll for changes, let Dropbox notify you when something is changed!

If you have any questions, or need more advice on the Dropbox API, please add a comment…

Flat design trends – How to create buttons that pop out in pure CSS

Flat design everywhere at the minute.  Add a bit of depth to the page and make your buttons stand out, whilst adhereing to the ‘flat’ design style.  See the example below:

…and here’s the code you need:

.bigbutton {
  border: 0;
  cursor:pointer;
  background: #e84c3d;
  color: white;
  padding: 7px 16px;
  font-size: 13px;
  text-decoration: none;
  display: inline-block;
  position: relative;
	box-shadow: 1px 0px #c1392d, 0px 1px #c1392d,
              2px 1px #c1392d, 1px 2px #c1392d,
              3px 2px #c1392d, 2px 3px #c1392d,
              4px 3px #c1392d, 3px 4px #c1392d,
              5px 4px #c1392d, 4px 5px #c1392d,
              6px 5px #c1392d, 5px 6px #c1392d;
}
.bigbutton:hover, .bigbutton:focus,
.bigbutton.hover, a.bigbutton.focus {
    transform: translate(2px, 2px);
	-webkit-transform: translate(2px,2px);
	-ms-transform: translate(2px,2px);
    box-shadow: 1px 0px #c1392d, 0px 1px #c1392d,
              2px 1px #c1392d, 1px 2px #c1392d,
              3px 2px #c1392d, 2px 3px #c1392d,
              4px 3px #c1392d, 3px 4px #c1392d;
	color: white;
	text-decoration: none;
  }
.bigbutton:active,
.bigbutton.active {
   	transform: translate(4px, 4px);
	-webkit-transform: translate(4px,4px);
	-ms-transform: translate(4px,4px);
    box-shadow: 1px 0px #c1392d, 0px 1px #c1392d,
              2px 1px #c1392d, 1px 2px #c1392d;
	color: white;
	text-decoration: none;
  }

Code explained

The button simply repeats a box-shadow (down and across) many times to give a raised look. This shadow is reduced at the hover state, and reduced further on the active (clicked) state.  This makes it seem as if the button is being ‘pressed’.

The transform property is used to adjust the position of the text as the shadow is reduced, so that the face of the button (and text) appear to move with the button itself. Clever!

As this technique uses many CSS3 properties, it’s won’t be compatible with older browsers.  For example, the box-shadow is only supported in IE9+, Firefox 4, Chrome, Opera, and Safari 5.1.1.  It will degrade gracefully however.

Live as if you were to die tomorrow. Learn as if you were to live forever

— Mahatma Gandhi

How Joomla generates passwords for registration

If you need to generate a Joomla friendly password within your Joomla component, then use the following function:

private function generatePassword($pw)
{
     $salt = JUserHelper::genRandomPassword(32);
     $crypted = JUserHelper::getCryptedPassword($pw, $salt);
     $password = $crypted.':'.$salt;
     return $password;
}

Simply pass in the password the user has chosen to return an encrypted password that you can store in a database, recognisable by the standard Joomla login system.  The function will return a Joomla password.

If you wish to generate a random password rather than rely on user’s submission, try this:

$pw = substr(md5(rand()), 0, 7); //7 character random string - courtesy of stackoverflow.com

Add or remove items in the Joomla breadcrumb system

As I’m sure you’re aware, the Joomla breadcrumb / pathway is built from the nested menu structure of your current page.  If you’ve developed a page that is not linked up in the menu, or want to edit the pathway for any reason, try this snippet:

//this returns the pathway object for manipulating
$pathway = JFactory::getApplication()->getPathway();  
//append item on the end of the pathway - first argument for the name, the second (optional) for a link
$pathway->addItem('Designers','index.php?Itemid=115'); 
//append just a name to the pathway
$pathway->addItem('Hello hello');

When adding an item to the pathway with a link, note that the link will automatically be JRouted to give you lovely SEF URLs – perfect!

Modifying items in the breadcrumb trail

So you want modify page names or links listed in the pathway but not sure how? This section of code shows you two ways on how to achieve this (description below):

//get the JApplication object for this session
$app = JFactory::getApplication();
//get the pathway object of the current page
$pathway = $app->getPathway();

//this function returns the array of pathway objects, allowing you to modify it
$pathwayarray = $pathway->getPathway();
//this will update the link for the first element in the pathway (the 'home' element is not included - it must always be there)
$pathwayarray[0]->link = 'index.php?Itemid=999';

//Then use setPathway to update the breadcrumbs with your modified path!
$pathway->setPathway($pathwayarray);

//you can also update pathway names using the built-in function setItemName:
//this will update the first element in the pathway with the title 'New title'
$pathway->setItemName(0,'New title');

Hopefully this is fairly self-explanatory.

Resetting the Joomla Pathway

Sometimes rather than editing the pathway, you might want to remove it entirely and start again.  This is easy, just set the pathway as we did before, but to an empty array.  Then continue to add items to the pathway to build your own!

//get the JApplication object for this session
$app = JFactory::getApplication();
//get the pathway object of the current page
$pathway = $app->getPathway();
//set the pathway to an empty array, to reset it
$pathway->setPathway($array);
//add items to the pathway as desired - remember the links will automatically be JRouted, giving you nice SEF URLs
$pathway->addItem('Category listing','index.php?Itemid=493');
$pathway->addItem('Cars','index.php?Itemid=693');
$pathway->addItem('Mazda');

The code above will give you a pathway of Category listing > Cars > Mazda, with links where a second parameter is given of course.

For more information on the pathway object, see the Joomla documentation.

How to limit text with or without the Joomla Framework

We’ll start with Joomla. If you need to limit a string to a specified character count then why not use Joomla’s built in functions.  The advantage of using Joomla’s truncate function over standard PHP functions is that Joomla will not cut up words – it keeps words whole, only limiting the block of text at a space closest to the limit specified.

How to use Joomla Truncate()

echo JHTML::_('string.truncate', $text_to_truncate, $character_limit);

Note that the function also adds three dots (…) to the end of the sentence – if you’re not a fan of this, see below…

How to truncate and preserve words outside of Joomla

Of course, you might not be using Joomla and need to limit characters without cutting up words.  In which case, here is a function for doing so:

function truncate($text, $length = 0, $end='...')
{
// Truncate the item text if it is too long.
if ($length > 0 && strlen($text) > $length)
{
// Find the first space within the allowed length.
$tmp = substr($text, 0, $length);
$tmp = substr($tmp, 0, strrpos($tmp, ' '));

// If we don't have 3 characters of room, go to the second space within the limit.
if (strlen($tmp) >= $length - 3) {
$tmp = substr($tmp, 0, strrpos($tmp, ' '));
}

$text = $tmp.$end;
}
return $text;
}

It’s the function Joomla uses, with built-in Joomla functions swapped out for standard PHP functions – so it can be used in any PHP application!  Thanks to the Joomla development team for this one.

I’ve also added in an optional third parameter that allows you to specify the text at the end.  The default is set to ‘…’, but you can pass in anything you like, even a read more link:

echo truncate ($text_to_truncate, 100, " - <a href='http://www.samontheweb.co.uk' target='_blank'>Read more</a>");

The above will output your text, limited to 100 characters (without splitting up words in the process) and give a ‘Read more’ hyperlink at the end.

If you have any questions or need further information, don’t hesitate to leave a comment below!

 Scroll to top