Box Integration with PHP


Brief:

Now a days most of the users use the cloud storage for storing their data and documents. Box is one type of cloud platform which provide online storage system to store documents, create folders, share documents, etc. on cloud instead of storing on PC, Hard Disk or any other devices. Box provides an third party API which can be used to integrate box in any of the website or mobile devices. Using Box you can easily access your documents from any place at anytime. Box provide libraries or SDK for most of the technology to integrate with it. Box is not provide all feature in it free version, so you need to purchase its license version to access all features.

Need:

Box is generally used to provide a platform to users to store a project related documents and create workspace for the project online at any website. Mostly all organizations used the manual system to create document and shared it with their employees via email or any other way. So to remove this manual system Box provide a very good alternative approach to create and share documents online in your project area only.

How It Works:

To integrate Box in PHP you need to use Content API for box. And you can use Content API  by creating your project box app on the developer Box portal. So firstly create Box app on the developer portal by using following steps:

  • Login with your Box Credentials on Developer box portal and click on “Create a Box Application”  link.
  • Enter your application name and click on “Create Application” button.
  • It will create a new app and redirect you to its configuration settings page and asked you for more details about your project.
  • Enter general information and keep save client_id and client_secret at your end.
  • Select the User Type as “App Users” and select the scopes which you would like to give to the app users.
  • Now generate an RSA key pair to retrieve the access token. You can create an RSA key pair by using following command:
           Generate Private Key

        openssl genrsa -aes256 -out private_key.pem 2048
         
            Generate Public Key

        openssl rsa -pubout -in private_key.pem -out public_key.pem

  • Now Add your public key under “Public Key Management” and verify it. Your public key and key ID will display in the settings.
  • Take a note of your client_id and client_secret and save the application details.

Now you require an app user token to access the Box Conent API. To create an app user token you will require JWT (Json Webs Token)  token. JWT assertion composed with three sections:
  1. Header – It specifies the algorithm for JWT signature.
  2. Claims – It contain required information to validate correct auth token.
  3. Signature – It is used to identify the request call through our website only and it is verified through public key.
You can use the below PHP libraries to generate the JWT token for your project:

        Firebase PHP-JWT : https://github.com/firebase/php-jwt
 

After generating the JWT token you need to create an app user to call the Box APIs. To create an App user you will require an enterprise access token. To request an enterprise token you need to use the JWT assertion grant and specify the “enterprise” as the box_sub_type  and enterprise_id as sub. You can find the enterprise id from your box account Admin Console enterprise settings.

Once you done with the generation of the enterprise access token you can use that access token to create an App User by following request:

    curl https://api.box.com/2.0/users \
    -H "Authorization: Bearer ACCESS_TOKEN" \
    -d '{"name": "James Anderson", "is_platform_access_only": true}' \
    -X POST


After the app user creation you can use the App User ID to create and app user access token to perform an content API call like create folder,upload file, create shared link, invite collaborator, etc. To request the App user token, you need to use the JWT assertion grant and specify the “user” as the box_sub_type and user_id as sub. So now you are all set to access any API of Box  with the app user access token.

There are lots of APIs available for accessing the box. Few important APIs are listed below:
  • Create Folder – To create a folder on box
  • Upload File – To upload a file on any particular folder or at root.
  • Invite Collaborations – Invite a people to particular folder to share the documents other stuffs.
  • Create Shared Link – To create shared link for any file or folder.
  • Preview File – To open the file in preview mode to view the content of the file
  • Upload new version of file – To upload a new version of any file (maintain version history )

Podcast

Michael Patterson sat down with the CEO of Boston Byte, Mustapha Shaikh to discuss the significance and rapid digitization of the healthcar...