Skip to content

Product Search with our PHP client

The examples below are using our PHP SDK, which is available via Packagist.

Start by making a Searcher-instance.

php
use Relewise\Searcher;
// You have to `use` the above to create a Searcher.

$searcher = new Searcher(
  "00000000-0000-0000-0000-000000000001", 
  "your api key");
$searcher->serverUrl = "the server URL for the dataset";

To make requests with the SDK you need to use the types from our PHP SDK. The types are located in the namespace Relewise\Models and an example of using a concrete type could be the following:

php
use Relewise\Models\ProductSearchRequest;

Before going any further, make sure you have read the following:

  • Read about how to authenticate against our API here.
  • Read about handling different types of users here.

Also remember to specify your Server URL in your request.

The following shows an example on how to perform a product search with 3 selected facets.

php
$request = ProductSearchRequest::create(
  Language::create("da"),
  Currency::create("DKK"),
  $this->getUser(),
  "Search overlay",
  "gaffel",
  skip: 0,
  take: 10
);

$request->setSettings(
  ProductSearchSettings::create()
    ->setSelectedProductProperties(
      SelectedProductPropertiesSettings::create()
        ->setDisplayName(true)
        ->setCategoryPaths(true)
        ->setBrand(true)
        ->setDataKeys("Description", "ImagePath")
        ->setAllVariants(true)
    )
);

$request->setFacets(
  ProductFacetQuery::create()
    ->addToItems(
      BrandFacet::create("77", "93")
    )
    ->addToItems(
      ProductDataStringValueFacet::create(
        DataSelectionStrategy::Product,
        "Environment",
        array("Selection 1"),
        Null
      )
    )
    ->addToItems(
      ProductDataBooleanValueFacet::create(
        DataSelectionStrategy::Product,
        "InStock",
        array(true),
        Null
      )
    )
);

$response = $searcher->productSearch($request);

Using facets and selecting values.

Adding the following code to the request will return a facet with the different brands from the products matching the search.

php
$request->setFacets(
  ProductFacetQuery::create()
    ->addToItems(
      BrandFacet::create()
    )
);

When the user selects a brand, update the code to the following:

php
$request->setFacets(
  ProductFacetQuery::create()
    ->addToItems(
      BrandFacet::create("93")
    )
);

You can learn more about facets here.

Don't know us? Don't worry - you can find more information about us, by visiting our main page www.relewise.com