elective-stereophonic
elective-stereophonic
DGS PHP Codesnippets singapore
Please login or register.

Login with username, password and session length
Advanced search  

News:

Latest Stable Nxt Client: Nxt 1.12.2

Author Topic: DGS PHP Codesnippets  (Read 2261 times)

Tosch110

  • Board Moderator
  • Hero Member
  • ****
  • Karma: +211/-18
  • Offline Offline
  • Posts: 2365
    • View Profile
DGS PHP Codesnippets
« on: August 25, 2014, 06:36:38 pm »

Hello,

since I have built the Sinanboom DGS shop or am working on the melodius app I want other people to be able to build their own shop. Melodius is designed to help people and make it easy for artists to set up shops. Still, for techie people it might be good to set something like this up themselves. For everyone who knows PHP, it might be a good help. For anybody who does not also, because it should ne be too hard to set up. I post this here before to keep building on it and have it reviewed. This guide is not a good example to build a secure shop, but you might want to use some snippets to display data of purchases on your website or understand the functionalities of NXT on testnet. When building your own shop you should find a secure way to store the passphrase when doing POST requests.

First, you need PHP. You can either host this on your webserver or localhost. If you want to get PHP to your computer you might want to use something like EasyPHP.

Localhost set-up

1) Try everything on NXT Testnet. Please get the current NXT version, open the document nxt/conf/nxt-default.properties with a texteditor and set:
Code: [Select]
nxt.isTestnet=true
2) get PHP enabled and run one of the functions from below

On a webserver

If you have no chance in running NXT localhost, you could use public peers instead. Replace the $server=''; variable from localhost to a public node. Examples can be found when you have NXT running and click on 'Peers' in the NXT Client or use the following link in your Browser when you have testNXT running. For security reasons it is not recommended to send your secretPhrase to any public node so you might want to skip these functions and just display data when using public nodes

TestNXT

If you need TestNXT for your account please ask here https://nxtforum.org/testnet/some-testnxt-to-test-asset-exchange/

Tosch110

  • Board Moderator
  • Hero Member
  • ****
  • Karma: +211/-18
  • Offline Offline
  • Posts: 2365
    • View Profile
Re: DGS PHP Codesnippets
« Reply #1 on: August 25, 2014, 06:37:52 pm »

Display current NXT time:

Code: [Select]
<?php
//NXT Timestamp: 24.11.2013 13:00:00 - the NXT time is in seconds from this point
$getTime file_get_contents($server.'?requestType=getTime');
$obj_time json_decode($getTime);
$nxt_time $obj_time->{'time'};

echo '<h1>Nxt Time</h1>'.htmlspecialchars($nxt_time);
?>

Instead of using localhost, you can use one of the public peers to get the information or for testing purposes. Take any host that fits your needs:
http://nxtpeers.com/index.php
« Last Edit: December 07, 2014, 02:51:16 pm by Tosch110 »
Logged

Tosch110

  • Board Moderator
  • Hero Member
  • ****
  • Karma: +211/-18
  • Offline Offline
  • Posts: 2365
    • View Profile
Re: DGS PHP Codesnippets
« Reply #2 on: August 25, 2014, 06:40:20 pm »

For POST requests I am using curl. Here you can find the function curl_request used later in other functions, too. Make sure to have this at the beginning when going further.
Below you can find how to list an item example on the DGS. Modify the variables to edit the Listening. To List the item, use the current commented (disabled) data at the end.
Account examples are:

   $secretPhrase = 'ThisIsATestSecretPhrasefortheDGS';
   $server = 'http://localhost:6876/nxt';
   $shop = 'NXT-J33Z-GQNN-WF2X-7AZSS';

Code: [Select]
<?php 
//curl request function

function curl_request($url$fields) {

$useragent 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko)
Chrome/8.0.552.224: Safari/534.10'
// notice this
//url-ify the data for the POST
        
$fields_string '';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string'&');

//open connection
$ch curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL$url);
curl_setopt($ch,CURLOPT_POSTcount($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS$fields_string);
curl_setopt($chCURLOPT_RETURNTRANSFER1);
curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);

curl_setopt($chCURLOPT_USERAGENT$useragent);
//execute post


$result curl_exec($ch);
return $result;

//close connection
curl_close($ch);

}

//Build the function for listening a DGS Item with curl and send them via POST
function DGSListItem($name$description$tags$priceNQT$secretPhrase$feeNQT$deadline$quantity$server) {

//our defined server is the url used here
$url $server;
//set POST variables
$data = array(
'requestType' => 'dgsListing',
'name' => urlencode($name),
'description' => urlencode($description),
'tags' => urlencode($tags),
'priceNQT' => urlencode($priceNQT),
'deadline' => urlencode($deadline),
'feeNQT' => urlencode($feeNQT),
'quantity' => urlencode($quantity),
'secretPhrase' => urlencode($secretPhrase)
);

//make the curl request and get the listening details as JSON
$json curl_request($url$data);

return $json;

}

/*
$name = 'Example Item';
$description = 'The desciption to be seen below the Item';
$tags = 'Three,Tags,Allowed';
$priceNQT = 5 * 100000000; //NQT = + 8 decimals. Price here 5 NXT
$feeNQT = 100000000; //fee = 1 NXT
$deadline = 600;
$quantity = 30;

$list_json = DGSListItem($name, $description, $tags, $priceNQT, $secretPhrase, $feeNQT, $deadline, $quantity, $server);

echo '<h1>List Item (json output)</h1>';
var_dump($list_json);
*/
?>
« Last Edit: August 25, 2014, 07:31:32 pm by Tosch110 »
Logged

