<?php
/**
* Native IdaSearch API client
*
* The client needs the following, non PHP libraries:
* - cUrl (http://php.net/manual/en/book.curl.php)
* - JSON (http://php.net/manual/en/book.json.php)
*
* @version 1.0.0
*/
class IdaSearchNativeClient
{
/**
* @const Hostname of IdaSearch API server
*/
const API_HOST = 'https://api.idasearch.com';
/**
* @const Resource of event post API
*/
const API_WHOAMI_RESOURCE = '/whoami/';
/**
* @const Resource of domain list API
*/
const API_DOMAINLIST_RESOURCE = '/domains/';
/**
* @const Resource of suggestion list API
*/
const API_SUGGESTIONLIST_RESOURCE = '/content-suggestion/url-based/list?domain=@DOMAIN@';
/**
* @const Resource of suggestion list remove API
*/
const API_SUGGESTIONLIST_REMOVE_RESOURCE = '/content-suggestion/url-based/remove?domain=@DOMAIN@&url=@URL@';
/**
* @const Resource of suggestion contents API
*/
const API_SUGGESTIONLIST_CONTENT_RESOURCE = '/content-suggestion/url-based/contents?domain=@DOMAIN@&url=@URL@';
/**
* HTTP request timeout in seconds
* After API_TIMEOUT seconds the methods will return 0 HTTP status
*/
const API_TIMEOUT = 10;
/**
* test API connection
*
* @param string $user API user name
* @param string $password API password (plain, not encoded)
*
* @return array HTTP response data
*/
public static function whoami($user, $password)
{
$url = static::API_HOST.static::API_WHOAMI_RESOURCE;
return static::requestHttp('get', $user, $password, $url);
}
/**
* get domain list of user
*
* @param string $user API user name
* @param string $password API password (plain, not encoded)
*
* @return array HTTP response data
*/
public static function getDomains($user, $password)
{
$url = static::API_HOST.static::API_DOMAINLIST_RESOURCE;
return static::requestHttp('get', $user, $password, $url);
}
/**
* get suggestion lists of domain
*
* @param string $user API user name
* @param string $password API password (plain, not encoded)
* @param string $domain Domain name
*
* @return array HTTP response data
*/
public static function getSuggestionLists($user, $password, $domain)
{
$url = static::API_HOST.
str_replace(
'@DOMAIN@',
$domain,
static::API_SUGGESTIONLIST_RESOURCE
)
;
return static::requestHttp('get', $user, $password, $url);
}
/**
* create new suggestion list for domain
*
* @param string $user API user name
* @param string $password API password (plain, not encoded)
* @param string $domain Domain name
* @param string $url Url of suggestion list
*
* @return array HTTP response data
*/
public static function createSuggestionList($user, $password, $domain, $url)
{
$apiUrl = static::API_HOST.
str_replace(
'@DOMAIN@',
$domain,
static::API_SUGGESTIONLIST_RESOURCE
)
;
$apiData = array(
'url' => $url,
);
return static::requestHttp('post', $user, $password, $apiUrl, $apiData);
}
/**
* get suggestion list contents of url
*
* @param string $user API user name
* @param string $password API password (plain, not encoded)
* @param string $domain Domain name
* @param string $url Url of suggestion list
*
* @return array HTTP response data
*/
public static function getSuggestionListContents($user, $password, $domain, $url)
{
$apiUrl = static::API_HOST.
str_replace(
array(
'@DOMAIN@',
'@URL@',
),
array(
$domain,
$url,
),
static::API_SUGGESTIONLIST_CONTENT_RESOURCE
)
;
return static::requestHttp('get', $user, $password, $apiUrl);
}
/**
* set suggestion list content of url
*
* @param string $user API user name
* @param string $password API password (plain, not encoded)
* @param string $domain Domain name
* @param string $url Url of suggestion list
* @param string $link Link of suggestable content
* @param string $title Title of suggestable content
* @param string $description Description of suggestable content
* @param string $image Image of suggestable content
* @param string $disabled Disabled state of suggestable content
*
* @return array HTTP response data
*/
public static function setSuggestionListContent($user, $password, $domain, $url, $link, $title, $description, $image, $disabled = false)
{
$apiUrl = static::API_HOST.
str_replace(
array(
'@DOMAIN@',
'@URL@',
),
array(
$domain,
$url,
),
static::API_SUGGESTIONLIST_CONTENT_RESOURCE
)
;
$apiData = array(
array(
'link' => $link,
'title' => $title,
'description' => $description,
'image' => $image,
'disabled' => $disabled,
)
);
return static::requestHttp('post', $user, $password, $apiUrl, $apiData);
}
/**
* set batch suggestion list content of url
*
* @param string $user API user name
* @param string $password API password (plain, not encoded)
* @param string $domain Domain name
* @param string $url Url of suggestion list
* @param array $items Suggestable items
*
* @return array HTTP response data
*/
public static function setBatchSuggestionListContent($user, $password, $domain, $url, $items)
{
$apiUrl = static::API_HOST.
str_replace(
array(
'@DOMAIN@',
'@URL@',
),
array(
$domain,
$url,
),
static::API_SUGGESTIONLIST_CONTENT_RESOURCE
)
;
return static::requestHttp('post', $user, $password, $apiUrl, $items);
}
protected static function requestHttp($method, $user, $password, $url, $postData = array())
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_ENCODING, "gzip, deflate");
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $user.":".$password);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, static::API_TIMEOUT);
curl_setopt($curl, CURLOPT_TIMEOUT, static::API_TIMEOUT);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_MAXREDIRS, 10);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
if (strtolower($method) == 'post') {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postData));
} elseif (strtolower($method) != 'get') {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, strtoupper($method));
}
curl_setopt($curl, CURLOPT_URL, $url);
$curlResult = curl_exec($curl);
$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$headers = explode("\n", substr($curlResult, 0, $headerSize));
foreach ($headers as $index => $header) {
$header = trim($header);
if ($header) {
$headers[$index] = $header;
} else {
unset($headers[$index]);
}
}
return array(
'responseHeaders' => $headers,
'status' => curl_getinfo($curl, CURLINFO_HTTP_CODE),
'errorCode' => curl_errno($curl),
'response' => json_decode(substr($curlResult, $headerSize), true),
);
}
}
A Webb & Flow kizárólag a termékeihez adott hivatalos API kliensekkel kapcsolatos support feladatokat látja el. Az egyedi implementációk hibamentesítésében forráskód hiányában nem tud segíteni, a forráskódot nem szeretné megismerni.