Code Examples
PHP Code Examples
Here are some code examples for connecting to the Monetization API via PHP. Be sure to replace key with your Monetization api key.
Note: The below code examples have been tested in PHP 5 only. For compatibility with other PHP versions, some modifications may be required.
Reportlist Function Example
This code example will retrieve a list of all generated reports and their statuses, using the API function reportlist.
<?php
// define settings
$start = "0"; // Report record to start from
$number = "10"; // How many report records to show
#
# Get a list of previously generated reports, using API function reportlist
#
// create query URL, correctly URL encoded -- It is best to urlencode all values, just in case
$query_url = "https://www.above.com/api.php?mode=reportlist&start=" . urlencode($start) ."&num=" . urlencode($number);
// attempt connection and to put response into variable $res
$res = file_get_contents($query_url);
// if connection was successful
if($res) {
// parse XML results
// put response in XML object $results
$results = simplexml_load_string($res); // you could also use a regex to get data, but this way is neater
// check for error
if($results->error) {
// if there was an error, get reason and display
$error_attributes = $results->error->attributes();
// display error
echo "Error: " . $error_attributes->error;
} elseif($results->getName() == "results") { // if there was NOT an error
// get list
foreach($results->result as $result){
// get attributes of each result
$result_attributes = $result->attributes();
// print report details
echo "Report ID: " . $result_attributes['reportid'] . ", Generated: " . $result_attributes['generated'] .
", Status: " . $result_attributes['status'] . "\n";
}
// end get list
}
} else { // if there was a connection error
// print error
echo "Connection error!";
}
?>
Fetch Function Example
This code example will retrieve a generated report, using the API function fetch.
<?
// define settings
$reportid = "12345"; // ID of the report to fetch
$start = "0"; // Report record to start from
$number = "20"; // How many report records to show
#
# Get a list of previously generated reports, using API function reportlist
#
// create query URL, correctly URL encoded -- It is best to urlencode all values, just in case
$query_url = "https://www.above.com/api.php?mode=fetch&reportid=" . urlencode($reportid) . "&start=" . urlencode($start) ."&num=" . urlencode($number);
// attempt connection and to put response into variable $res
$res = file_get_contents($query_url);
// if connection was successful
if($res) {
// parse XML results
// put response in XML object $results
$results = simplexml_load_string($res); // you could also use a regex to get data, but this way is neater
// check if any children exist for <results> -- which indicates report is not returned
if( count($results->children()) != 0 ) {
// check for error
if($results->error) {
// if there was an error, get reason and display
$error_attributes = $results->error->attributes();
// display error
echo "Error: " . $error_attributes->error;
} elseif($results->getName() == "results") { // no error, but status returned
// get status of report
$status = $result->attributes();
if($status['status'] == "Generating") {
// show status
echo "Report is currently generating - try again in a few minutes.";
} else {
// unknown status
echo "Unknown Status: " . $status['status'];
}
}
} elseif( count($results->children()) == 0 ) { // report is ready and has been returned
#
# What you do with the CSV output now is up to you!
# You could save it to a file or parse it and insert it into a database
#
# Remember: The first row is the heading - it does not contain values
#
echo "CSV: " . trim($results); // We trim to remove extra linebreaks and spaces
}
} else { // if there was a connection error
// print error
echo "Connection error!";
}
?>