Skip to content

Instantly share code, notes, and snippets.

@tw2113

tw2113/gist:6285629

Last active Dec 21, 2015
Embed
What would you like to do?
<?php
//USAGE
$semantics3 = biffleSemantics3::get_instance( $query );
$raw = $semantics3->get_product_search('products');
class biffleSemantics3 {
/*
Documentation
https://www.semantics3.com/docs/
Notes:
minimum price param
Example: {"price":{"gte":49.99}}
new only param
Example: {"sitedetails":{"latestoffers":{"condition":"new"}}}
save cat_id per mpn. When querying for mpn. check if we have a saved cat_id associated, search with both? just cat_id?
Save cat ID into post meta for the product.
ALL queries to the products endpoint must have either a cat_id, upc, ean, or sem3_id, except the search shown below.
GET https://api.semantics3.com/v1/products?q={"search":"Apple Macbook"}
TODO: error status handling.
*/
private static $endpoint = 'https://api.semantics3.com/v1/'; //1000 calls/day
private static $api_key = 'SEM36F540DB8B3E3D845703DD5ED507F445B'; //michael's free tier keys. Temp use.
private static $api_secret_key = 'OTE2NTFlOWNhY2RmOTkwMjkyMTM2MzczMDI5MGNmODY'; //michael's free tier keys. Temp use.
private static $instance;
//Helpers for frequently used Semantics3 category IDs.
public $electronics_cat_id = '13658';
public $appliances_cat_id = '18892';
public $musical_cat_id = '24271';
public $powertools_cat_id = '21949';
public $sports_cat_id = '2717';
static function get_instance( $query = NULL, $location = NULL, $sort_price = NULL, $return_count = NULL ) {
if ( ! static::$instance )
static::$instance = new self( $query, $location, $sort_price, $return_count );
return static::$instance;
}
public function __construct() {
$this->search_query = $query;
$this->location = $location;
$this->sort_price = $sort_price;
$this->return_count = $return_count;
//So that we can be authorized
include_once plugin_dir_path( dirname(__FILE__ ) ) . 'oauth-php/library/OAuthStore.php';
include_once plugin_dir_path( dirname(__FILE__ ) ) . 'oauth-php/library/OAuthRequester.php';
}
/**
* Used to return results from search query.
*
* @since 1.0
*
* @return array array of filtered results, including query, and then raw results from api.
*/
public function get_product_search() {
$query_params = json_encode( array( 'search' => $this->search_query ) );
$results = $this->get_product_results( 'products', $query_params );
//first part is JUST the data we want. second part is all of it
return array( $this->googlify( $results, $this->search_query ), $results );
}
/**
* Construct our oAuth signed request and send to Semantics3
* @param string $resource Resource type to be used with url construction. Either "products" or "categories"
* @param string $query_params Parameters for our request to send to Semantics3
* @return string array of decoded json holding our query results.
*/
public function get_product_results( $resource , $query_params ) {
//construct our url
$url = $this->construct_url( $resource, $query_params );
//Grab our api keys
$options = array(
'consumer_key' => $this->api_key,
'consumer_secret' => $this->api_secret_key,
);
//Set up our oAuth
OAuthStore::instance( "2Leg", $options );
$request = new OAuthRequester( $url, 'GET', "" );
$result = $request->doRequest();
return json_decode( $result['body'], true );
}
//Turn our results into an array best matching our original searchGoogle array. Pending how long we'll need this version.
public function googlify( $api_results, $query = '' ) {
$google_like = array();
foreach( $api_results['results'] as $result ) {
$google_like[] = array(
'google_id' => $result['cat_id'],
'search_term' => $query,
'title' => $result['name'],
'description' => $result['description'],
'link' => $result['link'],
'brand' => $result['brand'],
'gtin' => $result['sem3_id'],
'merchant' => $result['merchant'],
'google_merchant_id' => '',
'image_url' => $result['images'],
'location' => '',
'price' => $result['price'],
'tax' => $result['tax'],
'shipping' => '', //part of google results.
'run_date_time' => date( 'Y-m-d H:i:s')
);
}
//indicate that it's semantics3
$google_like['semantics3'] = 'Semantics3 Results';
return $google_like;
}
/**
* Reduce our variable down to just what we want to deal with, and cut out the rest of the returned data.
* @param array $api_results API data from Semantics3
* @return array filtered array of just what we want.
*/
public function get_filtered_results( $api_results, $query = '' ) {
if ( empty( $api_results ) )
return;
$new_result = array();
//need to match up with results array keys.
//Shipping and availability will be in sitedetails
$desired_fields = array(
'price',//
'tax',
'link',
'merchant',
'weight',//in milligrams
'features',//specs
'name',//the title
'description',//the content
'images', //featured image
'brand',//
'category',//cats
'cat_id',//from sematics3 categorization
'mpn',//
'sitedetails'//merchants with it.
);
foreach( $api_results['results'] as $field => $value ) {
if ( in_array( $field, $desired_fields ) ) {
$new_result[][ $field ] = $value;
}
}
return $new_result;
}
/**
* Retrieve results using a specified mpn number
* @param string $mpn mpn number to use with request
* @return array array holding our resulting data.
*/
public function get_product_results_by_mpn( $mpn = '' ) {
$query_params = json_encode( array( 'mpn' => $mpn ) );
$results = $this->get_product_results( 'products', $query_params);
return $this->googlify( $results );
}
/**
* Retrieve results using a specified cat_id number
* @param string $cat_id Semantics3 defined cat ID.
* @return array array holding our resulting data.
*/
public function get_product_results_by_cat_id( $cat_id = '' ) {
$query_params = json_encode( array( 'cat_id' => $cat_id ) );
$results = $this->get_product_results( 'products', $query_params);
return $this->googlify( $results );
}
/**
* Fetch updated data via unique ID for post.
* @param integer $post_id ID for the post to fetch an update for
* @return array|bool Fresh data from Semantics3, false on no data or no post ID.
*/
public function get_product_update_by_unique( $post_id = 0 ) {
if ( 0 == $post_id )
return false;
//Grab our identifier for the product. Returned value will be array, for use below.
$unique = $this->get_product_unique_id( $post_id );
if ( $unique ) {
$query_params = json_encode( $unique );
$results = $this->get_product_results( 'products', $query_params);
return $this->googlify( $results );
} else {
return false;
}
}
/**
* Grab and prioritize our unique identifier.
*
* Will prefer UPC first, but will use Semantics3 ID if it must.
* @param integer $post_id Post ID to fetch
* @return array|bool array to JSON encode for the query on success, false on no data or no post ID.
*/
public function get_product_unique_id( $post_id = 0 ) {
if ( 0 == $post_id )
return false;
//Try to find a UPC first
$$unique = get_post_meta( $post_id, 'sm3_upc', true );
$type = 'upc';
//Otherwise we'll use Semantics3's ID
if ( false === $unique ) {
$unique = get_post_meta( $post_id, 'sm3_sem3_id', true );
$type = 'sem3_id';
}
if ( false === $unique )
return false;
//Return a json_encode ready array.
return array( $type => $unique );
}
/**
* Piece together our url to send request to based on resource type and query parameters
* @param string $resource Type of resource to query for. Ether "products" or "categories"
* @param string $query_params Parameters for our request to send to Semantics3
* @return string urlencoded url to use with http request
*/
private function construct_url( $resource = 'products', $query_params ) {
$resource = strtolower( $resource );
if ( 'products' == $resource ) {
$url = $this->endpoint . 'products?q=';
} else {
$url = $this->endpoint . 'categories?q=';
}
$url .= urlencode( $query_params );
return $url;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.