I am attempting to convert this PHP code in winbatch.
This code is used to purchase Loans.
It has a json component and a PHP array which I am not sure how to handle.
Any help would be really appreciated.
Here is the winbatch code so far:
lc_InvestorId = "516xxxx"
lc_apikey = 'I18Osxxxxxxxxxxxxxxxxxx'
;$note[] = array("loanId" => "96188415", "orderId" => "157984483", "noteId" => "146150819", "bidPrice" => "14.37");
;$datas = array("aid" => $invester_id, "notes" => $note);
URL_BuyNotes = "https://api.lendingclub.com/api/investor/v1/accounts/%lc_InvestorId%/trades/buy"
URL_Var = URL_BuyNotes
GoSub CallAPI_LC
exit
;*****************************
:CallAPI_LC
Api_Error = 0
oHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
oHttp.SetTimeouts(50000, 50000, 50000, 50000);
oHttp.open("GET",URL_Var,"False")
strAuth='Bearer ':lc_apikey
oHttp.SetRequestHeader("Authorization", lc_apikey )
oHttp.SetRequestHeader("Accept", "application/json")
oHttp.SetRequestHeader("Content-Type", "application/json")
oHttp.Send()
if oHttp.Status != 200
If oHTTP.Status == 302
Display(2,App, strcat("Server Attempted Redirect to: ", oHTTP.getResponseHeader("Location"))
EndIf
Status = oHTTP.Status
StatusText = oHTTP.StatusText
headers = oHTTP.GetAllResponseHeaders()
;Pause(oHTTP.Status, headers)
Api_Error = 1
endif
strResponse=oHttp.responseText
Return
Here is the PHP code
<?php
$invester_id = "516xxxx";
$authkey = "I18Osxxxxxxxxxxxxxxxxxx";
define("DEBUG_LENDING_API", false);
$buy = buy_notes($invester_id, $authkey);
print_r($buy);die;
function buy_notes($invester_id, $authkey){
$buy_notes_url = "https://api.lendingclub.com/api/investor/v1/accounts/$invester_id/trades/buy";
$note[] = array("loanId" => "96188415", "orderId" => "157984483", "noteId" => "146150819", "bidPrice" => "14.37");
$datas = array("aid" => $invester_id, "notes" => $note);
$buy_notes = call_curl($buy_notes_url, $authkey, json_encode($datas));
$notes = json_decode($notes['data']);
return $notes;
}
function call_curl($url, $authkey, $post = "0"){
$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);
}
$headers = array();
$headers[] = "Authorization: $authkey";
$headers[] = "Content-type: $ContentType";
$headers[] = "Accept: $ContentType";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$server_output = curl_exec ($ch);
echo $server_output. "<br>";
$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);
}
}
?>
Appears you are getting a response text, and I assume it may be formatted as JSON. Without giving away any secrets could you post the structure of the response or overwrite with fake data. There are ways in WB to decode JSON whether from PHP or JScript.
Here is the response (output of $server_output ) when the order is accepted:
{"buyNoteConfirmations":[{"loanId":96188415,"noteId":146150819,"bidPrice":14.37,"outstandingAccruedInterest":null,"outstandingPrincipal":null,"yeildToMaturity":null,"executionStatus":["SUCCESS_PENDING_SETTLEMENT"],"txnId":null}]}
Or if the loan was already purchased then:
{"buyNoteConfirmations":[{"loanId":96188415,"noteId":146150819,"bidPrice":14.37,"outstandingAccruedInterest":null,"outstandingPrincipal":null,"yeildToMaturity":null,"executionStatus":["NOTE_NOT_AVAILABLE"],"txnId":null}]}
Seems correct JSON. I would suggest you try to incorporate the snippets [below] I posted and modify your script to include the Jscript EVAL UDF. Your basic script returns JSON... how that is decoded is up to several WB alternatives (although the Jscript is rather obvious to me for your situation). NOTE: there is a PHP dll you can call from WB which does about the same as Jscript.
http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/tsleft.web+WinBatch/OLE~COM~ADO~CDO~ADSI~LDAP/WinHttpRequest+Parse~JSON~Using~Eval.txt
Thanks Stan. This is great.
I am sure I will have some questions