-
-
Save oxodesign/3f400430ba760a864ba939dfb85584c0 to your computer and use it in GitHub Desktop.
Laravel Batch Request
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
// Simple. This method allows you to make a request like http://myapi.com/api/batch?request=[{"url":"/api/profile/1", "type":"GET", "request_id":"1"}, {"url":"/api/profile/2", "type":"GET", "request_id":2}] | |
$requests = \Input::get('request'); | |
$requests = json_decode($requests); | |
$output = array(); | |
foreach ($requests as $request) { | |
$url = parse_url($request->url); | |
$query = array(); | |
if (isset($url['query'])) { | |
parse_str($url['query'], $query); | |
} | |
$req = \Request::create($request->url, $request->type, $query); | |
if (isset($request->request_id)) { | |
$output[$request->request_id] = json_decode(\Route::dispatch($req)->getContent()); | |
} else { | |
$output[] = json_decode(\Route::dispatch($req)->getContent()); | |
} | |
} | |
return \Response::json(array('response' => $output)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment