如何在 PHP 中验证 Envato 购买代码

问题描述 投票:0回答:4

我正在使用 Envato API 来验证购买代码我使用了来自 GitHub 的插件示例代码https://github.com/codehaiku/Envato-Purchase-Code-Verifier在这里我添加了令牌和 AUTHOR_SALES_ENDPOINT_URI = 'https:/ /api.envato.com/v2/market/author/sale?code=' 当我尝试时也遇到错误无效的购买代码何时为有效的购买代码。我在这里错过了什么吗?谁能建议我。还有其他替代方法吗?

 <!DOCTYPE html>
<html>
<head>
    <title>Sample Envato Purchase Code Verifier</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div class="wrap">
        <!-- // Include the EnvatoPurchaseCodeVerifier.php file that holds the verifier class.--> 
        <?php require_once 'EnvatoPurchaseCodeVerifier.php'; ?>
        <!-- // Create your own access token at (https://build.envato.com/create-token)-->
        <?php $access_token = 'my token here'; // Change 'your_token' value with your own token ?>
        <!-- // Create new instance of EnvatoPurchaseCodeVerifier. -->
        <!-- // Pass your own access token -->
        <?php $purchase = new EnvatoPurchaseCodeVerifier($access_token); ?>

        <?php $buyer_purchase_code = filter_input(INPUT_POST, 'purchase_code', FILTER_DEFAULT); ?>

        <!-- check if user submits the form -->
        <?php if ( ! empty( $buyer_purchase_code ) ) { ?>

            <?php $verified = $purchase->verified($buyer_purchase_code); ?>
            <!-- User purchase code is good!-->
            <?php if ( $verified ) { ?>
                <?php
                     $item_id = $verified->item->id; 
                     $item_name = $verified->item->name; 
                     $buyer = $verified->buyer; 
                     $license = $verified->license; 
                     $amount = $verified->amount; 
                     $sold_at = $verified->sold_at; 
                     $supported_until = $verified->supported_until;
                 ?>
                 <h1>Valid Purchase Code</h1>
                 <table>
                    <tr><td>Item ID:</td><td><?php echo $item_id; ?></td></tr>
                    <tr><td>Item Name:</td><td><?php echo $item_name; ?></td></tr>
                    <tr><td>Buyer:</td><td><?php echo $buyer; ?></td></tr>
                    <tr><td>License:</td><td><?php echo $license; ?></td></tr>
                    <tr><td>Amount:</td><td><?php echo $amount; ?></td></tr>
                    <tr><td>Sold At:</td><td><?php echo $sold_at; ?></td></tr>
                    <tr><td>Supported Until:</td><td><?php echo $supported_until; ?></td></tr>
                </table>
            <?php } else { ?>
                <!-- Invalid purchase code -->
                <h1>Invalid Purchase Code</h1>
            <?php } ?>

        <?php } else  { ?>
            <form action="index.php" method="post">
                <label>Purchase Code: </label>
                <input type="text" name="purchase_code" placeholder="Type or paste the buyer's purchase code here"><br>
                <input type="submit">
            </form>
        <?php } ?>


    </div>
</body>
</html>


require_once __DIR__ . '/vendor/autoload.php';

use Curl\Curl;

final class EnvatoPurchaseCodeVerifier
{
    protected $accessToken = '';

    const AUTHOR_SALES_ENDPOINT_URI = 'https://api.envato.com/v2/market/author/sale?code=';

    /**
     * Class constructor, pass the access token.
     * @param mixed The access token <https://build.envato.com/create-token/>
     */
    public function __construct($accessToken)
    {
        $this->accessToken = $accessToken;
    }

    /**
     * Supply the purchase code of the buyer as argument.
     * @param  mixed $purchaseCode The purchase code of he buyer.
     * @return mixed (Boolean) False on fail, the api (Object) $curl->response info on success.
     */
    public function verified($purchaseCode)
    {
        if (empty($purchaseCode)) {
            return false;
        }

        $curl = new Curl();

        $curl->setHeader('Authorization', 'Bearer ' . $this->accessToken);

        $curl->get(self::AUTHOR_SALES_ENDPOINT_URI, array(
            'code' => $purchaseCode
        ));

        $curl->close();

        if ($curl->error) {
            return false;
        } else {
            return $curl->response;
        }
    }
}

php json wordpress api
4个回答
1
投票

请尝试以下操作,已测试且有效

<?php 
$code = "86781236-23d0-4b3c-7dfa-c1c147e0dece";

// If you took $code from user input it's a good idea to trim it:

$code = trim($code);

// Make sure the code is valid before sending it to Envato:

if (!preg_match("/^(\w{8})-((\w{4})-){3}(\w{12})$/", $code))
    throw new Exception("Invalid code");

// Query using CURL:

$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => "https://api.envato.com/v3/market/author/sale?code={$code}",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 20,

    CURLOPT_HTTPHEADER => array(
        "Authorization: Bearer KEY_HERE",
        "User-Agent: Enter a description of your app here for the API team"
    )
));

0
投票

自过去 4 个月以来,我也面临这个问题,我对我的网络排名感到非常抱歉,我也附上了屏幕截图,请检查我的网站:https://s3rcn.org/

在此输入图片描述


-1
投票

检查您是否已更新 Composer 并确保您拥有文件 EnvatoPurchaseCodeVerifier.php。因为根据我的理解,您的代码片段尝试在同一文件中添加 EnvatoPurchaseCodeVerifier 类,并在该类丢失之前添加一些 php 开始标记。正确的方法是将其放入不同的文件中,并检查作曲家自动加载是否正常工作。


-1
投票

试试这个:

function verify_purchase_code($code) {
    $code = urlencode($code);
    $url = "https://envato.eduardofiorini.com/index.php?item=35215272&code=" . $code . "&domain=" . $_SERVER['HTTP_HOST'];

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Content-type: application/json'));

    $ret = curl_exec($ch);
    curl_close($ch);

    if (!$ret) {
        $ret = file_get_contents($url);
    }

    $data = json_decode($ret??"{}",true);

    if($data["error"]){
        echo json_encode(array("success" => false, "message" => $data["msg"]));
        exit();
    }else{
        return true;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.