Last active
November 23, 2020 10:36
-
-
Save igoralves1/e3b385046858a36ced513cdad54f9cf4 to your computer and use it in GitHub Desktop.
Consuming API endpoints with Laravel 5+ AND Guzzle\Http
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
/* | |
Installation -> http://docs.guzzlephp.org/en/latest/overview.html#installation | |
https://www.youtube.com/watch?v=GunXVUqvO-s | |
https://www.udemy.com/http-clients-with-laravel-use-and-consume-services-and-apis/ | |
add Guzzle as a dependency using the composer.phar CLI: | |
php composer.phar require guzzlehttp/guzzle:~6.0 | |
After installing, you need to require Composer's autoloader: | |
require 'vendor/autoload.php'; | |
http://guzzle3.readthedocs.io/http-client/response.html#json-responses | |
http://docs.guzzlephp.org/en/latest/quickstart.html | |
*/ | |
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\Route; | |
use GuzzleHttp\Client; | |
class ProfessionalController extends Controller | |
{ | |
public function home() | |
{ | |
return "professional public home"; | |
} | |
public function getstate() | |
{ | |
$client = new Client(['base_uri' => 'http://drefapi.com/']); | |
$response = $client->request('GET', 'api/user/city'); | |
$body = $response->getBody(); | |
$content =$body->getContents(); | |
$arr = json_decode($content,TRUE); | |
//echo"<pre>"; | |
//print_r(get_class_methods($response)); | |
//print_r(get_class_methods($body)); | |
//print_r($arr); | |
//echo"</pre>"; | |
//dd(json_decode($content,true)); | |
return $arr; | |
} | |
} | |
<?php | |
Route::get('/', function () { | |
return view('index'); | |
}); | |
Route::get('/cityAPI','ProfessionalController@getstate'); | |
//Public routes | |
Route::get('/{drName}', function ($drName) { | |
return "drName = ".$drName; | |
}); | |
Route::get('/{drName}/articles', function ($drName) { | |
echo $drName." All Articles home page"; | |
//return view('welcome'); | |
}); | |
Route::get('/{drName}/articles/{id}', function ($drName,$id) { | |
echo $drName." Articles id=".$id; | |
//return view('welcome'); | |
}); | |
//Route::get('/showStateAPI', function () { | |
// echo "testerota"; | |
// dd(); | |
//}); | |
//Private routes. | |
//Must to be logged to get access to this portion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment