In this tutorial i’m going to get the data from 1 server to another server in Laravel.
1st step
Go to first server and make model for reterieve the data
php artisan make:model TaskboarData
Next go to modal and put below code
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class TaskboarData extends Model
{
use HasFactory;
protected $connection = 'mysqltaskbaord';
protected $table = 'task_boards';
}
Next go to database.php and put below code
'mysqltaskbaord' => [
'driver' => 'mysql',
'host' => env('DB_HOST_TASKBOARD_COUNT', 'localhost'),
'port' => env('DB_PORT_TASKBOARD_COUNT', '3306'),
'database' => env('DB_DATABASE_TASKBOARD_COUNT', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
NEXT GO TO .ENV AND PUT BELOW CODE
DB_CONNECTION_TASKBOARD=mysql
DB_HOST_TASKBOARD_COUNT=127.0.0.1
DB_PORT_TASKBOARD_COUNT=3306
DB_DATABASE_TASKBOARD_COUNT=wz_tasks_board_ms
DB_USERNAME_USER_COUNT=root
DB_PASSWORD_TASKBOARD_COUNT=
DB_CONNECTION_TASKBOARD=mysql
DB_HOST_TASKBOARD_COUNT=127.0.0.1
DB_PORT_TASKBOARD_COUNT=3306
Next go to another first microservice and create controller
php artisan make:controller Api/TaskboardDataController
<?php
namespace App\Http\Controllers\Api;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Config;
class TaskboardDataController extends Controller
{
private static function getKeywordAccessToken()
{
Log::info('In LinkApiController->getAddUrlAccessToken1()');
try{
Log::info('SD_KEYWORDS_MS_OAUTH_TOKEN_URL . SD_KEYWORDS_MS_OAUTH_TOKEN_URL: ' . Config::get('app.SD_KEYWORDS_MS_BASE_URL') . Config::get('app.SD_KEYWORDS_MS_OAUTH_TOKEN_URL'));
Log::info('SD_KEYWORDS_MS_GRAND_TYPE: ' . Config::get('app.SD_KEYWORDS_MS_GRAND_TYPE'));
Log::info('SD_KEYWORDS_MS_CLIENT_ID: ' . Config::get('app.SD_KEYWORDS_MS_CLIENT_ID'));
Log::info('SD_KEYWORDS_MS_SECRET: ' . Config::get('app.SD_KEYWORDS_MS_SECRET'));
Log::info('Getting the token!');
$http = new Client(); //GuzzleHttp\Client
$response = $http->post(
Config::get('app.SD_KEYWORDS_MS_BASE_URL') . Config::get('app.SD_KEYWORDS_MS_OAUTH_TOKEN_URL'),
[
'form_params' => [
'grant_type' => Config::get('app.SD_KEYWORDS_MS_GRAND_TYPE'),
'client_id' => Config::get('app.SD_KEYWORDS_MS_CLIENT_ID'),
'client_secret' => Config::get('app.SD_KEYWORDS_MS_SECRET'),
'redirect_uri' => '',
],
]
);
$array = $response->getBody()->getContents();
$json = json_decode($array, true);
$collection = collect($json);
$access_token = $collection->get('access_token');
Log::info('Got the token!');
return $access_token;
} catch(RequestException $e){
Log::info('There is some exception in LinkApiController->getAddUrlAccessToken()');
return $e->getResponse()->getStatusCode() . ': ' . $e->getMessage();
}
}
//get Pagerank function
public function geturl_all($webranking_data)
{
Log::info('keyword finding'.$webranking_data);
try{
Log::info('SD_ADD_URL_MS_KEYWORD_DELETE_ALL_URL: ' . Config::get('app.SD_ADD_URL_MS_KEYWORD_DELETE_ALL_URL'));
$access_token = $this->getAddUrlAccessToken();
$url = ''
.Config::get('app.SD_KEYWORDS_MS_BASE_URL')
.Config::get('app.SD_ADD_URL_MS_KEYWORD_DELETE_ALL_URL')
.'/'
.$webranking_data;
Log::info('Got the access token from DelelteWebrankingController::WebrankAccessToken().Fetching Webrank!');
Log::info('ALL Webrank URL: ' . $url);
$guzzleClient = new Client(); //GuzzleHttp\Client
$params = [
'headers' =>[
'Accept' => 'application/json',
'Authorization' => 'Bearer ' .$access_token
]
];
$response = $guzzleClient->request('GET', $url, $params);
Log::info('Got the Response from WB Keyword MS');
$json = json_decode($response->getBody()->getContents(), true);
// Log::info('Number of objects in response: ' . count($json['data']));
return $json;
} catch(\Exception $e){
Log::info('There was some exception in DelelteWebrankingController->getWeb_ranking()');
return $e->getResponse()->getStatusCode(). ':' . $e->getMessage();
}
}
}
Next go to .env and put below code
SD_TASK_BOARD_MS_BASE_URL=http://wz-tasks-board-ms
SD_TASK_BOARD_MS_OAUTH_TOKEN_URL=/oauth/token
SD_TASK_BOARD_MS_GRAND_TYPE=client_credentials
SD_TASK_BOARD_MS_CLIENT_ID=3
SD_TASK_BOARD_MS_SECRET=GliKecl41nzcMtrj5Tf8dX8FHVYlkGbWiTsDZR97
SD_TASK_BOARD_MS_ALL_URL=/api/v1/j/taskboard
SD_TASK_BOARD_MS_KEYWORDTO_TASK_BOARD_DELETE_ALL_URL=/api/v1/j/taskboard/keywordToTaskboardDelete
SD_TASKBOARD_MS_KEYWORD_DELETE_ALL_URL=/api/v1/j/taskboard/delete_data
Next put below code in controller
public function geturl_all_task($webranking_data)
{
Log::info('abhi url me in kiye hai');
$getalltask_borad = TaskBoard::where('project_id', $webranking_data)->orwhere('admin_id', $webranking_data)->first();
$valid_tasktypes = $getalltask_borad['project_id'];
$valid_slug_ids = $getalltask_borad['slug_id'];
$valid_admin_ids = $getalltask_borad['admin_id'];
$checking_taskboad = TaskBoard::where('project_id', $valid_tasktypes)->where('slug_id', $valid_slug_ids)->where('admin_id', $valid_admin_ids)->first();
if ($checking_taskboad) {
$response = [
'success' => true,
'data' => $checking_taskboad,
'message' => 'Data Found',
];
Log::info('we are using this project value in website ranking so cannot be delete this vlaue');
} else {
$response = [
'success' => false,
'data' => $checking_taskboad,
'message' => 'No data found',
];
Log::info('we didnt get value from project thats why task type will be delete');
}
return response()->json($response, 200);
}
Now all setup has been done now you can get the data.