API:Filters

From HasOffers

In progress

The filter parameter allows you to filter and limit the objects returned in your results according to the criteria that you set. The most basic filtering uses a key-value relationship to set the criteria, whereby the key is Model.field and the value is the criteria for the object's value.

Contents

Basic Usage

In the following example we implement filters to find all active subscription-based offers which have a subscription frequency set to "monthly".

<?php
$base = 'https://api.hasoffers.com/Api?';
 
$params = array(
	'Format' => 'json'
	,'Target' => 'Offer'
	,'Method' => 'findAll'
	,'Service' => 'HasOffers'
	,'Version' => 2
	,'NetworkId' => 'my-network-id'
	,'NetworkToken' => 'my-api-key'
	,'filters' => array(
		'Offer.status' => 'active'
		,'Offer.is_subscription' => true
		,'Offer.subscription_frequency' => 'monthly'
	)
);
 
$url = $base . http_build_query( $params );
 
$result = file_get_contents( $url );
 
echo '<pre>';
print_r( json_decode( $result ) );
echo '</pre>';
?>
 

Using the OR Operator

Unlike the previous example from above which only finds objects which match all conditions given, the OR operator will find objects that match any of the criteria passed within the OR parameter.

In this example we implement filters to find all active or pending offers.

<?php
 
$base = 'https://api.hasoffers.com/Api?';
 
$params = array(
	'Format' => 'json'
	,'Target' => 'Offer'
	,'Method' => 'findAll'
	,'Service' => 'HasOffers'
	,'Version' => 2
	,'NetworkId' => 'my-network-id'
	,'NetworkToken' => 'my-api-key'
        ,'filters' => array(
            'OR' => array(
                array('status' => 'pending')
                ,array('status' => 'active')
            )
        )
        ,'sort' => array('Offer.name' => 'asc')
);
 
$url = $base . http_build_query( $params );
 
$result = file_get_contents( $url );
 
echo '<pre>';
print_r( json_decode( $result ) );
echo '</pre>';
?>

Comparison Operators

Comparison operators allow you to filter your objects more precisely and give you a greater range of complex filtering, such as checking date and number ranges, or checking a list for a certain value.

  • EQUAL_TO -
  • NOT_EQUAL_TO -
  • LESS_THAN - Finds objects which evaluate to less than (<) to the provided value.
  • LESS_THAN_OR_EQUAL_TO - Finds objects which evaluate to less than or equal (<=) to the provided value.
  • GREATER_THAN - Finds objects which evaluate to greater than (>) to the provided value.
  • GREATER_THAN_OR_EQUAL_TO - Finds objects which evaluate to greater than or equal (>=) to the provided value.
  • LIKE - Search comparison; you may also use % as a wildcard.
  • NOT_LIKE - Search comparison which finds objects without the given query; you may also use % as a wildcard.
  • BETWEEN [stats only] - Finds objects which have a value that matches the given range. This works with numbers and dates.
  • BITWISE_AND -
  • NULL - Finds objects which the value for the field has not been defined.
  • NOT_NULL - Finds objects which the value for the field has been defined.
  • TRUE - Finds objects which the value for the field is true.
  • FALSE - Finds objects which the value for the field is false.

Advanced Usage

 
$base = 'https://api.hasoffers.com/Api?';
 
$params = array(
	'Format' => 'json'
	,'Target' => 'Offer'
	,'Method' => 'findAll'
	,'Service' => 'HasOffers'
	,'Version' => 2
	,'NetworkId' => 'my-network-id'
	,'NetworkToken' => 'my-api-key'
	,'filters' => array(
		'Offer.name' => array( 'LIKE' => 'My Offer' )
		,'Offer.default_payout' => array( 'GREATER_THAN_OR_EQUAL_TO' => 10 )
	)
);
 
$url = $base . http_build_query( $params );
 
$result = file_get_contents( $url );
 
echo '<pre>';
print_r( json_decode( $result ) );
echo '</pre>';
 

 
Email this page to a friend or co-worker