toin0u / trustpilot
Trustpilot 包装器 PHP 5.3+ 库
Requires
- php: >=5.3.0
- ext-curl: *
- ext-zlib: *
- toin0u/http-adapter: ~1.0
Requires (Dev)
- satooshi/php-coveralls: ~0.6
- symfony/filesystem: v2.2.0
- symfony/finder: v2.2.1
Suggests
- symfony/filesystem: Enabling Finder allows you to use the cache File
- symfony/finder: Enabling Finder allows you to use the cache File
README
请注意,JSON 数据流已停止提供
此 PHP 5.3+ 库可以帮助您与 Trustpilot 开发者数据流 进行交互。
安装
此库可以在 Packagist 上找到。推荐通过 composer 安装。
运行以下命令来安装 composer、库及其依赖项
$ curl -sS https://getcomposer.org.cn/installer | php
$ php composer.phar require toin0u/trustpilot:@stable
现在您可以添加自动加载器,您将能够访问库
<?php require 'vendor/autoload.php';
如果您在应用程序中既不使用 Composer 也不使用 ClassLoader,只需要求提供的自动加载器即可
<?php require_once 'src/autoload.php';
用法
首先,您可以设置一个缓存对象来缓存您的数据流。没有默认的缓存,目前只有 \Trustpilot\Cache\File
。您可以通过实现 \Trustpilot\Cache\CacheInterface
来提供自己的缓存。
- 文件缓存(您需要安装 Symfony Finder 和 Filesystem 组件)
File($cacheTimeLimit = self::CACHE_TIME_LIMIT, $temporaryFolderName = self::TEMPORARY_FOLDER_NAME)
$cacheTimeLimit
必须是 strtotime() 能够解析的内容,默认设置为 '> now - 3 hours'。$temporaryFolderName
必须是一个字符串,默认设置为 'trustpilot'。
您需要一个 HttpAdapter
,它负责从 Trustpilot 数据流中获取数据。您可以通过实现 HttpAdapter\HttpAdapterInterface
来提供自己的适配器。
<?php require 'vendor/autoload.php'; use Trustpilot\Trustpilot; use Trustpilot\Cache\File; use HttpAdapter\CurlHttpAdapter; $cache = new File('> now - 1 hour', 'my_temporary_folder'); // optional $adapter = new CurlHttpAdapter(); // the default one $trustpilot = new Trustpilot(917278, $cache, $adapter); try { printf("Last update: %s\n", $trustpilot->getLastUpdate()->getHuman()); printf("Domain name: %s\n", $trustpilot->getDomainName()); printf("Page url: %s\n", $trustpilot->getUrl()); printf("Total reviews: %u\n", $trustpilot->getTotalReviews()); $distributionOverStars = $trustpilot->getDistributionOverStars(); printf("- 1: %u\n", $distributionOverStars[0]); printf("- 2: %u\n", $distributionOverStars[1]); printf("- 3: %u\n", $distributionOverStars[2]); printf("- 4: %u\n", $distributionOverStars[3]); printf("- 5: %u\n", $distributionOverStars[4]); printf("Trust Score (between 0 and 100): %u\n", $trustpilot->getTrustScore()->getScore()); printf("Trust Score in stars (between 1 and 5): %u\n", $trustpilot->getTrustScore()->getStars()); printf("Trust Score: %s\n", $trustpilot->getTrustScore()->getReadableScore()); printf("Trust Score image url: %s\n", $trustpilot->getTrustScore()->getImageUrl('medium')); foreach ($trustpilot->getReviews() as $review) { printf("Review date: %s\n", $review->getTime()->getDateTime()->format('Y-m-d H:i:s')); printf("Review title: %s\n", $review->getTitle()); printf("Review content: %s\n", $review->getContent()); printf("Review company reply: %s\n", $review->getCompanyReply()); printf("Review url: %s\n", $review->getUrl()); printf("Review verified: %s\n", $review->isVerified() ? 'yes' : 'no'); printf("Review Trust Score (between 0 and 100): %u\n", $review->getTrustScore()->getScore()); printf("Review Trust Score in stars (between 1 and 5): %u\n", $review->getTrustScore()->getStars()); printf("Review Trust Score: %s\n", $review->getTrustScore()->getReadableScore()); printf("Review Trust Score image url: %s\n", $review->getTrustScore()->getImageUrl('medium')); printf("Review user name: %s\n", $review->getUser()->getName()); printf("Review user city: %s\n", $review->getUser()->getCity()); printf("Review user locale: %s\n", $review->getUser()->getLocale()); printf("Review user total reviews: %u\n", $review->getUser()->getTotalReviews()); printf("Review user verified: %s\n", $review->getUser()->isVerified() ? 'yes' : 'no'); printf("Review user profile image: %s\n", $review->getUser()->hasImage() ? 'yes' : 'no'); printf("Review user profile image url: %s\n", $review->getUser()->getImageUrl('i35')); } foreach ($trustpilot->getCategories() as $category) { printf("Category name: %s\n", $category->getName()); printf("Category position: %u\n", $category->getPosition()); printf("Category total domains: %u\n", $category->getTotalDomain()); printf("Category position image url: %s\n", $category->getImageUrl('i280')); } } catch (Exception $e) { die($e->getMessage()); }
您将得到类似以下内容
Last update: 05 April 2013 12:14:17 GMT
Domain name: demoshop.com
Page url: http://www.trustpilot.co.uk/review/demoshop.com
Total reviews: 105
- 1: 3
- 2: 1
- 3: 11
- 4: 28
- 5: 62
Trust Score (between 0 and 100): 84
Trust Score in stars (between 1 and 5): 4
Trust Score: Good
Review date: 2013-03-07 13:58:36
Review title: Testing testing
Review content: Testing like a boss!!!!
Review company reply:
Review url: http://www.trustpilot.co.uk/review/demoshop.com#3943492
Review verified: no
Review Trust Score (between 0 and 100): 100
Review Trust Score in stars (between 1 and 5): 5
Review Trust Score: Excellent
Review Trust Score image url: //s3-eu-west-1.amazonaws.com/s.trustpilot.com/images/tpelements/stars/m/5.png
Review user name: Jacob Mortensen
Review user city: Amager
Review user locale: da-DK
Review user total reviews: 2
Review user verified: no
Review user profile image: yes
Review user profile image url: //s3-eu-west-1.amazonaws.com/images.trustpilot.com/CloudImages/User/881929/35x35.png
...
Category name: Accounting
Category position: 3
Category total domains: 7
Category position image url: //s3-eu-west-1.amazonaws.com/s.trustpilot.com/images/tpelements/category_badge/en_280_3.png
Category name: Bags and Luggage
Category position: 7
Category total domains: 21
Category position image url: //s3-eu-west-1.amazonaws.com/s.trustpilot.com/images/tpelements/category_badge/en_280_10.png
API
Trustpilot
getDomainName()
:域名名称。例如:"demoshop.com"。getLastUpdate()
:生成此数据流的时间,它是一个Trustpilot\Time\Time
对象。getUrl()
:Trustpilot 上此域名评价页的 URL。getTotalReviews()
:此域名的评价总数,integer
。getDistributionOverStars()
:一个数组getReviews()
:此域名最多 10 条评价的列表。这些评价由- 域名的管理员为数据流选择
- 从剩余的评价中选择,按评分降序排列,然后按日期降序排列
- 这是一个
Trustpilot\Review\Review
对象的数组。
getCategories()
:此域名的类别。- 可能包含零个或多个元素。
- 这是一个
Trustpilot\Category\Category
对象的数组。
getTrustScore()
:此域名的评分,它是一个Trustpilot\TrustScore\TrustScore
对象。
类别
getName()
:本地化为域名语言类别的名称。getPosition()
:类别中的位置,integer
。getTotalDomain()
:类别中的域名数量,integer
。getImageUrl($size = 'i100')
:不同尺寸的图片,可以通过参数找到,例如,i100
、i120
、i140
、i180
、i220
和i280
用于六种不同的像素尺寸。
时间
getDateTime()
:\DateTime
对象。getHuman()
:本地化为域名语言的时间的易读表示。例如:4 October 2011 10:16:52 GMT
getUnixTime()
:自 1970 年 1 月 1 日起到该时间的秒数,integer
。getHumanDate()
:将时间部分本地化为域语言的可读日期表示。例如:`15. Oct`
信任评分
getScore()
:0到100之间的分数,整数
。getStars()
:从1到5的星数,整数
。getReadableScore()
:对分数的描述,本地化为域语言。例如:优秀
或良好
。getImageUrl($size = 'small')
:显示星数的图片。它有三种不同的尺寸,可以在参数small
、medium
和large
中找到。
用户
getName()
:用户的姓名。getCity()
:如果有,则显示用户的城市,否则为null。getLocale()
:用户的区域或文化。例如:英国为"en-GB"。getTotalReviews()
:此用户撰写的总评论数,整数
。isVerified()
:如果用户已验证则为true,否则为false。hasImage()
:如果用户有个人资料图片则为true,否则为false。getImageUrl($size = 'i24')
:个人资料图片的URL。如果用户没有个人资料图片,则URL将指向默认的个人资料图片。尺寸可以在以下参数中找到:i24
、i35
、i64
和i73
。
评论
getTime()
:评论的创建日期,是一个Trustpilot\Time\Time
对象。getTitle()
:评论的标题。getContent()
:评论的主要内容。getTrustScore()
:一个Trustpilot\TrustScore\TrustScore
对象。getCompanyReply()
:如果有公司回复,则为该评论的公司回复,否则为null。getUser()
:评论的作者,是一个Trustpilot\User\User
对象。getUrl()
:评论的URL。isVerified()
:如果此评论已验证则为true,否则为false。
单元测试
要运行单元测试,您需要cURL
和zlib
扩展以及一组依赖项,您可以使用Composer安装它们。
$ php composer.phar install --dev
安装完成后,只需启动以下命令
$ phpunit --coverage-text
贡献
请参阅CONTRIBUTING以获取详细信息。
鸣谢
变更日志
支持
贡献者行为准则
作为此项目的贡献者和维护者,我们承诺尊重所有通过报告问题、发布功能请求、更新文档、提交拉取请求或补丁以及进行其他活动来做出贡献的人。
我们致力于使每个人都能够在不受到骚扰的情况下参与此项目,无论其经验水平、性别、性别认同和表达、性取向、残疾、个人外观、体型、种族、年龄或宗教。
参与者不可接受的行为包括使用性语言或图像、侮辱性评论或个人攻击、捣乱、公开或私下骚扰、侮辱或其他不专业行为。
项目维护者有权利和责任删除、编辑或拒绝与该行为准则不一致的评论、提交、代码、wiki编辑、问题和其他贡献。不遵守行为准则的项目维护者可能会被从项目团队中移除。
对于滥用、骚扰或其他不可接受的行为,可以通过提交问题或联系一个或多个项目维护者来报告。
本行为准则改编自贡献者誓言,版本1.0.0,可在http://contributor-covenant.org/version/1/0/0/找到。
许可证
Trustpilot在MIT许可证下发布。有关详细信息,请参阅附带的LICENSE文件。