sterntours/trunk/src/AppBundle/Export/_bak_SternToursCrmExporter.php
Kevin Adametz a37785b391 01 2020
2020-02-14 10:19:06 +01:00

123 lines
No EOL
4 KiB
PHP

<?php
/**
* @author Ulrich Hecht <ulrich.hecht@hecht-software.de>
* @date 02/21/2017
*/
namespace AppBundle\Export;
use AppBundle\Util;
use Monolog\Logger;
abstract class SternToursCrmExporter
{
//auto
const API_URL_LOCAL = 'http://cms-stern-tours.local/api';
const API_v3_URL_LOCAL = 'http://mein.sterntours.local/';
const API_URL = 'https://cms.stern-tours.net/api';
const API_v3_URL = 'https://mein.sterntours.de/';
const API_KEY = 'f6077389c9ce710e554763a5de02c8ec';
const API_USER_ID = 15; // 'apiuser'
const WEBSITE_ID = 1; // 'sterntours.de'
const API_v3_MAIL = 'info@mein.sterntours.de';
const API_v3_PASS = '6m9j,v2GE8px<bt75w';
protected $logger;
public function __construct(Logger $logger)
{
$this->logger = $logger;
}
protected final function httpGet($url)
{
$auth = base64_encode("star:4w28baV8xEZa0SR4");
$resp = Util::httpGet($url, ['X-ApiKey: '. self::API_KEY, 'Authorization: Basic '.$auth]);
$ret = json_decode($resp['content'], true);
if ($ret === null)
{
$this->logger->warn(get_class($this) .': Invalid server response: '. $resp['content']);
$this->logger->warn('*** Date: '. (new \DateTime())->format('d.m.Y'));
}
return $ret;
}
protected final function httpPost($context, $postData = [], $isContextFullUrl = false)
{
$baseUrl = self::API_URL;
if($_SERVER['HTTP_HOST'] == 'sterntours.local') {
$baseUrl = self::API_URL_LOCAL;
}
$url = $isContextFullUrl ? $context : $baseUrl.'/'. $context .'.json';
$auth = base64_encode("star:4w28baV8xEZa0SR4");
$resp = Util::httpPost($url, $postData, ['X-ApiKey: '. self::API_KEY, 'Authorization: Basic '.$auth], true);
return [
'content' => json_decode($resp['content']),
'location' => isset($resp['response_headers']['location'])
? $resp['response_headers']['location']
: null,
'success' => $resp['success'] && ($resp['status_code'] == 201)
];
}
protected final function httpPostAPIv3($action, $postData = [])
{
return self::loadFromApi($action, $postData);
}
protected final function loadFromApi($action, $postData){
//first - login and get token
$baseUrl = self::API_v3_URL.'api/';
if($_SERVER['HTTP_HOST'] == 'sterntours.local') {
$baseUrl = self::API_v3_URL_LOCAL.'api/';
}
$data = array(
'email' => self::API_v3_MAIL,
'password' => self::API_v3_PASS,
);
$ret = [];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $baseUrl.'login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result = curl_exec($ch);
$r = json_decode($result);
if($r->success) {
//api URL
$data = json_encode($postData);
//var_dump($data);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $r->success->token, 'Accept:application/json', 'Content-Type:application/json']);
curl_setopt($ch, CURLOPT_URL, $baseUrl.$action);
$result = curl_exec($ch);
$r = json_decode($result);
//var_dump($r);
curl_close($ch);
if(isset($r->success)) {
return $r->success;
}
if(isset($r->error)) {
$this->logger->warn('*** v3 Error: '.$r->error);
return $r->error;
}
}
return $ret;
}
}