ut-devops / immoscout24-api-php
用于与Immoscout24 REST API交互的PHP客户端
0.2.2
2022-10-18 16:19 UTC
Requires
- guzzlehttp/guzzle: ^7.4
- guzzlehttp/guzzle-services: ^1.3
- guzzlehttp/oauth-subscriber: ^0.6.0
Requires (Dev)
- phpunit/phpunit: ^9.5
README
PHP中实现immoscout24 rest api的示例。这个客户端不是完整的,只实现了导出/导入API的一些方法,但它很容易扩展。
安装
composer install ut-devops/immoscout24-api-php
用法
该库包含一个抽象类,需要由用户定义的类扩展并实现4个方法,这些方法是恢复和保存令牌所必需的。
示例实现
class MyAPI extends \ut_devops\ScoutAPI\ImmoScoutAPI
{
/**
* should save the request token with tokenname + secret, the token can be temporarily saved within a session
* this token is only needed during the "request an access token" phase.
*
* @param string $token
* @param string $secret
*
* @return void
*/
public function saveRequestToken($token, $secret){
$_SESSION['is24reqtoken'] = $token;
$_SESSION['is24reqsecret'] = $secret;
}
/**
* restores the temporarily saved request token.
*
* @return array with 2 elements: first element is the token name, second element is the secret
*/
public function restoreRequestToken(){
if(isset($_SESSION['is24reqtoken']) && isset($_SESSION['is24reqsecret'])){
return [
$_SESSION['is24reqtoken'],
$_SESSION['is24reqsecret']
];
}
return null;
}
/**
* saves the access token, the information should be stored persistently, for example in a database or file.
* the progress of getting an access token only needs to be done once per user.
*
* @param string $token
* @param string $secret
*
* @return void
*/
public function saveAccessToken($token, $secret){
file_put_contents('accessToken', serialize([$token, $secret]));
}
/**
* restores the saved access token.
*
* @return array with 2 elements: first element is the token name, second element is the secret
*/
public static function restoreAccessToken(){
if(file_exists('accessToken')) {
return unserialize(file_get_contents('accessToken'));
}
return null;
}
}
如果您想支持多个用户,您可以在类中使用 $this->getUser()。
示例用法
$key = 'my consumer key';
$secret = 'my consumer secret';
// $api->dontUseSandbox();
$api = MyAPI::createClient($key, $secret);
if ($api->isVerified()) {
// get a list of possible contacts used in a real estate
$contactArray = $api->getContacts();
var_dump($contactArray);
} else {
// this will trigger the verifiaction with immoscout24, and will try to reconnect
// to the current url else you can also use a paramater to specify the callback url
$api->verifyApplication();
}