Last active
December 26, 2019 17:12
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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