Skip to content

Getting Started

Dominik Brader edited this page Jul 17, 2020 · 1 revision

Sending Requests

Configuration

Sending requests begins by creating a Config object, which holds general information about how the request is sent or what service it should request. The client sending the requests may use this information to properly send an receive data from the requested endpoint.

use FINDOLOGIC\Api\Config;

$config = new Config('ABCDABCDABCDABCDABCDABCDABCDABCD');

// Alternative long form
// $config = new Config();
// $config->setServiceId('ABCDABCDABCDABCDABCDABCDABCDABCD');

Create Your First Request

We also need to know which endpoint we are requesting. In case we want to do search, we create a new SearchRequest or if we want to do a navigation request we can create a new instance of NavigationRequest. Note: You can also create a SuggestionRequest instance for autocomplete requests.

use FINDOLOGIC\Api\Requests\SearchNavigation\NavigationRequest;
use FINDOLOGIC\Api\Requests\SearchNavigation\SearchRequest;

$request = new SearchRequest();
// Or a navigation request
// $request = new NavigationRequest();

If you request the API just with this information, you would usually receive a proper response. Our API wrapper prevents this, as we may need further information about the request, such as the shop URL or the user ip, so the requests can be billed properly.

Required

  • setShopUrl URL of your shop, without protocol (http/https).
  • setUserIp IP of the requester e.g. the user in the shop. IPv4 only, IPv6 is not supported.
  • setRevision Version of your API wrapper. Has no effect on the result and is usually used to inform customers that use an outdated version of their plugin.
use FINDOLOGIC\Api\Requests\SearchNavigation\SearchRequest;

$request = new SearchRequest();

// Required fields
$request->setShopUrl('www.your-shop.com');
$request->setUserIp('123.45.67.8');
$request->setRevision('1.0.0');

// Optional add the query that the user entered
$request->setQuery('shoes');

// Optional set the page on which the page was fired aka. referer
$request->setReferer('https://www.your-shop.com/home');

Send Requests With The Client

The Client takes the request and sends it to the Findologic API. Upon receiving the response, it is passed to object oriented response classes. Depending on the set OutputAdapter, the instance may differ.

OutputAdapter is only available for SearchRequest and NavigationRequest instances. It has no effect for SuggestRequest instances.

use FINDOLOGIC\Api\Client;
use FINDOLOGIC\Api\Definitions\OutputAdapter;
use FINDOLOGIC\Api\Responses\Autocomplete\SuggestResponse;
use FINDOLOGIC\Api\Responses\Html\GenericHtmlResponse;
use FINDOLOGIC\Api\Responses\Json10\Json10Response;
use FINDOLOGIC\Api\Responses\Xml21\Xml21Response;

// ... Config and Request initialization ...
$request->setOutputAdapter(OutputAdapter::JSON_10);

// Other output adapters
// $request->setOutputAdapter(OutputAdapter::XML_21);
// $request->setOutputAdapter(OutputAdapter::HTML_20);
// $request->setOutputAdapter(OutputAdapter::HTML_30);
// $request->setOutputAdapter(OutputAdapter::HTML_31);

$client = new Client($config);

/** @var Json10Response|Xml21Response|GenericHtmlResponse|SuggestResponse $response */
$response = $client->send($request);

Parsing the response

Since we already get a pretty nice response object, we can already work with that to get all the data we need.

Get all products

JSON 1.0

use FINDOLOGIC\Api\Responses\Json10\Properties\Item;

// ... Request Building and sending it ...

/** @var Item[] $items */
$items = $response->getResult()->getItems();

// ... Loop over all items ...
echo $item->getName();
echo $item->getPrice();
echo $item->getSummary();

// ... Or get data from your database to show additional data ...
$article = $articleRepository->find($item->getId());
echo $article->getStock();

XML 2.1

use FINDOLOGIC\Api\Responses\Xml21\Properties\Product;

// ... Request Building and sending it ...

/** @var Product[] $products */
$products = $response->getProducts();

// ... Loop over all products ...
echo $product->getId(); // XML has very limited data about products
echo $product->getRelevance();

// ... Or get data from your database to show additional data ...
$article = $this->em->find($product->getId());
echo $article->getStock();