I agree to the use of cookies in accordance with the Sourcefabric Privacy Policy.

Support our media development efforts

Please note: due to the quarantine measures required by the coronavirus outbreak, we are unable to answer the phone in our Prague office. Please send an email to contact@sourcefabric.org and someone will get back to you as soon as possible.

Who, what, when, where and why

Get the latest news about Sourcefabric software, solutions and ideas.

BACK TO BLOG OVERVIEW

'Latest articles widget' with Newscoop Rest API PHP-SDK

Image 1170
Image 1170(photo: Kristin Trethewey)

About Newscoop REST API PHP-SDK.

PHP-SDK is a library that simply allows access to the Newscoop REST API. You can easily install it in any php project with Composer - just add

"sourcefabric/newscoop-php-sdk": "dev-master"

to the required section. If you don’t use Composer yet, then you can clone this repository: https://github.com/sourcefabric/newscoop-api-php-sdk to your project vendors.

The SDK uses two required dependencies (if you are not using Composer, then you must download them to you project vendors too):

  • “symfony/event-dispatcher”: “2.1.*”
  • “kriswallsmith/buzz”: “v0.7”

The most important class in sdk is Newscoop\API\Client - You will use this class for almost everything.

Last articles widget


For providing a better experience we will use Silex - The PHP micro-framework based on Symfony2 Components as a base.
 
First create a directory for the widget - for example newscoop-widget.
For the next step you need to have Composer installed in your system.
Now let's create the composer.json file: https://gist.github.com/3958118
 
   
and run the command:
php composer.phar install
 
After composer works you should have the vendor/ directory with all the needed dependencies.

Now create web/ in your project directory and inside it the file index.php (newscoop-widget/web/index.php). At first you must initialize the silex application - this will be the first version of your widget: https://gist.github.com/3958564

 

Next step - SDK initialization:

Add the use statements before require_once function (on top):

use Newscoop\API\Client; 
use Symfony\Component\HttpFoundation\Request;
  

https://gist.github.com/3963466 

Now we have the Client object - it’s time for our first api request.

You must first replace your routing function, 

 

https://gist.github.com/3963528

 
Create a new directory in “web” - “Resources” and in this directory and add - “views”. Now you can create your first twig template file:
 

To make the design better - add this asset to your web/Resources/public/css direcotry (create it)

bootstrap.css - 
https://raw.github.com/ahilles107/newscoop-widget/master/web/Resources/public/css/bootstrap.css
widget.css - 
https://github.com/ahilles107/newscoop-widget/raw/master/web/Resources/public/css/widget.css

This is all in the tutorial. You can find more complex version of this widget here - https://github.com/ahilles107/newscoop-widget/.

BACK TO TOP