如何从PHP中获取REST API的LENDDO分数

时间:2016-04-19 09:30:23

标签: php api rest curl

如何从PHP中获取REST API的LENDDO分数 我的客户ID = LEDEMO1070168781157742500
对于上面的client_id,我的Lenddo得分是480,它显示在我的Lenddo仪表板上但没有通过API得到这个分数。
不共享我的API和密钥。
基本上我需要帮助来生成我在signRequest()函数中执行的AUTHORIZATION HEADER。
引用= https://www.lenddo.com/documentation/rest_api.html

/*
<?php
$method = "GET";
$date = date("D M j G:i:s T Y");
$url = '/ClientScore/LEDEMO1070168781157742500'; 

function signRequest($method, $body, $date, $url) { 
$api = '';       //my api key
$secret = '';    // my secret key 
$contentMd5 = NULL;
if( !empty( $body ) ) { 
    $contentMd5 = md5( $body );
}
$stringToSign = $method . "\n" . $contentMd5 . "\n" . $date . "\n" . $url; 
$string = "LENDDO " . $api . ":"; 



$string .= base64_encode(
    hash_hmac( "sha1", $stringToSign, $secret, TRUE )
);
return $string; 
}
$val = signRequest('GET', '', $date, $url);  // get the access token



 $ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://scoreservice.lenddo.com/ClientScore/LEDEMO1070168781157742500");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);

$headers = array('Authorization: Bearer ' . $val);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);
curl_close($ch);

var_dump($response);
// the above source code gives me the output => string(141) "{"message": "Signature generated from request data does not match the signature provided in the Authorization Header.", "name": "FORBIDDEN"} "
// i pass my api and secret key into the code above but got this error, help me to find Lenddo Score.
?>

1 个答案:

答案 0 :(得分:1)

我是Lenddo的首席开发人员。看来你正在采取艰难的整合方式。值得庆幸的是PHP(5.3+)我们实际上提供了一个SDK,它可以使集成变得相当轻松。

您可以在此处找到PHP SDK:https://github.com/Lenddo/php-lenddo。在此页面上是README,您可以在其中找到目录,更具体地说是installation instructions

由于您尝试检索分数,因此可以使用REST Service Client。在此页面上,您将找到有关检索分数的说明。

为了帮助您快速启动,我将在此处提供一份摘要说明列表(如今所示):

  1. Install composer
  2. 执行以下命令:composer require lenddo/sdk
  3. 将以下代码段粘贴到空的PHP文件中:
  4. <?php
    
    // Fill out the ID & Secret provided to you by your contact at Lenddo.
    $id = '';
    $secret = '';
    
    // Require the Composer autoloader
    require 'vendor/autoload.php';
    
    // Instantiate the Lenddo Service Client
    $client = new Lenddo\ServiceClient( $id, $secret );
    
    // Replace the APPLICATION_ID with your client/application ID
    // Replace the PARTNER_SCRIPT_ID with your partner script id
    $response = $client->applicationScore('APPLICATION_ID', 'PARTNER_SCRIPT_ID');
    
    // Get the Status Code for the response
    $status_code = $response->getStatusCode(); // 200
    
    // Retrieve the body of the response
    $score_results = $response->getBody();
    
    // Return the score value and reason flags.
    $score_value = $score_results->score;
    $score_flags = $score_results->flags;
    
    echo $score_value;
    
    1. 关注所提供的代码示例的所有内嵌注释。