endpoint = 'https://www.landingiexport.com'; $this->exportHash = $exportHash; } public function getLanding($hash) { return $this->modifyLandingContent($this->prepareCurl($hash)); } private function prepareBaseUrl() { return $this->endpoint.'/api/render?' . http_build_query(array('export_hash' => $this->exportHash)); } private function prepareConversionUrl($hash) { return $this->endpoint.'/api/render?' . http_build_query( array ( 'conversion_hash' => $hash, 'export_hash' => $this->exportHash ) ); } private function chooseWhichUrl($hash) { if (!$hash) { return $this->prepareBaseUrl(); } return $this->prepareConversionUrl($hash); } /** * @param $hash * @return bool|mixed */ private function prepareCurl($hash) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->chooseWhichUrl($hash)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); if (isset($_COOKIE['tid'])) { $tidCookie = $_COOKIE['tid']; curl_setopt($ch, CURLOPT_COOKIE, "stg-tracker=tid=$tidCookie"); } curl_setopt($ch, CURLOPT_TIMEOUT, 10); $data = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $curlErrorCode = curl_errno($ch); $curlErrorMsg = curl_error($ch); curl_close($ch); if ($curlErrorCode || $curlErrorMsg) { exit(sprintf('check curl settings: %s %s', $curlErrorCode, $curlErrorMsg)); } if (302 === $httpcode && is_string($data) && $data = json_decode($data)) { $getParams = $this->unsetExportParam($data->redirect); header('Location: ' . $getParams); } if ((404 === $httpcode && is_string($data)) || ($httpcode >= 200 && $httpcode <300)) { $data = json_decode($data); setcookie('tid', $data->tid, time() + self::MONTH); return $data; } return $data; } private function unsetExportParam($queryStr) { $parser = parse_url($queryStr); $newQuery = []; if (isset($parser['query'])) { parse_str($parser['query'], $newQuery); } $parser['query'] = array_merge($newQuery, $_REQUEST); foreach (['tid', 'export_hash'] as $param) { unset($parser['query'][$param]); } $parser['query'] = http_build_query($parser['query']); return $this->buildRedirectUrl($parser); } private function buildRedirectUrl($parser) { $return = ''; if (isset($parser['scheme'])) { $return .= $parser['scheme'] . '://'; } if (isset($parser['host'])) { $return .= $parser['host']; } if (isset($parser['path'])) { $return .= $parser['path']; } if (isset($parser['query'])) { $return .= '?' . $parser['query']; } return $return; } private function getActualLink() { return (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; } private function modifyLandingContent($data) { return preg_replace( '/(/', sprintf( '$1="%s">', $this->getActualLink() ), preg_replace( '/ action="([\s\S]*?)"/', sprintf( ' action="%s${1}?export_hash=%s&tid=%s"', $this->endpoint, $this->exportHash, $data->tid ), $data->content ) ); } } const BASE = "Pb1dV2uVua1S540rJe3D"; $landingHash = null; $landing = new LandingExportPhp(BASE); if (isset($_REQUEST['hash'])) { $landingHash = $_REQUEST['hash']; } $landing = $landing->getLanding($landingHash); echo $landing;