user2186504 user2186504 - 6 months ago 23
PHP Question

Setting plimus API in order to fetch an authentication token

I need to set up plimus API in order to fetch an authentication token and then use it to log the customer into Plimus platform.

I'm using to following code:

$URL = "https://sandbox.plimus.com/services/2/tools/auth-token?shopperId=$shopperId&expirationInMinutes=$expiration";

// use base64 to encode the credentials
$authorization = base64_encode($username.':'.$password);

$ch = curl_init();
// set URL
curl_setopt_array($ch, array(CURLOPT_URL => $URL));
// set headers
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Basic $authorization", "Content-type: application/xml")); // This line is mandatory for every API call!
// set HTTP request to GET
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); // This service (get token) is implement via RESTful GET, other services might use POST and PUT
// stop output of curl_exec to standard output (don't send output to screen)
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
// make HTTP call and read the response into an XML object

$xml = new SimpleXMLElement(curl_exec($ch));

// -------- PART II - Log the customer into Plimus (according to $target) --------

// construct plimus URL
$plimus = "https://sandbox.plimus.com/jsp/entrance.jsp?contractId=$contractId&target=$target&token={$xml->token}";
// redirect
header("Location: $plimus");


And I get the following error and don't know why:

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as
XML' in xxx\get_token.php:40 Stack trace: #0 xxx\get_token.php(40):
SimpleXMLElement->__construct('') #1 {main} thrown in xxx\get_token.php on line 40


Thanks

Answer

It looks like it's my code, taken from the second part of the article: How to Improve Customer-Experience and Conversion with API
:)

Use var_dump(curl_exec($ch)) and see the exact response you're getting from the server!

Comments