/** * Send a POST request to the API * * @param SimpleXmlElement $params Parameters to pass to the requested resource * @param string $pathExtension Defaults to XML API endpoint * * @return SimpleXmlElement Returns an XML response object * @throws SilverpopConnectorException */ protected function post($params, $pathExtension = '/XMLAPI', $urlParams = '') { // Wrap the request XML in an "envelope" element $envelopeXml = "<Envelope>\n\t<Body>\n"; $params = $params->asXml(); $paramLines = explode("\n", $params); $paramXml = ''; for ($i = 1; $i < count($paramLines); $i++) { $paramXml .= "\t\t{$paramLines[$i]}\n"; } $envelopeXml .= $paramXml; $envelopeXml .= "\n\t</Body>\n</Envelope>"; $xmlParams = http_build_query(array('xml' => $envelopeXml)); $curlHeaders = array('Content-Type: application/x-www-form-urlencoded', 'Content-Length: ' . strlen($xmlParams)); // Use an oAuth token if there is one if ($accessToken = SilverpopRestConnector::getInstance()->getAccessToken()) { $curlHeaders[] = "Authorization: Bearer {$accessToken}"; $url = $this->baseUrl . '/XMLAPI'; } else { // No oAuth, use jsessionid to authenticate $url = $this->baseUrl . "/XMLAPI;jsessionid={$this->sessionId}"; } $ch = curl_init(); $curlParams = array(CURLOPT_URL => $url, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_POST => 1, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_MAXREDIRS => 3, CURLOPT_POSTFIELDS => $xmlParams, CURLOPT_RETURNTRANSFER => 1, CURLOPT_HTTPHEADER => $curlHeaders); curl_setopt_array($ch, $curlParams); $result = curl_exec($ch); curl_close($ch); return $this->checkResponse($result); }
/** * Performs Silverpop authentication using the supplied REST credentials, * or with the cached credentials if none are supplied. Any new credentials * will be cached for the next request. * * @param string $clientId * @param string $clientSecret * @param string $refreshToken * * @throws SilverpopConnectorException */ public function authenticateRest($clientId = null, $clientSecret = null, $refreshToken = null) { $this->clientId = empty($clientId) ? $this->clientId : $clientId; $this->clientSecret = empty($clientSecret) ? $this->clientSecret : $clientSecret; $this->refreshToken = empty($refreshToken) ? $this->refreshToken : $refreshToken; $this->restConnector = SilverpopRestConnector::getInstance(); return $this->restConnector->authenticate($this->clientId, $this->clientSecret, $this->refreshToken); }