PHP 框架,用于与 Hattrick 网站相关的 CHPP 应用程序

3.13 2024-09-18 17:50 UTC

This package is auto-updated.

Last update: 2024-09-18 17:54:00 UTC


README

工作正在进行中,欢迎 fork、修复、增强或在大 pht 论坛上提出想法、观点、评论

PHT 遵循 psr-0、psr-1、psr-2 建议,日志系统符合 psr-3 规范

CHPP OAuth 连接与主 pht 对象分离,请参考以下代码示例

许多类中都有大量的链式函数,因此您可以轻松获取额外数据,但请记住,如果不使用缓存,则每次都会加载 XML

别忘了使用 UTF-8 编码

祝您测试愉快!

要求

PHP 5.3+

扩展

  • curl
  • dom
  • libxml
  • pcre
  • 反射
  • xml

用于缓存的目的可选

  • session
  • apc
  • memcached

/!\ 请仔细阅读,此示例不完整,请阅读注释并根据您的需求保存数据

连接

<?php
require_once 'PHT/autoload.php';
$config = array(
    'CONSUMER_KEY' => 'xxxxxxxxxxxxxx',
    'CONSUMER_SECRET' => 'xxxxxxxxxxxxx'
);
$HT = new \PHT\Connection($config);
$auth = $HT->getPermanentAuthorization('http://your-server/callback-page.php'); // put your own url :)
if ($auth === false) {
    // handle failed connection
    echo "Impossible to initiate chpp connection";
    exit();
}
$tmpToken = $auth->temporaryToken; // save this token somewhere (session, database, file, ...) it's needed in next step
header('Location: ' . $auth->url); // redirect to hattrick login page, or get the url and show a link on your site
exit();
?>

在回调页面文件中

<?php
require_once 'PHT/autoload.php';
$config = array(
    'CONSUMER_KEY' => 'xxxxxxxxxxxxxx',
    'CONSUMER_SECRET' => 'xxxxxxxxxxxxx',
    'CACHE' => 'none',
    'LOG_TYPE' => 'file',
    'LOG_LEVEL' => \PHT\Log\Level::DEBUG,
    'LOG_FILE' => __DIR__ . '/pht.log',
);
$HT = new \PHT\Connection($config);
// retrive the $tmpToken saved in previous step
$access = $HT->getChppAccess($tmpToken, $_REQUEST['oauth_token'], $_REQUEST['oauth_verifier']);
if ($access === false) {
    // handle failed connection
    echo "Impossible to confirm chpp connection";
    exit();
}
// if you want to save user credentials for future use
// do it now by saving $access->oauthToken and $access->oauthTokenSecret
// then you can request xml data
$config['OAUTH_TOKEN'] = $access->oauthToken;
$config['OAUTH_TOKEN_SECRET'] = $access->oauthTokenSecret;
try {
    $HT = new \PHT\PHT($config);
    echo $HT->getClub()->getTeamName();
    // then explore PHT\PHT object, all requests start from there, another example:
    $league = $HT->getSeniorLeague();
    echo $league->getCountry()->getEnglishName(); // chaining with country
    foreach($league->getTeams() as $team)
    {
        // chaining with team details
        echo $team->getPosition().' : '.$team->getName().' ('.$team->getTeam()->getShortName().')<br/>';
    }
    
    // you can handle bad argument without going out of main process
    try {
        $HT->setBid(12345, 123456789, \PHT\Utils\Money\FRANCE, -1000);
    } catch(\PHT\Exception\InvalidArguementException $e) {
        echo $e->getMessage(); // amount must be positive
    }

    // look at your log file to see cache in action
    echo $HT->getClub()->getTeamName(); // no xml request done;
    // clear the cache for club file
    \PHT\Cache\Driver::getInstance()->clear('club');
    // request xml again:
    echo $HT->getClub()->getTeamName();
    
    // get HTI team
    $teamConf = new \PHT\Config\Team();
    $teamConf->international = true;
    echo $HT->getSeniorTeam($teamConf)->getName();

} catch(\PHT\Exception\ChppException $e) {
    // chpp request returns xml content
    // but response generate an error
    // maybe wrong data, maybe element not found, ...
    echo $e->getErrorCode().': '.$e->getError();
    // you can also get whole xml response like any other chpp request:
    echo $e->getXml(false);

} catch(\PHT\Exception\NetworkException $e) {
    // chpp request does not return xml
    // so probably html content due to server down or server error
    echo $e->getError();
}
?>

配置参数列表

CONSUMER_KEY           : your chpp app consumer key
CONSUMER_SECRET        : your chpp app consumer secret
OAUTH_TOKEN            : chpp user token
OAUTH_TOKEN_SECRET     : chpp user token secret
HT_SUPPORTER           : override ht supporter level, by default 0 (0=no change, -1=deactivate, 1=activate)
STARTING_INDEX         : used for loop, by default 0 (pht v2 has starting index at 1)
PROXY_IP               : set your proxy ip
PROXY_PORT             : set your proxy port
PROXY_USER             : set your proxy username
PROXY_PASSWORD         : set your proxy password
LOG_TYPE               : set log type, pht provides: 'file' and 'none', set a classname to use another logger
LOG_LEVEL              : set minimum level of log (see \PHT\Log\Level constants)
LOG_TIME               : set log time format (use php date() format)
LOG_FILE               : set log filename, prefer full path name
CACHE                  : set cache mechanism you want to use: 'none', 'apc', 'session', 'memory', 'memcached'. default: 'none'
CACHE_PREFIX           : set a prefix for cache key, default 'PHT_',
CACHE_TTL              : set a default ttl in seconds for caching xml request, default: 3600
MEMCACHED_SERVER_IP    : set ip of memcached server
MEMCACHED_SERVER_PORT  : set port of memcached server