How to use remote api?
-
Hi Gabriel,
How can i use remote api? For example, if i want to display mycred points in my own webpage(html page) not wordpress, how can i integrate mycred to my webpage and display points as well as update points from my page?
-
Hi.
You would need to have the Remote API enabled and setup on your WordPress site and use PHP to create callbacks that you use on your html page. You could for example create a form that when submitted calls to your WordPress site and get a users balance for example. But you can not do this with plain HTML pages because you must encode your callbacks to include your secret key. If you just create a plain HTML page with a form, you would not be able to do this since the Remote API requires an encrypted token with each request.
Hi Gabriel,
Thanks for your reply, I have enabled remote api in wordpress, Could you please give an example for creating callbacks, encode callbacks to include secret key and display the users balance in other site as well as updating points from other site.The “catching” of api calls are handled by the remote API assuming you have enabled it on both sites and both sites have the same secret key.
Ok so lets do some examples.
First off, I will be calling the site we make our calls from Site A and the recipient site Site B to avoid confusion. I will also be using WordPress functions in the examples which are documented.Before we begin
A few notable things:
1. Both site A and B has been setup to use the Remote API and their icons are green in the admin area.
2. Both site A and B has the same secret key.
3. We have setup the URL for our calls on site B.Making Calls.
You can make POST, GET or JSON calls using the API, if you have a preference for one of these then by all means go ahead and use it, but for the examples below I will be using the POST method.WordPress has a nifty little function called wp_remote_post which we can use to make remote POST calls. To use this function we need two things. First we need the URL for site B that we setup in the remote API settings. Lets say this is https://siteB.com/api/.
Here is a quick example of using this function:
$remote_url = 'https://siteB.com/api/'; $request = array( 'method' => 'POST', 'body' => array( 'action' => 'GET', 'account' => '[email protected]', 'token' => 'md5 hash', 'host' => 'https://siteA.com' ) ); $raw_response = wp_remote_post( $remote_url, $request );
As you can see, we first set what method we use (POST) and then in the body, insert an array of arguments that are passed on to site B. This array of arguments is the important part as it contains our request.
There are 4 possible actions you can give:
GET – get the users balance
CREDIT – give the user points
DEBIT – remove points from a user
PAY – transfer points between two users.In the above example, we use GET which means it will return the users balance.
Next we have account, which must be set to the email address of the targeted user on site B. Email addresses is the only way we can identify users so if the user does not exist, you will receive a 201 error if debug mode is enabled.
Next we have the token key which is the md5 hash of our request (more on this later).
And finally we have the host key which is the URL of site A.
There are a few other keys you can use depending on the action you want to take.
Now lets start doing some practical examples.
Example 1: Get my balance from site B:// The secret key $secret_key = '123ABC'; // The remote URL $remote_url = 'https://siteB.com/api/'; // Action we want to take $action = 'GET'; // The account in question $account = '[email protected]'; // Site A's URL $host = get_bloginfo( 'url' ); // Host + action + key hashed $token = md5( $host . $action . $secret_key ); // Construct the request $request = array( 'method' => 'POST', 'body' => array( 'action' => $action, 'account' => $account, 'token' => $token, 'host' => $host ) ); // Let WordPress do it's magic $raw_response = wp_remote_post( $remote_url, $request ); // Parse response $response = ''; if ( ! is_wp_error( $raw_response ) && ( $raw_response['response']['code'] == 200 ) ) $response = maybe_unserialize( $raw_response['body'] ); echo 'Your Site B Balance is: ' . $response;
The above code will call site B and ask the balance of a user with the email [email protected]. If the user exists and the md5 hash match, the amount is returned. If you have debug mode enabled for the remote API, then you will see an error message if something goes wrong. It is highly recommended that you have it enabled while developing to help you pin point any issues but disable it once it is up and running.
A few things to remember:
1. You should not make calls on every single page load on site A. If you want to show on every single page your users balance from site B, then I recommend you make the call once a day and then save it on site A until the next call is made. Otherwise, each page load will be delayed until site B replies.
2. Never ever send your secret key in your request! The key should only be used in your hashing, if you include it in the request, then everyone will see it (well at least those who are listing).
3. If you change the secret key on Site B, you must change it to the same on site A.
For more information about the remote API class and error messages, you can consult the myCRED Codex.
There are a lot more to be said about the remote API but I figured I start with this. I will also work on getting the online documentation up and running.
Hi Gabriel,
First Thank you so much for your reply, I was waiting for you to reply, I tried the above method i am getting error saying that [ Fatal error: Call to undefined function wp_remote_post() ], if i include [ include_once ‘../wp-load.php’; ] by putting this file in wordpress its not showing any error but its not displaying points, output is like this when i run this code ( Your Site B Balance is: )
This is the way i used it
<?php
include_once ‘../wp-load.php’;
$remote_url = ‘https://b.localhost/api/’;
$request = array(
‘method’ => ‘POST’,
‘body’ => array(
‘action’ => ‘GET’,
‘account’ => ‘[email protected]’,
‘token’ => ‘md5 hash’,
‘host’ => ‘https://a.localhost/woo’
)
);$raw_response = wp_remote_post( $remote_url, $request );
// The secret key
$secret_key = ‘*********’;// The remote URL
$remote_url = ‘https://b.localhost/api/’;// Action we want to take
$action = ‘GET’;// The account in question
$account = ‘[email protected]’;// Site A’s URL
$host = get_bloginfo( ‘url’ );// Host + action + key hashed
$token = md5( $host . $action . $secret_key );// Construct the request
$request = array(
‘method’ => ‘POST’,
‘body’ => array(
‘action’ => $action,
‘account’ => $account,
‘token’ => $token,
‘host’ => $host
)
);// Let WordPress do it’s magic
$raw_response = wp_remote_post( $remote_url, $request );// Parse response
$response = ”;
if ( ! is_wp_error( $raw_response ) && ( $raw_response[‘response’][‘code’] == 200 ) )
$response = maybe_unserialize( $raw_response[‘body’] );echo ‘Your Site B Balance is: ‘ . $response;
?>Am i using the code in wrong way? Need your help to get it done.
Hey.
Ok so you are trying to do this outside of WordPress which the above code examples is not written for. The code examples was written for use inside WP i.e. via your theme or a custom plugin.
The problem you face is that myCRED requires WP so you must load WordPress and all plugins (including myCRED) in order for this to work. For example, the remote API hooks into the WordPress init hook. So if this hooks is not loaded or if myCRED is not loaded then your calls will not be received.
While I think it is technically possible to load myCRED outside WP, it is not supported unfortunately as I need to stay focused on WordPress environments which most of myCRED functions rely on.
You can check to see if myCRED gets loaded by checking if for example the myCRED_VERSION constant is defined.
If you can not reach the init hook then you might want to include the remote file into your script and try accessing it directly. Note that all myCRED files require the myCRED_VERSION contact to be defined in order to be loaded or the file will exit itself as soon as it is loaded.
what about using a payment gate way? could we request information chsnge point values that way. but instead of it taking a payment it would just transfer information?
@jmodine You could of course build a custom bridge between the two and post messages to each other. The Remote API in myCRED is very basic but allows you to get information about a users balance from a remote source, ask to remove points from the user, add points to the user or transfer points between two users.
I used this to deduct points but fails . would you please check it ?
[insert_php]
$secret_key = ‘secret key ‘;
$remote_url = ‘site B’;
$action = ‘CREDIT’;
$account = ‘[email protected]’;
$host = get_bloginfo( ‘url’ );
$amount = 10;
$ref = ‘reference’;
$point_type = ‘mycred_default’;
$ref_id = 0;
$entry = ‘Payment for test’;
$data = ‘optional extra’;$token = md5( $host . $action . $amount . $secret_key );
$request = array(
‘method’ => ‘POST’,
‘body’ => array(
‘action’ => $action,
‘account’ => $account,
‘amount’ => $amount,
‘ref’ => $ref,
‘ref_id’ => $ref_id,
‘type’ => $point_type,
‘entry’ => $entry,
‘data’ => $data,
‘token’ => $token,
‘host’ => $host
)
);$raw_response = wp_remote_post( $remote_url, $request );
$response = ”;
if ( ! is_wp_error( $raw_response ) && ( $raw_response[‘response’][‘code’] == 200 ) )
$response = maybe_unserialize( $raw_response[‘body’] );echo ‘TEST’ . $response;
[/insert_php]
I solved my previous post problem now I have this problem :
I can ‘DEBIT’ my points but I can not ‘CREDIT’ and ‘PAY’ my points , here is the codes : (AMOUNT CAN DEDUCT BUT NOT CREDIT )
*********************************************
DEBIT : (WORK WELL)
*********************************************
$secret_key = ‘MY SECRET KEY’;
$remote_url = ‘https://www.SITEREMOTE.com/api/’;$action = ‘CREDIT’;
$account = ‘[email protected]’;
$amount = 3;
$ref = ‘reference’;
$point_type = ‘mycred_default’;
$ref_id = 0;
$entry = ‘Payment for test’;
$data = ‘optional extra’;
$host = get_bloginfo( ‘url’ );$token = md5( $host . $action . $amount . $secret_key );
$request = array(
‘method’ => ‘POST’,
‘body’ => array(
‘action’ => $action,
‘account’ => $account,
‘amount’ => $amount,
‘ref’ => $ref,
‘ref_id’ => $ref_id,
‘type’ => $point_type,
‘entry’ => $entry,
‘data’ => $data,
‘token’ => $token,
‘host’ => $host
)
);$response = wp_remote_post( $remote_url, $request );
*********************************************
CREDIT : (THE AMOUNT WILL NOT CREDIT TO THE USER)
*********************************************
$secret_key = ‘MY SECRET KEY’;
$remote_url = ‘https://www.SITEREMOTE.com/api/’;$action = ‘CREDIT’;
$account = ‘[email protected]’;
$amount = 3;
$ref = ‘reference’;
$point_type = ‘mycred_default’;
$ref_id = 0;
$entry = ‘Payment for test’;
$data = ‘optional extra’;
$host = get_bloginfo( ‘url’ );$token = md5( $host . $action . $amount . $secret_key );
$request = array(
‘method’ => ‘POST’,
‘body’ => array(
‘action’ => $action,
‘account’ => $account,
‘amount’ => $amount,
‘ref’ => $ref,
‘ref_id’ => $ref_id,
‘type’ => $point_type,
‘entry’ => $entry,
‘data’ => $data,
‘token’ => $token,
‘host’ => $host
)
);$response = wp_remote_post( $remote_url, $request );
*********************************************
PAY :
here amount will deduct from the ‘USER’ but not credit to the ‘USER2’
*********************************************
$secret_key = ‘MY SECRET KEY’;
$remote_url = ‘https://www.SITEREMOTE.com/api/’;$action = ‘PAY’;
$account = ‘[email protected]’;
$amount = 7;
$to = ‘[email protected]’;
$ref = ‘reference’;
$ref_id = 0;
$entry = ‘Payment for order #1234’;
$data = ‘optional extra’;
$point_type = ‘mycred_default’;
$host = get_bloginfo( ‘url’ );$token = md5( $host . $action . $amount . $from . $to . $secret_key );
$request = array(
‘method’ => ‘POST’,
‘body’ => array(
‘action’ => $action,
‘account’ => $from,
‘amount’ => $amount,
‘to’ => $to,
‘ref’ => $ref,
‘ref_id’ => $ref_id,
‘type’ => $point_type,
‘entry’ => $entry,
‘data’ => $data,
‘token’ => $token,
‘host’ => $host
)
);$response = wp_remote_post( $remote_url, $request );
Dear Gabriel I am waiting for your answer ,
Regards
- The topic ‘How to use remote api?’ is closed to new replies.