Yo PHP
Yo PHP is a Yo client written in PHP. This library is still a work in progress., (*1)
, (*2)
Installation
The recomanded way to install Yo PHP is through Composer:, (*3)
{
"require": {
"black/yo-php": "@stable"
}
}
Protip: You should browse the black/yo-php
page to choose a stable version to use, avoid the @stable
meta
constraint., (*4)
Yotip: You want to know when yo-php is updated? Add YOPHPCLIENT \o/!, (*5)
Usage
yoAll
nutshell:
The yoAll
method will send a yo to all your friends., (*6)
<?php
$yo = new \Yo\Yo(['token' => 'yourtoken']);
$send = new \Yo\Service\SendYoService($yo->getHttpClient(), $yo->getOptions());
$send->yoAll();
yo
nutshell:
The yo
method will send a yo to a dedicated username. This username MUST be in uppercase and this is your
responsibility., (*7)
<?php
$yo = new \Yo\Yo(['token' => 'yourtoken']);
$send = new \Yo\Service\SendYoService($yo->getHttpClient(), $yo->getOptions());
$send->yo('USERNAME');
subscribers_count
nutshell:
The subscribersCount
method will retrieve the number of your subscribers. This is just a GET request with a json
response., (*8)
<?php
$yo = new \Yo\Yo(['token' => 'yourtoken']);
$status = new \Yo\Service\StatusService($yo->getHttpClient(), $yo->getOptions());
$subscribers = $status->subscribersCount();
If you want to convert the json to an array just replace $status->subscribersCount()
by
$status->subscribersCount()->json()
, (*9)
Send a link:
It is possible to send a link through Yo since 08/15/2014. Just add a link
key to the constructor of new Yo()
or
use $yo->addLink('url://myurl.com');
., (*10)
<?php
$yo = new \Yo\Yo(['token' => 'yourtoken', 'link' => 'http://www.desicomments.com/dc/21/50927/50927.gif']);
$send = new \Yo\Service\SendYoService($yo->getHttpClient(), $yo->getOptions());
$send->yoAll();
<?php
$yo = new \Yo\Yo(['token' => 'yourtoken');
$yo->addLink('http://www.desicomments.com/dc/21/50927/50927.gif');
$send = new \Yo\Service\SendYoService($yo->getHttpClient(), $yo->getOptions());
$send->yoAll();
Send a location:
It is possible to send your location since 10/07/2014. Just add a location
key to the constructor of new Yo()
or
use this code., (*11)
$coordinates = new Geo\Coordinates(latitude, longitude);
$yo->addLocation($coordinates);
Warning 1
It is not possible to receive or send link and location at the same time. When you construct a Yo with link and location,
link is always overrided to null., (*12)
If you use ->add(Location|Link)
function, the class will set the other parameter to null. The code is very simple so take
your time and look at the src/spec/Yo/YoSpec.php
., (*13)
Warning 2
Yo api not using a valid format for coordinates. They use ";" instead of ","
so be aware of this and don't forget to explode/convert your values (see example below)., (*14)
Receive a yo:
During the registration process, Yo will ask to if you want to know when an Yo user Yo you. This pingback send you a
GET request with the Yo username
and location
query parameters., (*15)
So... You need to create a dedicated controller. For example:, (*16)
<?php
namespace Yo\Controller;
class YoController
{
public function yoAction($username, $location = null)
{
$yoUser = new \Yo\Model\YoUser($username);
if (null !== $location) {
$location = explode(";", $location);
$coordinates = new Geo\Coordinates($location[0], $location[1]);
$yoUser->addLocation($location);
}
$dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
$dispatcher->addSubscriber(new YourSubscriber());
$yo = new \Yo\Service\ReceiveYoService($dispatcher);
$yo->receive($yoUser);
}
}
As you can see, the ReceiveYoService
will dispatch an event named yo.receive
and getting his information from a
YoUser
., (*17)
I made the choice of create a true model because you maybe want to persist all your Yo friends in a database or
anything you want., (*18)
A "default" subscriber is located in Yo/Event
directory. This YoSubscriber
will add a new line in your Monolog logs.
If you want to use it, use this sample code (or see the ./tests/Yo/ReceiveYoServiceTest
:, (*19)
<?php
namespace Yo\Controller;
class YoController
{
public function yoAction($username)
{
$yoUser = new \Yo\Model\YoUser($username);
$logger = new Monolog\Logger();
$dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
$dispatcher->addSubscriber(new \Yo\Event\YoSubscriber($logger));
$yo = new \Yo\Service\ReceiveYoService($dispatcher);
$yo->receive($yoUser);
}
}
Running the tests
There is no development key for Yo so the only way to pass the tests suite is to replace the fake token and run the
tests., (*20)
Contributing
See CONTRIBUTING file., (*21)
Credits
This README is heavily inspired by Hateoas library by the great @willdurand. This guy needs your PR for the
sake of the REST in PHP., (*22)
Alexandre "pocky" Balmes alexandre@lablackroom.com. Send me Flattrs if you love my work, [buy me gift][9] or hire me!, (*23)
License
Yo PHP is released under the MIT License. See the bundled LICENSE file for details., (*24)