bitly api
Deprecation info
檢查 phplicengine/bitly 作為可能的替代品。
基於 Guzzle 的 PHP 函式庫來使用 Bit.ly API。
使用 Guzzle 的最大優點是您可以輕鬆地將 Guzzle 外掛程式附加到您的用戶端。例如,在這裡,您可以看到如何附加日誌外掛並將所有請求寫入檔案。
還可以與 Symfony2 整合。
master
遵循psr4標準並取得2.x
標籤psr0
遵循psr0標準並獲得1.x
標籤 - 沒有新功能,只有錯誤修復該項目遵循語義版本控制。
建議的安裝此程式庫的方法是透過 Composer。有關 Composer 以及如何安裝的資訊請查看此處。
從命令列運行
./composer create-project hpatoio/bitly-api your_prj_dir ' ~2.0 '
進入您的專案目錄並運行
./composer require hpatoio/bitly-api ' ~2.0 '
或添加到你的composer.json
{
...
"require" : {
...
"hpatoio/bitly-api" : " ~2.0 "
}
}
並運行
./composer update
<?php
// This file is generated by Composer
require_once ' vendor/autoload.php ' ;
# To find your bitly access token see here https://bitly.com/a/oauth_apps
$ my_bitly = new Hpatoio Bitly Client ( " insert_here_your_bitly_api_access_token " );
$ response = $ my_bitly -> Highvalue ( array ( " limit " => 3 ));
print_r ( $ response );
可能是 bit.ly 無法訪問,並且您想要設定特定的逾時。只需在客戶端中設定 cURL 逾時選項:
$ my_bitly = new Hpatoio Bitly Client ( " insert_here_your_bitly_api_access_token " );
// set cURL timeout, you can specify any cURL options
$ my_bitly -> setConfig ( array (
' curl.options ' =>
array (
CURLOPT_TIMEOUT => 2 ,
CURLOPT_CONNECTTIMEOUT => 2
)
));
$ response = $ my_bitly -> Highvalue ( array ( " limit " => 3 ));
print_r ( $ response );
要取得方法名稱,請從 API url 中刪除“v3”,並將其他單字駝峰化並刪除斜線。
範例:
目前該庫支援以下 API:
您需要複製 Behat 預設設定檔並在其中輸入您的access_token
選項。
$ cp behat.yml.dist behat.yml
現在打開behat.yml
並使用您的訪問令牌更改字串your_bitly_access_token_here
。運行套件輸入
$ bin/behat
整合該程式庫的 Symfony2 套件可在此處取得
在這裡您可以看到如何將 Guzzle Log 外掛程式附加到您的客戶端並將所有請求儲存到檔案中。
注意:要執行此腳本,您需要monolog/monolog
<?php
// This file is generated by Composer
require_once ' vendor/autoload.php ' ;
use Guzzle Log MessageFormatter ;
use Guzzle Log MonologLogAdapter ;
use Guzzle Plugin Log LogPlugin ;
use Monolog Handler StreamHandler ;
use Monolog Logger ;
$ logger = new Logger ( ' client ' );
$ logger -> pushHandler ( new StreamHandler ( ' /tmp/bitly_guzzle.log ' ));
$ adapter = new MonologLogAdapter ( $ logger );
$ logPlugin = new LogPlugin ( $ adapter , MessageFormatter:: DEBUG_FORMAT );
# To find your bitly access token see here https://bitly.com/a/oauth_apps
$ my_bitly = new Hpatoio Bitly Client ( " your_bitly_access_token " );
$ my_bitly -> addSubscriber ( $ logPlugin );
$ response = $ my_bitly -> Highvalue ( array ( " limit " => 3 ));
print_r ( $ response );
現在在/tmp/bitly_guzzle.log
中您可以看到您的所有請求。