black / yo-php
PHP的Yo客户端
Requires
- php: >=5.4.0
- guzzlehttp/guzzle: ~4
- monolog/monolog: ~1.10
- symfony/event-dispatcher: ~2.5
- symfony/options-resolver: ~2.5
Requires (Dev)
- phpunit/phpunit: ~4.1
This package is not auto-updated.
Last update: 2020-05-08 14:07:44 UTC
README
Yo PHP是用PHP编写的Yo客户端。这个库仍在开发中。
安装
推荐通过Composer安装Yo PHP。
{ "require": { "black/yo-php": "@stable" } }
技巧:你应该浏览black/yo-php
页面,选择一个稳定版本来使用,避免使用@stable
元约束。
提示:你想知道yo-php何时更新吗?添加YOPHPCLIENT \o/!
用法
yoAll
概要
yoAll
方法将向所有朋友发送Yo。
<?php $yo = new \Yo\Yo(['token' => 'yourtoken']); $send = new \Yo\Service\SendYoService($yo->getHttpClient(), $yo->getOptions()); $send->yoAll();
yo
概要
yo
方法将向特定的用户名发送Yo。这个用户名必须是大写的,这是你的责任。
<?php $yo = new \Yo\Yo(['token' => 'yourtoken']); $send = new \Yo\Service\SendYoService($yo->getHttpClient(), $yo->getOptions()); $send->yo('USERNAME');
subscribers_count
概要
subscribersCount
方法将检索你的订阅者数量。这只是一个带有json响应的GET请求。
<?php $yo = new \Yo\Yo(['token' => 'yourtoken']); $status = new \Yo\Service\StatusService($yo->getHttpClient(), $yo->getOptions()); $subscribers = $status->subscribersCount();
如果你想将json转换为数组,只需将$status->subscribersCount()
替换为$status->subscribersCount()->json()
发送链接
自2014年8月15日起,可以通过Yo发送链接。只需在new Yo()
构造函数中添加一个link
键或使用$yo->addLink('url://myurl.com');
。
<?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();
发送位置
自2014年10月7日起,可以发送位置。只需在new Yo()
构造函数中添加一个location
键或使用此代码。
$coordinates = new Geo\Coordinates(latitude, longitude); $yo->addLocation($coordinates);
警告1:无法同时接收或发送链接和位置。当构造同时包含链接和位置的Yo时,链接总是被重置为null。
如果你使用->add(Location|Link)
函数,类将设置其他参数为null。代码非常简单,所以请花时间查看src/spec/Yo/YoSpec.php
。
警告2:Yo api没有使用有效的坐标格式。他们使用";"代替",",所以请注意这一点,不要忘记分解/转换你的值(见下面的示例)。
接收Yo
在注册过程中,Yo会询问你是否想知道何时有Yo用户Yo你。这个pingback发送一个包含Yo username
和location
查询参数的GET请求。
所以...你需要创建一个专门的控制器。例如
<?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); } }
如你所见,ReceiveYoService
将触发一个名为yo.receive
的事件,并从YoUser
获取其信息。
我选择了创建一个真正的模型,因为你可能想在数据库中持久化你的所有Yo朋友或任何你想要的东西。
“默认”订阅者位于Yo/Event
目录。这个YoSubscriber
将为你添加新的行到Monolog日志中。如果你想使用它,请使用以下示例代码(或查看./tests/Yo/ReceiveYoServiceTest
)
<?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); } }
运行测试
Yo没有开发密钥,所以通过测试套件的唯一方法是替换伪造的令牌并运行测试。
贡献
查看CONTRIBUTING文件。
鸣谢
这个README受到了伟大的Hateoas库的极大启发,该库由@willdurand创建。为了PHP中的REST,这个人需要你的PR。
Alexandre "pocky" Balmes alexandre@lablackroom.com。如果你喜欢我的工作,请给我Flattrs,买礼物或雇佣我!
许可
Yo PHP在MIT许可下发布。有关详细信息,请参阅捆绑的LICENSE文件。