Created
August 14, 2013 17:14
-
-
Save RaVbaker/6233181 to your computer and use it in GitHub Desktop.
[PHP] klasa do obsługi API Ceneo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Author: Rafał "RaVbaker" Piekarski <ravbaker @ gmail.com> | |
* Website: http://ravbaker.net/ | |
* License: MIT | |
* Created in: 2013 | |
*/ | |
class CeneoApi { | |
private $_api_domain = "https://partnerzyapi.ceneo.pl/"; | |
private $_api_key = null; | |
private $_authorized = false; | |
private $_token = null; | |
private $_token_type = null; | |
private $_token_expire = null; | |
function __construct($api_key) { | |
$this->_api_key = $api_key; | |
} | |
// $resource_name może także być pełnym adresem do api ceneo | |
function call($resource_name, array $arguments = array(), $try = 3) { | |
if (!$this->_isAuthorized()) { | |
$this->_authorize(); | |
} | |
$headers = array("Authorization: Bearer {$this->_token}"); | |
$arguments['$format'] = 'json'; | |
$response = $this->_callServer($resource_name, $arguments, $headers); | |
if ($response['code'] == 400 && $try > 0) { | |
$this->_authorized = false; | |
return $this->call($resource_name, $arguments, $try-1); | |
} | |
if ($response['headers']['error']) { | |
$error_description = array( | |
'error' => $response['headers']['error'], | |
'error_description' => $response['headers']['error_description'], | |
'error_uri' => $response['headers']['error_uri'] | |
); | |
return array_merge($response, $error_description); | |
} | |
return json_decode($response['body'], true); | |
} | |
private function _isAuthorized() { | |
return ($this->_authorized && time() < $this->_token_expire); | |
} | |
private function _authorize() { | |
$headers = array("Authorization: Basic {$this->_api_key}"); | |
$data = array('grantType' => '\'client_credentials\''); | |
$response = $this->_callServer('AuthorizationService.svc/GetToken', $data, $headers); | |
$this->_token = $response['headers']['access_token']; | |
$this->_token_expire = time()+$response['headers']['expires_in']; | |
$this->_token_type = $response['headers']['token_type']; | |
$this->_authorized = ($response['code'] == 200); | |
} | |
private function _callServer($path, $data = array(), $headers = array(), $post = false) { | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
// curl_setopt($ch, CURLOPT_VERBOSE, 1); | |
curl_setopt($ch, CURLOPT_HEADER, 1); | |
// pomiń ssl weryfikację: | |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,2); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0); | |
if ($post) { | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
} elseif (strpos($path, "?") === false && !empty($data)) { | |
$path .= "?".http_build_query($data); | |
} | |
// pełen adres żądania | |
if (strpos($path, "http") === false) { | |
$url = $this->_api_domain.$path; | |
} else { | |
$url = $path; | |
} | |
// ustawiamy nagłówek żądania | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
// ustawiamy adres pod jaki wchodzimy | |
curl_setopt($ch, CURLOPT_URL, $url); | |
// wywołujemy żądanie: | |
$response = curl_exec($ch); | |
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); | |
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
$header = substr($response, 0, $header_size); | |
$body = substr($response, $header_size); | |
curl_close($ch); | |
$assoc_response = array('body' => $body, 'url' => $url, 'path' => $path, 'data' => $data, 'code' => $code); | |
$assoc_response['headers'] = $this->_httpParseHeaders($header); | |
return $assoc_response; | |
} | |
private function _httpParseHeaders($raw_headers) { | |
if (function_exists('http_parse_headers')) { | |
return http_parse_headers($raw_headers); | |
} | |
$headers = array(); | |
foreach (explode("\n", $raw_headers) as $i => $h) { | |
$h = explode(':', $h, 2); | |
if (isset($h[1])) { | |
if(!isset($headers[$h[0]])) { | |
$headers[$h[0]] = trim($h[1]); | |
} else if(is_array($headers[$h[0]])) { | |
$tmp = array_merge($headers[$h[0]],array(trim($h[1]))); | |
$headers[$h[0]] = $tmp; | |
} else { | |
$tmp = array_merge(array($headers[$h[0]]),array(trim($h[1]))); | |
$headers[$h[0]] = $tmp; | |
} | |
} | |
} | |
return $headers; | |
} | |
} | |
$api_key = "PUT_API_KEY_HERE"; | |
$api = new CeneoApi($api_key); | |
$r = $api->call('PartnerService.svc/Categories'); | |
print_r($r); | |
$r = $api->call('PartnerService.svc/Categories(40)'); | |
print_r($r); | |
$r = $api->call('http://partnerzyapi.ceneo.pl/PartnerService.svc/Categories(40)/Products'); | |
print_r($r); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment