C# .NET Communication with API

Post Affiliate Pro API is PHP-based, but you can also access it from other programming languages.

The only requirement is to correctly implement an HTTP POST request and properly encode your JSON request to our server.

Example: Making a Request Using C# in .NET

Below is an example of such a request using C# in .NET (as a simple console application):

//    EXAMPLE OF LOGIN REQUEST 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a request using a URL that can receive a POST. 
            WebRequest request = WebRequest.Create("https://getting-started.postaffiliatepro.com/scripts/server.php");
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Create POST data and convert it to a byte array.
            // WRITE JSON DATA TO VARIABLE D
            string postData = "D={\"requests\":[{\"C\":\"Pap_Api_AuthService\", \"M\":\"authenticate\", \"fields\":[[\"name\",\"value\"],[\"Id\",\"\"],[\"username\",\"user@example.com\"],[\"password\",\"ab9ce908\"],[\"rememberMe\",\"Y\"],[\"language\",\"en-US\"],[\"roleType\",\"M\"],[\"apiVersion\",\"\"]]}],\"C\":\"Gpf_Rpc_Server\", \"M\":\"run\"}";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();
            // Get the response.
            WebResponse response = request.GetResponse();
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            Console.WriteLine(responseFromServer);
            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();
        }
    }
}

Description of Fields in the postData Variable

  • C: The main class being accessed (in this case, "Pap_Api_AuthService").
  • M: The method of the class to use (in this case, "authenticate").
  • fields: An array of name-value arrays that are sent to the chosen method.

Now, you only need to correctly encode and decode JSON data for your requests.
JSON encoding and decoding in .NET can be accomplished using various libraries that serialize and deserialize data to and from the JSON format.

An example of such a library is Json.NET.

How to Use Json.NET in Your Project

  1. Download the Json.NET library.
  2. Extract Newtonsoft.Json.dll and Newtonsoft.Json.xml from the archive's /bin directory into your application directory.
  3. Add a reference to Newtonsoft.Json.dll within your Visual Studio .NET project.

Now you can decode the response from the request executed in the previous example:

JsonSerializer serializer = new JsonSerializer();
object result = JsonConvert.DeserializeObject(responseFromServer);

It is best practice to create interfaces for each type of request and use deserialization directly into .NET objects; however, this is beyond the scope of this short example.

How to Obtain Proper Request Content

Simply use our PHP API file. For example, here is how you can retrieve data from the Pap_Api_PayoutsHistoryGrid table:

include '../api/PapApi.class.php';

$session = new Pap_Api_Session("path_to_your_server.php_file");
$session->setDebug(true); // This will produce output with request/response
if(!$session->login("merchant_name", "merchant_pass")) {
    die('Failed authentication');
}

$payoutHistoryrequest = new Pap_Api_PayoutsHistoryGrid($session);

$payoutHistoryrequest->sendNow();

$historyGrid = $payoutHistoryrequest->getGrid();
$recordset = $historyGrid->getRecordset();

// Print list of payouts
foreach($recordset as $rec) {
    echo $rec->get('id') . ';' . $rec->get('amount') . ';' . $rec->get('accountid') . ';' . $rec->get('users') . ';' . '
'; }

You will receive an answer similar to this:

REQUEST: {"C":"Gpf_Rpc_Server","M":"run","requests":[{"C":"Pap_Merchants_Payout_PayoutsHistoryGrid","M":"getRows"}],"S":"ep6udpp8i2mgnh70vdp52on5h3"}
RESPONSE: [{"rows":[["id","payouthistoryid","dateinserted","merchantnote","affiliatenote","accountid","amount","users"],["0bc99b08","0bc99b08","2011-03-25 05:39:49","","","default1",99,"1 payee"]],"count":1}]

0bc99b08;99;default1;1 payee;

Notice that rows start with REQUEST and RESPONSE.

  • REQUEST shows what the request string should look like.
  • RESPONSE contains the server’s answer.

For a detailed description of the JSON data model, see:
https://support.qualityunit.com/110653-Data-model-JSON-description

×