Skip to content

Instantly share code, notes, and snippets.

@devbanana
Last active December 26, 2019 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devbanana/1eb731fe2daa991b9472f3ed084b6f70 to your computer and use it in GitHub Desktop.
Save devbanana/1eb731fe2daa991b9472f3ed084b6f70 to your computer and use it in GitHub Desktop.
This gist accepts a search query and uses the IEX Cloud API to fetch all stock tickers that match the query. It returns both the ticker symbol and company name.
<?php
require_once 'vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
$request = Request::createFromGlobals();
if (!isset($_ENV['IEX_TOKEN'])) {
throw new Exception('IEX token not found.');
}
$token = $_ENV['IEX_TOKEN'];
$response = new JsonResponse();
$response->headers->set('Access-Control-Allow-Origin', '*');
if ($request->request->has('query')) {
$query = $request->request->get('query');
$search = json_decode(file_get_contents(
"https://cloud.iexapis.com/stable/search/$query?token=$token"
));
$results = [];
foreach ($search as $item) {
$results[$item->symbol] = $item->securityName;
}
$data = [
'results' => $results,
];
$response->setData($data);
} else {
$response->setData('Search query not found');
$response->setStatusCode(JsonResponse::HTTP_BAD_REQUEST);
}
$response->send();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment