admin管理员组文章数量:1323737
I am having issue and I'm trying to figure out what causes this.
SITE A
SITE A Code
web.php
Route::get('/eon/auth/check/login',
[ApiController::class, 'checkLogin']
);
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\MemoModel;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Str;
class ApiController extends Controller
{
public function checkLogin(Request $request){
$username = 'admin';
$token = 'xadasd12324';
$user = User::where('username', $username)->first();
return response()->json([
'username' => $username,
'token' => $token,
'user' => $user->first_name
// 'ok' => $ok
]);
if($oauth_user){
RateLimiter::clear($this->throttleKey($username));
return response()->json([
'status_code' => 200,
'message' => 'Success',
]);
}else{
RateLimiter::hit($this->throttleKey($username), $seconds = 3600);
return response()->json([
'status_code' => 401,
'message' => 'Unauthorized2',
]);
}
}
private function throttleKey($username)
{
return Str::lower(request('username'));
}
private function checkTooManyFailedAttempts($username)
{
if (! RateLimiter::tooManyAttempts($this->throttleKey($username), 50)) {
return;
}
abort(403, 'IP address banned. Too many login attempts.');
}
}
SITE B Code
web.php
Route::get('/eon/oauth/', [ApiController::class, 'oAuth']);
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Str;
class ApiController extends Controller
{
public function oAuth(Request $request){
$username = $request->u;
$token = $request->id;
dd($this->checkAuthToken($token, $username));
}
private function checkAuthToken($token, $username){
$result = Http::get(':8080/one/auth/check/login');
$data = json_decode($result->body(), true);
return $data;
}
}
SITE A Result (calling the api via browser)
enter image description here
SITE B Result
enter image description here
I am trying to display the data from SITE A and do a conditional IF ELSE based on the data I retrieved from SITE A using the SITE B Controller.
I'm not very familiar with how Laravel API works or APIs in general, I tried Guzzle but I am still getting the same error, I guess it's something to do with Laravel restriction or structure with which I am not familiar.
I am having issue and I'm trying to figure out what causes this.
SITE A
SITE A Code
web.php
Route::get('/eon/auth/check/login',
[ApiController::class, 'checkLogin']
);
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\MemoModel;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Str;
class ApiController extends Controller
{
public function checkLogin(Request $request){
$username = 'admin';
$token = 'xadasd12324';
$user = User::where('username', $username)->first();
return response()->json([
'username' => $username,
'token' => $token,
'user' => $user->first_name
// 'ok' => $ok
]);
if($oauth_user){
RateLimiter::clear($this->throttleKey($username));
return response()->json([
'status_code' => 200,
'message' => 'Success',
]);
}else{
RateLimiter::hit($this->throttleKey($username), $seconds = 3600);
return response()->json([
'status_code' => 401,
'message' => 'Unauthorized2',
]);
}
}
private function throttleKey($username)
{
return Str::lower(request('username'));
}
private function checkTooManyFailedAttempts($username)
{
if (! RateLimiter::tooManyAttempts($this->throttleKey($username), 50)) {
return;
}
abort(403, 'IP address banned. Too many login attempts.');
}
}
SITE B Code
web.php
Route::get('/eon/oauth/', [ApiController::class, 'oAuth']);
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Str;
class ApiController extends Controller
{
public function oAuth(Request $request){
$username = $request->u;
$token = $request->id;
dd($this->checkAuthToken($token, $username));
}
private function checkAuthToken($token, $username){
$result = Http::get('http://site-a.local:8080/one/auth/check/login');
$data = json_decode($result->body(), true);
return $data;
}
}
SITE A Result (calling the api via browser)
enter image description here
SITE B Result
enter image description here
I am trying to display the data from SITE A and do a conditional IF ELSE based on the data I retrieved from SITE A using the SITE B Controller.
I'm not very familiar with how Laravel API works or APIs in general, I tried Guzzle but I am still getting the same error, I guess it's something to do with Laravel restriction or structure with which I am not familiar.
Share Improve this question edited Mar 1 at 12:17 halfer 20.3k19 gold badges109 silver badges202 bronze badges asked Jan 13 at 0:20 JoneeelJoneeel 32 bronze badges 7- Are both of these Laravel sites hosted on the same machine? – Joundill Commented Jan 13 at 1:13
- Check the site URL name is corrected. also add functionality to try & catch exception handling for the actual error. – Developer Nilesh Commented Jan 13 at 6:22
- Hello @Joundill Yes, they are both hosted in laravel and I am testing it on my local machine using the same web server – Joneeel Commented Jan 13 at 8:54
- @DeveloperNilesh I tried and still I got the same display which is NULL – Joneeel Commented Jan 13 at 8:55
- if you check with try catch and print the exception, we can track the error. – Developer Nilesh Commented Jan 14 at 12:02
1 Answer
Reset to default 0The $result->body()
will return string
not JSON data.
you should try as following.
try {
$url = 'http://site-a.local:8080/one/auth/check/login';
// a small timeout
$headResponse = Http::timeout(10)->head($url);
// check if url has response or not
if (!$headResponse->ok()) {
return response()->json(['message' => 'URL is not accessible or does not exist.'], 404);
}
// get the response with time
$response = Http::timeout(30)->get($url);
// if response is ok
if (!$response->ok()) {
return response()->json(['message' => 'Failed to access URL: '], 400);
}
// this contain all json data and you can access like $response['user']
return $response;
} catch (\Exception $e) {
return response()->json(['message' => 'Error accessing URL: ', 'error' => $e->getMessage()], 500);
}
本文标签: laravelGetting a Null result when using json data from Site A using Site B ControllerStack Overflow
版权声明:本文标题:laravel - Getting a Null result when using json data from Site A using Site B Controller - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742118979a2421604.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论