Tosch110

  • Board Moderator
  • Hero Member
  • ****
  • Karma: +211/-18
  • Offline Offline
  • Posts: 2365
    • View Profile
Re: DGS PHP Codesnippets
« Reply #3 on: August 25, 2014, 06:42:35 pm »

GET all listed goods from a certain account. Here the example account

Code: [Select]
<?php
function getGoods($shop$server) {

$getGoods file_get_contents($server.'?requestType=getDGSGoods&seller='.$shop);
$obj_goods json_decode($getGoods);
$goods $obj_goods->{'goods'};

return $goods;

}

$json_goods getGoods($shop$server);

echo '<h1>Goods from one Shop (json)</h1>';
var_dump($json_goods);

echo '<h2>Example access</h2>';

$amount_goods count($json_goods);
for($x=0;$x<$amount_goods;$x++) {

echo 'Seller: '.htmlspecialchars($json_goods[$x]->sellerRS);
echo '<br>';
echo 'Name: '.htmlspecialchars($json_goods[$x]->name);

}
?>

« Last Edit: August 25, 2014, 07:31:51 pm by Tosch110 »
Logged

Tosch110

  • Board Moderator
  • Hero Member
  • ****
  • Karma: +211/-18
  • Offline Offline
  • Posts: 2365
    • View Profile
Re: DGS PHP Codesnippets
« Reply #4 on: August 25, 2014, 07:18:14 pm »

The next code snippets require above curl_request function and the variables secretPhrase, server and shop

It shows undelivered Items. Currently it does not deliver the Item when run. You need to get rid of the // before $json_delivery and var_dump($json_delivery) to enable and show Delivery of an Item.
Modify the variables to place your text in the delivery, a download link or whatever.

Code: [Select]
<?php
//get undelivered items
$getItems file_get_contents($server.'?requestType=getDGSPendingPurchases&seller='.$shop);
$obj json_decode($getItems);
$json_purchases $obj->{'purchases'};

echo '<h1>Undelivered Items</h1>';
var_dump($json_purchases);

echo '<h2>Example access</h2>';

$amount_purchases count($json_purchases);
for($x=0;$x<$amount_purchases;$x++) {

echo 'Buyer: '.htmlspecialchars($json_purchases[$x]->buyerRS);
echo '<br>';
echo 'Quantity: '.htmlspecialchars($json_purchases[$x]->quantity);
echo '<br>';
echo 'Purchase: '.htmlspecialchars($json_purchases[$x]->purchase).' this is the product ID';
$undelivered_array[$x] = $json_purchases[$x]->purchase;
}


//purchase Delivery 
function purchaseDelivery ($purchase,$goodsToEncrypt$deadline$feeNQT$secretPhrase$server) {
// URL is again our before defined Serverurl
$url $server;
$data = array(
'requestType' => 'dgsDelivery',
'purchase' => urlencode($purchase),
'goodsToEncrypt' => urlencode($goodsToEncrypt),
'feeNQT' => urlencode($feeNQT),
'deadline' => urlencode($deadline),
'secretPhrase' => urlencode($secretPhrase)
);
//Do the curl function defined at the beginning
$json curl_request($url$data);

return $json;

}
//Undelivered Items
if(!empty($undelivered_array)) {

$amount_undelivered count($undelivered_array);


//Loop through the undelivered Items and make the purchase delivery. You should delay it, or save the different purchase deliverys in a database to prevent double spending.
//To make it automatic, you can use a Cron 
//Be safe with POST requests that contain your secretPassphrase
//You should delay any purchase delivery after the purchase. The more time you delay it after the purchase, the more secure the purchase will be.
for($x=0;$x<$amount_undelivered;$x++) {

echo '<h2>Delivery of: '.htmlspecialchars($json_purchases[$x]->purchase).'</h2>';

$purchase $json_purchases[$x]->purchase;
$goodsToEncrypt 'Here you can put the Info to be seen encrypted by the NXT user';
$feeNQT 100000000;
$deadline 1000;

//$json_delivery = purchaseDelivery($purchase,$goodsToEncrypt, $deadline, $feeNQT, $secretPhrase, $server);

//var_dump($json_delivery);


}


}

?>
« Last Edit: August 25, 2014, 07:32:06 pm by Tosch110 »
Logged

Tosch110

  • Board Moderator
  • Hero Member
  • ****
  • Karma: +211/-18
  • Offline Offline
  • Posts: 2365
    • View Profile
Re: DGS PHP Codesnippets
« Reply #5 on: August 25, 2014, 07:25:49 pm »

This is an example to look at comments done by the Feedback function after purchase.

Code: [Select]
<?php
//Look at public comments from purchases

$getComments file_get_contents($server.'?requestType=getDGSPurchases&seller='.$shop);
$obj_comments json_decode($getComments);
$nxt_comments $obj_comments->{'purchases'};


$am_comments count($nxt_comments);

for($x=0;$x<$am_comments;$x++) {


if(!empty($nxt_comments[$x]->publicFeedbacks)) {
$time_comment $nxt_comments[$x]->timestamp;
$time_comment $time_comment 1385294400;
$time_comment date("F j, Y, g:i a",$time_comment);
$comment_buyer $nxt_comments[$x]->buyerRS;

echo '<h1>Public Feedback</h1>';
echo 'Comment: ';
echo htmlspecialchars($nxt_comments[$x]->publicFeedbacks[0]);
echo 'Time: ';
echo htmlspecialchars($time_comment); 

}

}
?>

« Last Edit: August 25, 2014, 07:32:22 pm by Tosch110 »
Logged
 

elective-stereophonic
elective-stereophonic
assembly
assembly