Share |
Paweł Mikołajczuk31.10.2012.

Tools IMG_0171 | Photo credit OZinOH Flickr CC BY-NC 2.0

Tools IMG_0171 | Photo credit OZinOH Flickr CC BY-NC 2.0

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/.

Recent Blog Posts RSS
All you can read

Get our newsletter: