Skip to content

Instantly share code, notes, and snippets.

@devbanana
Last active December 26, 2019 17:12
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/10887cce6fcbaa525a0e3832c23a45b6 to your computer and use it in GitHub Desktop.
Save devbanana/10887cce6fcbaa525a0e3832c23a45b6 to your computer and use it in GitHub Desktop.
This gist accepts a search query and calls the IEX Cloud API to fetch all stock tickers that match the query.
<?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;
}
$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