2013-04-18 86 views
2

我需要设置plimus API以获取身份验证令牌,然后使用它将客户登录到Plimus平台。设置plimus API以获取身份验证令牌

我使用下面的代码:

$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"); 

我碰到下面的错误,不知道为什么:

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 

感谢

回答

1

它看起来就像是我的代码,摘自文章第二部分:How to Improve Customer-Experience and Conversion with API
:)

使用var_dump(curl_exec($ch))并查看您从服务器获得的确切响应!

+0

是的,它是你的代码,并且特别为你问题:) – user2186504

+0

@ user2186504 var_dump(curl_exec($ ch))''的输出是什么? – alfasin

+0

正如你已经看到的,完全相同的脚本对我来说工作正常:http://alfasin.com/get_token.php?target=step1所以我的猜测是你传递的参数有一个错误(shopperId/contractId/etc)或凭证。 Firefox上有一个很棒的插件Poster - 这是一个用于调试HTTP调用的好工具! – alfasin