服务器设置

版本 发行版 2024.3703
关键词 广告、广告宣传、Peanut Labs、服务器
另请参阅 peanutlabs.*

概述

Peanut Labs 要求你集成他们的服务器端回调,并将回调数据存储在服务器上的数据库中。然后,你可以从应用中检索数据,以验证用户是否获得了奖励。

以下示例只是开始使用时的指南。有很多方法可以实现此功能,当然我们无法列出所有方法。有关更多信息,请参阅 Peanut Labs 服务器回调文档

示例

PHP 回调
<?php

$db = new mysqli('localhost', 'YOURDATABASEUSER', 'YOURDATABASEUSERPASSWORD', 'YOURDATABASE');
if (!$db) {
    // Can't connect to the database
    echo( 0 );
}

define("TABLE_NAME", "peanutlabs"); // Database table name (change for your needs)

define("APPLICATION_KEY", 'Your_Application_Key'); // Your Peanut Labs application key
define("TRANSACTION_KEY", 'Your_Transaction_Key'); // Your Peanut Labs transaction key
define("PUBLISHER_ID", "Your_App_ID");  // Your Peanut Labs app ID

$transactionHash = $_GET['txnHash'];
$oidHash = $_GET['oidHash'];

$cmd = $_GET['cmd'];
$endUserId = $_GET['endUserId']; // 'sample_user_id';
$status = $_GET['status']; // Transaction status
$surveyId = $_GET['offerInvitationId']; // Survey ID
$currencyAmount = $_GET['currencyAmt'];
$transactionId = $_GET['transactionId'];
$publisherCut = $_GET['amt'];

$myTransactionHash = md5($transactionId . TRANSACTION_KEY);
$myOidHash = md5($surveyId . APPLICATION_KEY);

if ($myTransactionHash == $transactionHash) {
    // Transaction hash is valid
} else {
    // Invalid transaction hash
    //echo( "invalid transation hash - " . $myTransactionHash . " " . $transactionHash );
    echo( 0 );
    exit;
}

if ($myOidHash == $oidHash) {
    // OID hash is valid
} else {
    // Invalid OID hash
    //echo( "invalid OID hash" );
    echo( 0 );
    exit;
}

// Update DB with $publisherCut
// Escape the strings to prevent SQL injection attacks
$kEndUserId = $db->real_escape_string($endUserId);
$kStatus = $db->real_escape_string($status);
$kSurveyId = $db->real_escape_string($surveyId);
$kCurrencyAmount = (int)$db->real_escape_string($currencyAmount);
$kTransactionId = $db->real_escape_string($transactionId);
$kPublisherCut = (float)$db->real_escape_string($publisherCut);

// Set the values to insert into the database
$now = time();
$query = $db->prepare("INSERT INTO " . TABLE_NAME . " (userId, status, surveyId, currencyAmount, transactionId, publisherCut, timestamp) VALUES ( ?, ?, ?, ?, ?, ?, ?);" );
$query->bind_param("sssisd", $kEndUserId, $kStatus, $kSurveyId, $kCurrencyAmount, $kTransactionId, $kPublisherCut, $now);

// Execute the query
$result = $query->execute();
if(!$result) {
    //echo( "DB query failed" );
    echo(0);
    exit;
}

echo(1);
?>
获取 PHP 结果
<?php

$db = new mysqli('localhost', 'YOURDATABASEUSER', 'YOURDATABASEUSERPASSWORD', 'YOURDATABASE');
if (!$db) {
    // Can't connect to the DB
    trigger_error( "Cannot connect to the DB" );
}

define("TABLE_NAME", "peanutlabs"); // Database table name (change for your needs)

define("APPLICATION_KEY", 'Your_Application_Key'); // Your Peanut Labs application key
define("TRANSACTION_KEY", 'Your_Transaction_Key'); // Your Peanut Labs transaction key
define("PUBLISHER_ID", "Your_App_ID");  // Your Peanut Labs app ID

// Get the user ID passed to the GET call, for example "http://yoursite.com/getUser.php?userId=User_Id_Here"
$kEndUserId = $db->real_escape_string( $_GET['userId'] );

// Create an array to hold the rows
$rows = array();

// Get the SQL column
$queryString = "SELECT * FROM peanutlabs WHERE userId = '" . $kEndUserId . "' ORDER BY timestamp DESC LIMIT 1";

$result = $db->query( $queryString );
if ($result) {
    // Fetch associative array
    $i = 1;
    while ($row = $result->fetch_assoc()) {
        $rows[$i++] = $row;
    }
    $result->free();
    // Close connection
    $db->close();
    // Return the data
    echo json_encode($rows);        
} else {
    trigger_error('Query failed: ' . $queryString . ' Error: ' . $db->error, E_USER_ERROR);
}
?>
SQL
CREATE TABLE IF NOT EXISTS `peanutlabs` (
    `id` mediumint(9) NOT NULL AUTO_INCREMENT,
    `userId` text COLLATE utf8_unicode_ci NOT NULL,
    `status` char(1) COLLATE utf8_unicode_ci NOT NULL,
    `surveyId` text COLLATE utf8_unicode_ci NOT NULL,
    `currencyAmount` int(11) NOT NULL,
    `transactionId` text COLLATE utf8_unicode_ci NOT NULL,
    `publisherCut` float NOT NULL,
    `timestamp` int(11) NOT NULL,
    UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=10 ;