Support » Plugin: License Manager for WooCommerce » API wrap around

  • Resolved kimherrero

    (@kimherrero)


    Hi to all, considering that my software has an InstallId that needs to get paired with the LicenseId and that I want to avoid to hard code the consumer keys, I made the following work around php that calls the rest API and does the extra job.

    As I am not a php expert, the question is, is it ok to make a call inside a call, or it could be a potentially server overloading problem?

    By the way, congrats for this excellent solution.

    P.D. I echo the results because I treat them as an INI file.

    Kim

    —————

    <?php

    if(isset($_GET[“id”])) {

    $iguid = $_GET[“id”];
    $ch = curl_init();

    curl_setopt_array($ch, array(
    CURLOPT_URL => “{host}/wp-json/lmfwc/v2/licenses/”.$iguid.”?consumer_key={consumer_key}&consumer_secret={consumer_secret}”,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => “”,
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => false,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => “GET”,
    ));

    $response = curl_exec($ch);
    $err = curl_error($ch);

    curl_close($ch);

    if ($err) {
    echo “Error #:” . $err;
    } else {

    $json = json_decode($response);
    if ( $json->success == true) {

    echo “Key=” . $json->data->licenseKey . “\n”;
    echo “Order=” . $json->data->orderId . “\n”;
    echo “Version=” . $json->data->productId . “\n”;
    echo “Status=” . $json->data->status . “\n”;
    echo “Expires=” . $json->data->expiresAt . “\n”;

    }

    // extra job here

    }

    }

    ?>

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Drazen Bebic

    (@drazenbebic)

    Hello @kimherrero,

    thank you for your message and for using my plugin.

    I have a question first: Is $iguid your license key? If so, all is good.

    It is always best to cut down on remote API calls as much as possible. You said that you don’t want to hardcode the consumer_key and consumer_secret, and that is reasonable. However, your “order” is wrong in your example. You would first need to do your extra job to obtain the consumer_key and consumer_secret, and then replace the placeholders on this line:

    CURLOPT_URL => “{host}/wp-json/lmfwc/v2/licenses/”.$iguid.”?consumer_key={consumer_key}&consumer_secret={consumer_secret}”

    I’m referring to {consumer_key} and {consumer_secret}. And it shouldn’t cause an overload by itself. However you will have to make 2 API calls to get the job done, which increases the risk of something going wrong. The request will also take longer to complete. But if you absolutely cannot have your keys hardcoded, then I don’t see another way.

    Let me know if you need further assistance

    Plugin Author Drazen Bebic

    (@drazenbebic)

    Hello @kimherrero,

    have you been able to work this one out? Do you need any more assistance?

    Tried the codesnippet provided above, but getting syntax problems all over the place.

    Are there any specific php modules that needs installed for it?

    I blank copied the code – adjusted the URL to match a postman-working URL (to make sure it worked) but im throwing php parse problems from line 26 and further.

    If I entirely remove if ($err) { and the rest, its 200,- working fine.

    Issues begins right at that point, and when I fix one line, I break another.
    Any chance someone can look it over?

    Plugin Author Drazen Bebic

    (@drazenbebic)

    Hello @pipermp3,

    Try this:

    <?php
    
    if (isset($_GET["id"])) {
    
    	$iguid = $_GET["id"];
    	$ch    = curl_init();
    
    	/**
    	 * Replace {host} with your website URL
    	 * Replace {consumer_key} with your consumer key generated by the plugin (starts with ck_)
    	 * Replace {consumer_secret} with your consumer secret generated by the plugin (starts with cs_)
    	 */
    	curl_setopt_array($ch, array(
    	CURLOPT_URL => "{host}/wp-json/lmfwc/v2/licenses/". $iguid ."?consumer_key={consumer_key}&consumer_secret={consumer_secret}",
    	CURLOPT_RETURNTRANSFER => true,
    	CURLOPT_ENCODING => "",
    	CURLOPT_MAXREDIRS => 10,
    	CURLOPT_TIMEOUT => 0,
    	CURLOPT_FOLLOWLOCATION => false,
    	CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    	CURLOPT_CUSTOMREQUEST => "GET",
    	));
    
    	$response = curl_exec($ch);
    	$err = curl_error($ch);
    
    	curl_close($ch);
    
    	if ($err) {
    		echo "Error #:" . $err;
    	} else {
    		$json = json_decode($response);
    
    		if ( $json->success == true) {
    			echo "Key=" . $json->data->licenseKey . "\n";
    			echo "Order=" . $json->data->orderId . "\n";
    			echo "Version=" . $json->data->productId . "\n";
    			echo "Status=" . $json->data->status . "\n";
    			echo "Expires=" . $json->data->expiresAt . "\n";
    		}
    
    		// extra job here
    	}
    
    }
    
    ?>

    Hi @drazenbebic

    Whoah, that was hella quick response.

    Yep, in this setup that snippet works 100%.
    Done a few translations for you for Norwegian whilst I was waiting for ya.

    Great plugin by the way, works fab. Newbie to API in general so this plugin has proven quite good in getting to grips of it.

    For anyone else that eventually lands on this code snippet, it works if you have a “form” that submits TO this code.

    Ie, the code above would be your api.php
    and this could be your “form”;

    form.php
    <html>
    <body>

    <form action=”http://hostaddress/api.php&#8221; method=”get”; <—- api.php is the codesnippet above
    License Key: </br>
    <input type=”text” name=”id”> </br>
    <input type=”submit”>
    </form>

    </body>
    </html>

    Horribly basic form, no validation.
    But it works, and is a great starter-point to develop on further what you may aim to do with the API/Plugin.

    Thanks again @drazenbebic !

    • This reply was modified 9 months, 2 weeks ago by pipermp3.
    Plugin Author Drazen Bebic

    (@drazenbebic)

    @pipermp3

    No problem, also thanks a lot for the translations! 🙂

    If you hit another snag or need help with anything else, do not hesitate to ask.

    PS: I would greatly appreciate if you could take some time out of your day to write a plugin review here on WordPress.org. These reviews really help the plugin grow and mean a lot to me personally. Here’s the link:

    https://wordpress.org/support/plugin/license-manager-for-woocommerce/reviews/#new-post

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘API wrap around’ is closed to new replies.