<?xml version="1.0" encoding="UTF-8"?> <xml-result xmlns="http://api.freelancer.com/schemas/xml-0.1"> <id>588582</id> <name>sign design web software</name> <url>http://www.sandbox.freelancer.com/projects/PHP-ASP/sign-design-web-software.html</url> <buyer> <url>http://www.sandbox.freelancer.com/users/1353095.html</url> <id>1353095</id> <username>billk89</username> </buyer> <short_descr>Create and upload sign design web software for our client nlsigndesign.com Public internet users must be able to create there own signs online, then save and submit as a file. </short_descr> <jobs> <job>PHP</job> </jobs> ... </xml-result>
<response> .... <profile> .... <buyer> <cnt_assignments>0</cnt_assignments> <op_contract_date>December 18, 2011</op_contract_date> <timezone>Russia (UTC+06)</timezone> </buyer> <op_title>A social network client app for iPhone/iPad</op_title> <ciphertext>~~05d405f2d5b8eb27</ciphertext> .... <op_required_skills> <op_required_skill> <skill>ipad,ui-design,iphone-development</skill> </op_required_skill> </op_required_skills> .... <op_desc_digest> Hello, We need a social network client app for iPhone/iPad to be developed. It should support Facebook, Twitter and Linked-In. </op_desc_digest> .... </profile> </response>
interface ServiceInterface { public function authorize(); public function getServiceName(); public function setCredential(ModelCredential $credential); public function searchProjects(); }
class FreelancerService implements ServiceInterface { private $_serviceName = 'freelancer'; private $_service = null; public function __construct(ModelCredential $credential = null) { Yii::import('ext.freelancer-api-wrapper.*'); $this->_service = new Freelancer(Yii::app()->params['freelancer']['token'],Yii::app()->params['freelancer']['secret'], 'http://geeks-board.local/authorizeService/freelancer/?'); if($credential !== null) { $this->setCredential($credential); } } public function authorize() { if(!isset($_GET['oauth_token'])) { $requestToken = $this->_service->requestRequestToken(); $redirectUrl = $this->_service->getRedirectUrl($requestToken); header('Location: ' . $redirectUrl); } else { $oauth_verifier = $this->_service->getRequestTokenVerifier($_GET['oauth_token']); $auth = $this->_service->requestAccessToken($oauth_verifier,$_GET['oauth_token']); $this->_service->oauth->setToken($auth['oauth_token'],$auth['oauth_token_secret']); $this->_service->auth = $auth; return $auth; } return false; } public function getServiceName() { .... } public function setCredential(ModelCredential $credential) { .... } public function searchProjects() { .... } }
class OdeskService implements ServiceInterface { private $_serviceName = 'odesk'; private $_service = null; public function __construct(ModelCredential $credential = null) { Yii::import('ext.odesk-api.*'); Yii::import('classes.mapper.service.RequestMapper'); $this->_service = new oDeskAPI(Yii::app()->params['odesk']['secret'], Yii::app()->params['odesk']['token']); if($credential !== null) { $this->setCredential($credential); } } public function authorize() { return $this->_service->auth(); } public function getServiceName() { .... } public function setCredential(ModelCredential $credential) { .... } public function searchProjects() { .... } }
class ServiceFactory { public static function create($serviceName, $credentials = null) { $className = ucfirst(strtolower($serviceName)).'Service'; $serviceObject = Yii::createComponent($className, $credentials); if(!($serviceObject instanceof ServiceInterface)) { throw new Exception('Not an instance of Service'); } return $serviceObject; } }
Yii::import('application.models.ModelCredential'); // - , Credentials - // . $credentialsRecords = ModelCredential::model()->findAllByAttributes(array( 'type' => 'public' )); if(!empty($credentialsRecords)) { Yii::import('classes.service.ServiceFactory'); $aggregatedProjects = array(); foreach($credentialsRecords as $serviceCredential) { try { // credentials. $service = ServiceFactory::create($serviceCredential->service, $serviceCredential); $foundProjects = $service->searchProjects(); // if(is_array($foundProjects)) $aggregatedProjects = array_merge($aggregatedProjects,$foundProjects); } catch(Exception $e) { print_r($e); } } } else { echo 'empty '. "\n"; }
Source: https://habr.com/ru/post/204436/
All Articles