Searchmetrics Developers API

Examples

PHP example

Note: in the example below we are using Guzzle to make HTTP requests, but this is not a requirement - you can use any library or plain PHP instead.

require 'vendor/autoload.php';

$API_KEY = 'YOUR-API-KEY';
$API_SECRET = 'YOUR-API-SECRET';

$client = new \GuzzleHttp\Client();

$query = <<<GQL
  query {
    greeting {
      who
    }
  }
GQL;

$res = $client->request(
  'POST',
  'https://graphql.searchmetrics.com/',
  [
    'json' => [
      'query' => $query,
      'variables' => null,
      'operationName' => null
    ],
    'headers' => [
      'sm-api-key' => $API_KEY,
      'sm-api-secret' => $API_SECRET
    ]
  ]);

echo $res->getBody();
// '{"data":{"greeting":{"who":"Hello Name"}}}'

$mutation = <<<GQL
  mutation CreateGreetingMutation(\$template: String!) {
    create_greeting(template: \$template) {
      who
    }
  }
GQL;

$res = $client->request(
  'POST',
  'https://graphql.searchmetrics.com/',
  [
    'json' => [
      'query' => $mutation,
      'operationName' => 'CreateGreetingMutation',
      'variables' => ['template' => 'Have you met']
    ],
    'headers' => [
      'sm-api-key' => $API_KEY,
      'sm-api-secret' => $API_SECRET
    ]
  ]);

echo $res->getBody();
// {"data":{"create_greeting":{"who":"Have you met Name"}}}