checking database and validate form with angular - angularjs

I can use ajax to check infomation in database with jquery but I dont know do same with angularjs . I use
$http({
type : "post",
dataType : "JSON",
url : "register.php",
data : data,
success : function(result)
{
....
}
php code
$errors = array(
'error' => 0
);
$username = $_POST['username']
$password = $_POST['password']
$email =$_POST['email']
$fullname = $_POST['fullname']
$sql = "SELECT * "
. "FROM USERS "
. "WHERE username='".$username."' "
. "OR email='".$email."'";
if (mysqli_num_rows($result) > 0)
{
$row = mysqli_fetch_assoc($result);
if ($row['username'] == $username){
$errors['username'] = 'Tên đăng nhập đã tồn tại';
}
if ($row['email'] == $email){
$errors['email'] = 'Email đã tồn tại';
}
}
if (count($errors) > 1){
$errors['error'] = 1;
die (json_encode($errors));
}else{
//insert database
}
$result = mysqli_query($conn, $sql);
but I dont know do next step . I want check in database if have user name show message error else show succes .Pls help me

Using success is deprecated but you're on the right path. Here's how you would do it now:
$http({
type : "post",
url : "register.php",
data : data
}).then(function(response){
// If data is returned, do stuff with it here
console.log('Yay, my data was POSTed', response.data);
}, function(response){
console.log('Aww, it failed.');
});
It would be easier to help you further, if you add a bit more information on what you're actually trying to achieve. For instance what is returned by this "register.php" endpoint, and what you intent to do after this.

check the DOC for $http.
The $http service is a function which takes a single argument — a configuration object — that is used to generate an HTTP request and returns a promise.
The response comes under promise (.then).
$http({
type : "POST",
url : "register.php",
data : data,
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available/success
console.log(response.data)
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log(response.data)
});

Related

Angular and spring REST API : NULL POINTER EXCEPTION

I GET NullPointerException with status code : 400
I am pushing data into a JsonObject and then post it to my back end (Spring ) and get the field using Json mapper (data.get("Field name").asText;.
I also tried get(index) and data.path but this return null, where as in the debug console It indicates that Json objects were received, what is it that makes these fields to be null?
Is it because this data is not directly from ng-model, or is it because I have pushed it into json object using Angular, because I am getting 400 erro code.
MY ANGULAR JS :
app.controller("OrderOperationsCtrl", function($scope, $http,$window) {
$http.get(productURL).then(function(response) {
$scope.productData = response.data;
$scope.newData = [];
$scope.total = 0.0;
$scope.sync = function(bool,productData,submit){
if(bool === true){
var numberOrdered = 0;
numberOrdered++;
$scope.total = $scope.total + productData.price;
var newD = $scope.newData.push({
"numberOrdered":numberOrdered,
"customerId":localStorage.getItem('takealot_customer'),
"id":productData.id,
"total":$scope.total
});
$scope.sender = $scope.newData;
console.log(" NewD " + $scope.newData);
}
if(submit === true){
$http.put(orderURL,JSON.stringify($scope.sender)).then(function(response){
if(response.data)
console.log("Order created !");
},function(response){
console.log("Error creating Order !");
console.log(response.status);
});
MY SPRING CONTROLLER UPDATE
public ResponseEntity createOrder(#RequestBody OrderOperations[] operation) throws OrderOperationsNotFoundException, IOException{
logger.info("recieved json String " + operation);
services.createOrder(operation);
return new ResponseEntity(HttpStatus.OK);
}
CONSOLE OUTPUT :
Hibernate: select products0_.id as id1_3_, products0_.category as category2_3_, products0_.description as descript3_3_, products0_.name as name4_3_, products0_.price as price5_3_, products0_.quantity as quantity6_3_, products0_.supplier as supplier7_3_ from products products0_
2017-11-03 11:34:41.689 INFO 10504 --- [nio-8080-exec-9]
c.p.C.OrderOperationsController : recieved json String
[{"numberOrdered":1,"customerId":null,"id":1,"total":78.32,"$$hashKey":"object:17"},
{"numberOrdered":1,"customerId":null,"id":2,"total":156.70999999999998,"$$hashKey":"object:19"},
{"numberOrdered":1,"customerId":null,"id":1,"total":235.02999999999997,"$$hashKey":"object:21"}]
THanks in advance
First of all you dont need to map the json into the OrderOperation Object.
Spring will automatically convert the JSON Object into OrderOperations by passing as Parameter
public ResponseEntity createOrder(#RequestBody OrderOperations operation)
And i think you get the Nullpointer while you parsing the Data
Finally I have got a solution. some weeks ago.
- For those who are still struggling to save multiple objects from JSON array
see below ... I hope this helps someone else.
public List<String> createOrder(#RequestBody OrderOperations[] json) throws OrderOperationsNotFoundException, IOException{
logger.info("recieved json String " + json);
List<String> response = new ArrayList<String>();
for(OrderOperations op : json){
services.createOrder(op);
response.add("saved order : " + op.toString());
}
return response;
}

Number format exception - not able to convert string to long value

JSP code --- I have used Angular js in scripting. The issue is that the form Data is not being sent to the resource method in portlet class. Also when I log the form data in console it displays empty. It takes up JSON data and append into form data. the json value is passed as transaction data { 1: "1", purchaseRegisterId: 33411, undefined: "book3.jpg" } and documentId is passed as documentId :: documents33411. these data is taken from the console log.
$scope.savePurchaseRegisterEntry=function(documentId, json){
console.log("documentId :: "+documentId);
//console.log("json");
console.log(json);
var fd = new FormData();
fd.append('auditDetailesId',$scope.auditDetailesId);
console.log(fd);
var documentcollection=[];
for(var key in json){
fd.append(key,json[key]);
if("purchaseRegisterId"!=key && key!=='undefined'){
documentcollection.push(key);
}
}
$.each( $("#"+documentId), function( input_key, input_value ) {
$.each( input_value.files, function( file_key, file_value ) {
fd.append('myFiles', file_value);
});
});
fd.append("documentCollectionIds",documentcollection.join(','));
console.log("fd");
console.log(fd);
console.log("documentcollectionids :: "+documentcollection.join(','))
//return false;
$http({
method:'POST',
url:'<%=savePurchaseRegisterEntry%>',
type:"json",
data:fd,
headers: {'Content-Type': undefined},
transformRequest:angular.identity
}).success(function(response){
console.log(response);
var response=angular.fromJson(response.selectedTransactionList);
$scope.dataList=angular.fromJson(response.data);
$scope.ariaValuenow =response.currentCount;
$scope.ariaValuemin=0;
$scope.ariaValueMax=response.data[0]['maxInputDocCount']* response.total;
$scope.percentageComplete=(($scope.ariaValuenow - $scope.ariaValuemin) * 100) / ($scope.ariaValueMax - $scope.ariaValuemin);
if($scope.percentageComplete==100){
$scope.saveStatus();
$scope.readyForAudit=true;
}else{
$scope.readyForAudit=false;
}
});
}
Resurces Method --- the data is being fetched here to store the values into database table.
public void savePurchaseRegisterEntry(ResourceRequest req,
ResourceResponse res) throws SystemException, IOException {
_log.info("i am in savePurchaseRegisterEntry!!!");
ThemeDisplay themeDisplay = (ThemeDisplay) req
.getAttribute(WebKeys.THEME_DISPLAY);
_log.info(" ----- " + ParamUtil.getString(req, "purchaseRegisterId"));
_log.info(" ----- " + ParamUtil.getLong(req, "purchaseRegisterId"));
String documentCollectionIds=ParamUtil.getString(req, "documentCollectionIds");
String[] documentCollectionIdArray=documentCollectionIds.split(",");
MultiDocumentUploadUtil.createFolder(req, themeDisplay ,ParamUtil.getLong(req, "purchaseRegisterId") , "purchaseRegisterId");
String fileIds=MultiDocumentUploadUtil.fileUpload(themeDisplay, req);
_log.info("fileIds : "+fileIds);
long purchaseRegisterId =ParamUtil.getLong(req, "purchaseRegisterId");
for(String s : documentCollectionIdArray){
System.out.println("s is "+Long.parseLong(s));
_log.info(Long.parseLong(s)+"long sssss "+ s+" s :: "+ParamUtil.getLong(req, s));
_log.info(Long.valueOf(s)+"long sssss "+ s+" s :: "+ParamUtil.getLong(req, s));
AuditDocumentCollection adc= AuditDocumentCollectionLocalServiceUtil.fetchByPurchaseRegisterIdAnddocumentCollectionId(purchaseRegisterId, Long.valueOf(s));
AuditDocumentCollection adc1=adc;
if(adc==null){
adc=AuditDocumentCollectionLocalServiceUtil.createAuditDocumentCollection(CounterLocalServiceUtil.increment());
}
adc.setDocumentCollectionId(Long.valueOf(s));
adc.setPurchaseRegisterId(purchaseRegisterId);
adc.setValue(ParamUtil.getLong(req, s));
if(adc1==null){
adc.setCreateDate(new Date());
adc.setCreatedBy(themeDisplay.getUserId());
adc.setModifiedBy(themeDisplay.getUserId());
adc.setModifiedDate(new Date());
AuditDocumentCollectionLocalServiceUtil.addAuditDocumentCollection(adc);
}else{
adc.setModifiedBy(themeDisplay.getUserId());
adc.setModifiedDate(new Date());
AuditDocumentCollectionLocalServiceUtil.updateAuditDocumentCollection(adc);
}
}
PurchaseRegister purchaseRegister=PurchaseRegisterLocalServiceUtil.fetchPurchaseRegister(purchaseRegisterId);
if(!fileIds.isEmpty()){
purchaseRegister.setDocumentIds(fileIds);
}
PurchaseRegisterLocalServiceUtil.updatePurchaseRegister(purchaseRegister);
JSONObject jsonobject = JSONFactoryUtil.createJSONObject();
long auditDetailesId=ParamUtil.getLong(req, "auditDetailesId");
jsonobject.put("selectedTransactionList",CommonUtil.getAuditProcessInfo( auditDetailesId));
PrintWriter out = res.getWriter();
out.write(jsonobject.toString());
out.flush();
out.close();
}
I suppose the data present in form data is being sent to resource method. it is not taking documentCollectionIds and its empty. The error I get is this,
[http-bio-3030-exec-10][PurchaseRegisterPortlet:644] i am in savePurchaseRegisterEntry!!!
05:58:34,050 INFO [http-bio-3030-exec-10][PurchaseRegisterPortlet:648] -----
05:58:34,050 INFO [http-bio-3030-exec-10][PurchaseRegisterPortlet:649] ----- 0
Folder is already Exist
fileEntryList.toArray().toString() :: 34529
05:58:34,255 INFO [http-bio-3030-exec-10][PurchaseRegisterPortlet:657]fileIds : 34529
Exception in thread "liferay/document_library_image_processor-3"05:58:34,256 ERROR [DispatcherPortlet:559] Could not complete request
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:453)
at java.lang.Long.parseLong(Long.java:483)
at com.auditcompliance.purchaseregister.controller.PurchaseRegisterPortlet.savePurchaseRegisterEntry(PurchaseRegisterPortlet.java:660)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
check your file first the typscript file of your component if you give
it exactly the type Number, then your service
Or if it's through the html file of your component that you give it
the right type in parameter
For me it was in the html file that I gave him another type
different from number

AJAX post request to third party REST api

. I am using laravel backend as API and angular as frontend which calls laravel api. Laravel api access the teamwork api through basic authentication using curl.
Now I am working with teamwork Api. Trying to create a comment using the API.
API documentation describes following.
{
"comment": {
"body": "Reply to earlier comment",
"notify": "",
"isprivate": false,
"pendingFileAttachments": "",
"content-type": "TEXT"
}
}
//ref : http://developer.teamwork.com/comments#creating_a_commen
in my ajax call i have used following
var data = $.param({
'body' : commentBodyValue, //variable
'notify': "",
'isPrivate':false,
"pendingFileAttachments": "",
"content-type": "TEXT"
});
My post does not give error, but it also do not create a new comment too. What am I missing? I think i failed to arrange data according to the format allowed in api. Can you kindly help me to fix it?
Edit:
Angular Controller:
//add new comment
$scope.addComment = function(taskId,commentAuthorId)
{
var commentBod = document.getElementById("commentBody");
var commentBodyValue = commentBod.value;
console.log("new comment : "+ taskId + commentBodyValue);
//submit the post request
//$http.post();
//using jquery function param to serialize
var data = $.param({
'body' : commentBodyValue,
'notify': "",
'isPrivate':false,
"pendingFileAttachments": "",
"content-type": "TEXT"
});
var config = {
headers : {
// 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;',
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
}
var url = "http://abounde.com/portal/api/post_comment/"+taskId;
$http.post(url,data,config)
.then(
function(response){
//success
console.log("response : Submitted :D " + response);
},
function(response){
//failure
console.log(response);
}
);
}
Edit 2
Laravel route and controller
Routes:
//post comment to resource
Route::post('post_comment/{task_id}',[
'uses'=>'PortalController#postComment',
'as'=>'portal.client.postComment'
]);
Controller:
//post comment at task
public function postComment(Request $request, $task_id)
{
$secretApiKey = $request->session()->get('secretApiKey');
if(!empty($secretApiKey)){
//post the msg
$this->callTeamworkApi($secretApiKey,"tasks/".$task_id."/comments.json");
}else{
return response()->json(['response'=>"Not Logged In"]);
}
}
//authentication method
public function callTeamworkApi($secretApiKey, $apiCallString)
{
//cURL
$password = "xxx";
$channel = curl_init();
//options
curl_setopt($channel, CURLOPT_URL, "http://projects.abounde.com/".$apiCallString); // projects.json?status=LATE gets all late projects
curl_setopt($channel, CURLOPT_HTTPHEADER,
array(
"Authorization: Basic " . base64_encode($secretApiKey . ":" . $password)
));
$msg = curl_exec($channel);
curl_close($channel);
return response()->json(['res'=>$msg]);
}
EDIT 3: Contacted with teamwork Api support.
After their advice i came up with following code
//post comment at task
public function postComment(Request $request, $task_id)
{
$secretApiKey = $request->session()->get('secretApiKey');
if(!empty($secretApiKey)){
//post the msg
$comment=array();
// $comment['body']=$request->input('body');
// $comment['notify']=$request->input('notify');
// $comment['isprivate']=$request->input('isprivate');
// $comment['pendingFileAttachments']=$request->input('pendingFileAttachments');
// $comment['content-type']=$request->input('content-type');
$comment['comment']['body']="test";
$comment['comment']['notify']="";
$comment['comment']['isprivate']=false;
$comment['comment']['pendingFileAttachments']="";
$comment['comment']['content-type']="text";
$this->callTeamworkPostApi($secretApiKey,"tasks/".$task_id."/comments.json",json_encode($comment));
//var_dump($comment);
//return response()->json(['response'=>"Trying"]);
}else{
return response()->json(['response'=>"Not Logged In"]);
}
}
}
///
public function callTeamworkPostApi($secretApiKey, $apiCallString,$comment)
{
//cURL
$password = "xxx";
$channel = curl_init();
//options
curl_setopt($channel, CURLOPT_URL, "http://projects.abounde.com/".$apiCallString); // projects.json?status=LATE gets all late projects
curl_setopt($channel, CURLOPT_HTTPHEADER,
array(
"Authorization: Basic " . base64_encode($secretApiKey . ":" . $password)
));
curl_setopt($channel, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($channel, CURLOPT_POSTFIELDS, $comment);
$msg = curl_exec($channel);
curl_close($channel);
var_dump($msg);
//return response()->json(['res'=>$msg]);
}
var_dump($comment) gives
string(107) "{"comment":{"body":"test","notify":"","isprivate":false,"pendingFileAttachments":"","content-type":"text"}}"
However the api still do not save the comment. It has to be a curl post method issue. Right?
Edit 4:
I have done a var_dump(curl_getinfo($channel));
this gave me following response.
array(26) { ["url"]=> string(55) "http://projects.abounde.com/tasks/8199861/comments.json" ["content_type"]=> NULL ["http_code"]=> int(500) ["header_size"]=> int(229) ["request_size"]=> int(311) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0.5) ["namelookup_time"]=> float(0) ["connect_time"]=> float(0.109) ["pretransfer_time"]=> float(0.109) ["size_upload"]=> float(107) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(214) ["download_content_length"]=> float(0) ["upload_content_length"]=> float(107) ["starttransfer_time"]=> float(0.5) ["redirect_time"]=> float(0) ["redirect_url"]=> string(0) "" ["primary_ip"]=> string(13) "23.23.184.208" ["certinfo"]=> array(0) { } ["primary_port"]=> int(80) ["local_ip"]=> string(11) "192.168.0.6" ["local_port"]=> int(31657) }
The response code 500 may points to the fact that teamwork has some kind of internal issues.
The issue i found out is that you are not passing your comment data which you received from the frontend to the curl the request in callTeamworkApi().Try below code and see if you get your desired output or not
//post comment at task
public function postComment(Request $request, $task_id)
{
$secretApiKey = $request->session()->get('secretApiKey');
if(!empty($secretApiKey)){
//post the msg
$comment=array();
$comment['body']=$request->input('body');
$comment['notify']=$request->input('notify');
$comment['isPrivate']=$request->input('isprivate');
$comment['pendingFileAttachments']=$request->input('pendingFileAttachments');
$comment['content-type']=$request->input('content-type');
$this->callTeamworkApi($secretApiKey,"tasks/".$task_id."/comments.json",json_encode($comment));
}else{
return response()->json(['response'=>"Not Logged In"]);
}
}
//authentication method
public function callTeamworkApi($secretApiKey, $apiCallString,$comment)
{
//cURL
$password = "xxx";
$channel = curl_init();
//options
curl_setopt($channel, CURLOPT_URL, "http://projects.abounde.com/".$apiCallString); // projects.json?status=LATE gets all late projects
curl_setopt($channel, CURLOPT_HTTPHEADER,
array(
"Authorization: Basic " . base64_encode($secretApiKey . ":" . $password)
));
curl_setopt($channel, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($channel, CURLOPT_POSTFIELDS, $comment);
$msg = curl_exec($channel);
curl_close($channel);
return response()->json(['res'=>$msg]);
}
Solution Found:
While calling to the teamwork API, it is essential that the content-type is set to Json. and the information should be in header file. header variable should be a multi array which takes authentication and content type information.
$headers = array();
$headers[] = "Authorization: Basic " . base64_encode($secretApiKey . ":" . $password);
$headers[] = "Content-Type: application/json";
//pass it as variable
curl_setopt($channel, CURLOPT_HTTPHEADER, $headers);
And then it works like charm.
returns following after creation of the comment.
{"commentId":"4294639","STATUS":"OK"}

angular/node.js POST for payumoney integration

I'm using angular/node.js stack for payumoney integration.
On the angular side, an order is placed using $http.post to a route endpoint at the server side (node.js) as follows:
$http.post('/placeOrder',order).success(function(data, status, headers, config){
//handle responses on client side
console.log("Successfully POSTED to payment gateway");
window.location = "https://test.payu.in/_payment";
}).error(function(data, status, headers, config) {
console.log("Error in posting");
});
The actual heavy lifting is done on the node.js (server side):
router.post('/placeOrder', function(req, res, next){
hash_data = MERCHANT_KEY+'|'+txnid+'|'+amount+'|'+productinfo+'|'+firstname+'|'+email+'|'+udf1+'|'+udf2+'|'+udf3+'|'+udf4+'|'+udf5+'||||||'+SALT;
var data = querystring.stringify({
'key': MERCHANT_KEY,
'txnid': txnid,
'amount': amount,
'productinfo': productinfo,
'firstname': firstname,
'email': email,
'phone': phone,
'surl': SUCCESS_URL,
'furl': FAILURE_URL,
'curl': FAILURE_URL,
'hash': hash,
'service_provider': SERVICE_PROVIDER
//'salt': SALT
});
//POST options
var POST_OPTIONS = {
hostname: PAYU_BASE_URL,
port: 443,
path: '/_payment',
method: 'POST',
//json: true,
agent: false,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
//'Content-Length': Buffer.byteLength(data)
'Content-Length': data.length
}
};
var resp_status = "";
var req = https.request(POST_OPTIONS, function(response) {
console.log('STATUS: ' + response.statusCode);
console.log('HEADERS: ' + JSON.stringify(response.headers));
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log("body: " + chunk);
resp_status = 200;
res.json(chunk);
});
response.on('error', function (err) {
console.log("Got error: " + err.message);
resp_status = 500;
return res.send(err);
});
});
req.end(data);
However, this doesn't seem to work as the POST doesnt seem to work using this approach. While debugging on the browser through the network tab, I always see:
Request URL:https://test.payu.in/_payment
Request Method:GET
Status Code:200 OK
Also, the test payment page (https://test.payu.in/_payment) shows:
"Error Reason
One or more mandatory parameters are missing in the transaction request."
Any help would be appreciated!!
How did I implement this..
Use Jquery and create a Form
Use sha512 to create hashcode. (bower install js-sha512)
var hashString = this.merchantKey+'|'+ options.uid +'|'+ options.totalPrice + '|'+'options.uid + '|' +
options.recipient_name + '|'+ options.email +'|||||||||||'+ this.merchantSalt ;
var hash = sha512(hashString);
var key1 = $('<input></input>').attr('type', 'hidden').attr('name', "key").val("merchantKey");
var key2 = $('<input></input>').attr('type', 'hidden').attr('name', "txnid").val(options.uid);
var key3 = $('<input></input>').attr('type', 'hidden').attr('name', "amount").val(options.totalPrice);
var key4 = $('<input></input>').attr('type', 'hidden').attr('name', "productinfo").val(options.uid);
var key5 = $('<input></input>').attr('type', 'hidden').attr('name', "firstname").val(options.recipient_name);
var key6 = $('<input></input>').attr('type', 'hidden').attr('name', "email").val(options.email);
var key7 = $('<input></input>').attr('type', 'hidden').attr('name', "phone").val(options.phone);
var key8 = $('<input></input>').attr('type', 'hidden').attr('name', "surl").val("http://192.168.43.121/payment/success");
var key9 = $('<input></input>').attr('type', 'hidden').attr('name', "furl").val("http://192.168.43.121/payment/error");
var key10 = $('<input></input>').attr('type', 'hidden').attr('name', "hash").val(hash);
var key11 = $('<input></input>').attr('type', 'hidden').attr('name', "service_provider").val("payu_paisa");
var form = $('<form/></form>');
form.attr("id", "payuform");
form.attr("action", this.payumoneyLink );
form.attr("method", "POST");
form.attr("style", "display:none;");
form.append(key1, key2, key3, key4, key5, key6, key7, key8, key9,key10, key11);
$("body").append(form);
// submit form
form.submit();
This is my first answer on StacksOverflow. Hope it helps!
As per the browser network tab which you have mentioned,
Request URL:https://test.payu.in/_payment Request Method:GET Status Code:200 OK
This means PayU is getting called with GET request instead of POST request somehow. PayU accepts data as a POST request only.
Also, the test payment page (https://test.payu.in/_payment) shows: "Error Reason One or more mandatory parameters are missing in the transaction request."
This is due to GET request. I have faced similar situation in my JSF based application wherein I was sending all parameters correctly but as a GET request. Later on when I switched to POST, the error got resolved automatically.
For information about sending POST request from angular, check below link.
https://www.devglan.com/angular/payumoney-integration-angular
NOTE: If input type is hidden, angularjs has some issue connecting model and view. So please take a note of that. txnid and hash I am getting from AJAX get call so I had to bind it in seperate variable from scope.
Angular code is staright forward, just populate the variables.
One more thing to remember as of today, if your account is not active then you need to use test salt/key provided by their customer support:
MID : 4934580
Key : rjQUPktU
Salt : e5iIg1jwi8
Authorization : y8tNAC1Ar0Sd8xAHGjZ817UGto5jt37zLJSX/NHK3ok=
Test Card : 5123456789012346
Expiry : 05/20
CVV : 123

How to pass data into a Listener when using Guzzle?

I am using Guzzle 3.8.1 with CakePHP 2.5.6 and I am making a call to the Box API. I would like to be able to refresh my access token when it expires. The issue I am encountering is that I may be using any one of N possible access tokens and I want to be able to use the $account_id that is in scope for the function I am defining this in. I don't see how I can pass it into the Listener defined function, since I am not calling that myself. I do see that the expired access_token exists in the $event variable, but I would have to locate it and then parse it from the header that I am passing to Box. I think I could add a custom header containing the account_id, but that seems like a bad way to approach the problem. Can I somehow specify a variable in the request that wouldn't be added as part of the call to Box? Any suggestions are much appreciated, thanks!
`
public function get_folder_list ($account_id = null, $parent_folder_id = null) {
// Get account
$this->Account = ClassRegistry::init('Account');
$account = $this->Account->findById($account_id);
$this->Account->log($account);
// If account doesn't exist, throw error
if(empty($account)) {
throw new NotFoundException(__('Account id not found: ' . $account_id));
}
// Call Box for folder list
$box_url = "https://api.box.com/2.0/folders/";
if (empty($parent_folder_id)) {
$box_url .= "0";
} else {
$box_url .= $parent_folder_id;
}
$this->Account->log($box_url);
$bearer = 'Bearer ' . $account['Account']['access_token'];
$client = new Guzzle\Http\Client();
$client->getEventDispatcher()->addListener('request.error', function(Event $event) {
if ($event['response']->getStatusCode() == 401) {
$Account = new Account;
$Account->log($event);
$Box = new BoxLib;
$account = $Box->refresh_token($account_id);
$bearer = 'Bearer ' . $account['Account']['access_token'];
$request = $client->get($box_url, array('Authorization' => $bearer));
$event['response'] = $newResponse;
$event->stopPropagation();
}
});
$request = $client->get($box_url, array('Authorization' => $bearer));
try {
$response = $request->send();
} catch (Guzzle\Http\Exception\BadResponseException $e) {
// HTTP codes 4xx and 5xx throw BadResponseException
$this->Account->log('Service exception!');
$this->Account->log('Uh oh! ' . $e->getMessage());
$this->Account->log('HTTP request URL: ' . $e->getRequest()->getUrl() . "\n");
$this->Account->log('HTTP request: ' . $e->getRequest() . "\n");
$this->Account->log('HTTP response status: ' . $e->getResponse()->getStatusCode() . "\n");
$this->Account->log('HTTP response: ' . $e->getResponse() . "\n");
}
$this->Account->log($response->json());
$json = $response->json();
$folder_list = array();
foreach ($json['item_collection']['entries'] as $folder) {
$folder_list[$folder['id']] = $folder['name'];
}
//$this->Account->log($folder_list);
return $folder_list;
}
`

Resources