Last active
December 26, 2019 17:13
-
-
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.
This file contains hidden or 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] = $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