2

I am attempting to buy Notes of the secondary market of Lending club and I keep getting "Internal server error". I have asked Lending club support multiple times too, but they are clueless. I also tried following this post , but no luck LendingClub.com API 500 Error for Buying Notes on Secondary Market.

Please help

<?php
$invester_id = "516xxxxxx";
$api_key = "GVsZuxxxxxxxxx"; 
$ContentType = "application/json";
define("DEBUG_LENDING_API", true);

$buy = buy_notes($invester_id, $api_key);
print_r($buy);die;

function buy_notes($invester_id, $api_key){
    $buy_notes_url = "https://api.lendingclub.com/api/investor/v1/accounts/$invester_id/trades/buy";
    $note = array("loanId" => "97277470", "orderId" => "139320895", "noteID" => "149206918", "bidPrice" => "19.45");
    $datas = array("aid" => "70654", "notes" => $note);
    $buy_notes = call_curl($buy_notes_url, $api_key, json_encode($datas));
    $notes = json_decode($buy_notes['data']);
    return $notes;
  }

function call_curl($url, $api_key, $post = "0"){
   $invester_id = "516xxxxxx";
   $ContentType = "application/json";
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt ( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0" );
   if($post != "0"){
     curl_setopt($ch,CURLOPT_POST, 1);
     curl_setopt($ch,CURLOPT_POSTFIELDS, $post);
   }
   curl_setopt ( $ch, CURLOPT_AUTOREFERER, true );
   curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, true );
   $headers = array();
   $headers[] = "Authorization: $api_key";
   $headers[] = "Content-type: $ContentType";
   $headers[] = "Accept: $ContentType";
   $headers[] = "X-LC-Application-Key: $invester_id";
   //print_r(array_values($headers));
   //exit;
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   $server_output = curl_exec ($ch);
   echo $server_output. "<br>";
   exit;
   $info = curl_getinfo($ch);
   curl_close ($ch);
   if(DEBUG_LENDING_API == true){
     return array("data" => $server_output, "response" => $info);
   }else{
     return json_decode($server_output);
   }
  }
?>
hdsouza
  • 354
  • 4
  • 17
  • 2
    Are you getting the 500 from your server, or theirs? I can see 3 places where your code would throw a 500 locally, as `$ContentType`, `$invester_id`, and `$notes` are not defined within the scope of the functions. – aynber Dec 27 '17 at 20:16
  • 1
    That's the exact same thing. An Internal Server Error returns an HTTP status of 500. – aynber Dec 27 '17 at 20:24
  • $ContentType and $invester_id have already been defined at the top of the script. If you scroll up you will see them. $notes is defined at $notes = json_decode($notes['data']); – hdsouza Dec 27 '17 at 20:56
  • 1
    ContentType and invester_id do not exist within the scope of the function. See the [php documentation](http://php.net/manual/en/language.variables.scope.php) and the [SO answer](https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) for more information. `$notes = json_decode($notes['data']);`, the first `$notes` is okay, but `$notes` is not previously defined so `json_decode` can't decode anything. You probably mean `$notes = json_decode($buy_notes['data']);` – aynber Dec 27 '17 at 21:00
  • Thanks Aynber. I have made the necessary changes and updated the code above. I see that the script breaks on line $server_output. – hdsouza Dec 27 '17 at 22:23

1 Answers1

2

I managed to figure it out. I set "aid" to $invester_id and that finally did the trick. Thanks Aynber for your tips along the way.

hdsouza
  • 354
  • 4
  • 17