0

I'm trying to use the backpack-connect API to push badges to a user's backpack. The example in the open badges documentation uses node.js.

My question is: Do I have to use node.js or is it possible to write something equivalent to this in php? I'm not asking for someone to write it, just to say:

  • If it is possible.
  • If i'm missing the point, or have a fundamental misunderstanding.

I have some experience coding in php and js, and have dipped into other apis with success (eg. linkedin), I'm finding this one difficult because of a lack of practical examples or helpful info (there's plenty of conceptual fluff talk, but little in the way of implementation examples).

The node.js example from the moz docs is as follows:

var assertionData = querystring.stringify({
        badge: 'http://yoursite.com/badge-assertion.json'
});

var requestOptions = {
    host : 'backpack.openbadges.org',//adjust for your api root
    path : '/api/issue', 
    method : 'POST', 
    headers: { 'Authorization': 'Bearer ' + b64enc('your-access-token'),
        'Content-Type': 'application/json',
        'Content-Length': Buffer.byteLength(assertionData)
    }
};

var postRequest = http.request(requestOptions, function(pushResponse) {
var response = [];
pushResponse.setEncoding('utf8');

//store data
pushResponse.on('data', function (responseData) {
    response.push(responseData);
});

pushResponse.on('end', function(){
     var pushData=JSON.parse(response.join('')); 
     //...
 });
});

postRequest.on('error', function(e) {
 console.error(e);
});

// post the data
postRequest.write(assertionData);
postRequest.end();

Can I forget about node.js and just do this via PHP?

Thanks for any help!

user3606161
  • 11
  • 1
  • 3
  • How is it called from the browser? If it is just an AJAX request, you can do it with PHP. Or if this is just an API over HTTP, then again yes, PHP is fine. – halfer Jul 25 '14 at 18:49
  • Thanks halfer, it's called from the browser via js using moz's methods in their issuer script, and I supply a url for my callback. The response I get is in the form of a GET. I guess this is just over http so it sounds like I should be able to achieve this with php. Thanks again for your reply. – user3606161 Jul 25 '14 at 19:35

0 Answers0