Php 正在尝试为到另一台服务器的API调用构建API路由和控制器。新手拉维尔·奎斯顿
我正在尝试将一些站点迁移到Laravel 8(只是学习Laravel)。我对Laravel有一个非常基本的了解,但有其他开发经验 这是我的composer.json的一部分,所以我安装了jetstream、sanctum、tinker和livewire,我有一些用户、登录、注册、忘记密码、团队等。这些设置似乎工作正常。我必须创建一个指向存储目录的符号链接,才能显示配置文件图像Php 正在尝试为到另一台服务器的API调用构建API路由和控制器。新手拉维尔·奎斯顿,php,json,ajax,laravel,api,Php,Json,Ajax,Laravel,Api,我正在尝试将一些站点迁移到Laravel 8(只是学习Laravel)。我对Laravel有一个非常基本的了解,但有其他开发经验 这是我的composer.json的一部分,所以我安装了jetstream、sanctum、tinker和livewire,我有一些用户、登录、注册、忘记密码、团队等。这些设置似乎工作正常。我必须创建一个指向存储目录的符号链接,才能显示配置文件图像 "license": "MIT", "require": {
"license": "MIT",
"require": {
"php": "^7.3",
"fideloper/proxy": "^4.2",
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.0.1",
"laravel/framework": "^8.0",
"laravel/jetstream": "^1.1",
"laravel/sanctum": "^2.6",
"laravel/tinker": "^2.0",
"league/oauth2-google": "^3.0",
"livewire/livewire": "^2.0",
"phpmailer/phpmailer": "^6.1"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.5",
"facade/ignition": "^2.3.6",
"fzaninotto/faker": "^1.9.1",
"mockery/mockery": "^1.3.1",
"nunomaduro/collision": "^5.0",
"phpunit/phpunit": "^9.3"
},
. . . . .
我有一些来自另一个项目的后端PHP代码,我需要将它们合并到Laravel框架中。该代码从前端页面(现在是Laravel.blade视图的一部分)获取一个AJAX请求,并返回一个JSON响应,我将使用该响应在现在的blade视图上重新呈现一些内容
对我的backend.php页面的请求(现在来自.blade视图)是JSON(它发布JSON):
后端会进行一些检查,以根据某些会话参数编辑调用,因此我需要访问Laravel中现在的用户表来检查这些列,我已经向用户添加了这些列,然后该页面会向本地主机上运行的另一个服务器发送CURL调用(如果服务器位于同一位置)或者在反向代理(NGINX)后面,这就是为什么在这种情况下我要传递头文件和源文件
我的后端PHP页面看起来是这样的,它从另一台服务器返回一个JSON响应,并将其传递到最初进行AJAX调用的页面,所以有点像两个跳跃
function executeCURLPOSTJSON($JSONQuery) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:8042/studies/page');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $JSONQuery);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$headers = array();
// $headers[] = 'Authorization:Bearer CURLTOKEN';
// $headers[] = 'Token:wxwzisme';
// $headers[] = 'Origin:https://www.sias.dev';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
$result = '{"error":"' . curl_strerror(curl_errno($ch)). '"}';
}
curl_close($ch);
return $result;
}
session_start();
$JSONQuery = json_decode(file_get_contents('php://input'));
// if (!empty($_SESSION['PatientID'])) $JSONQuery->Query->PatientID = $_SESSION['PatientID'];
// if (!empty($_SESSION['ReferringPhysicianName'])) $JSONQuery->Query->ReferringPhysicianName = '*' . $_SESSION['ReferringPhysicianName'] . '*';
$JSONQuery = json_encode($JSONQuery);
echo executeCURLPOSTJSON($JSONQuery);
因此,总结一下:
<?php
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
Route::middleware(['auth:sanctum', 'verified'])->get('/studies', function () {
$user = Auth::user();
Debugbar::error($user);
Debugbar::error($user->patientid);
return view('studies');
})->name('studies');
Route::middleware(['auth:sanctum', 'verified'])->post('api/getstudies', function (Request $request) {
$query = json_decode($request->getContent());
$user = Auth::user();
if (!empty($user->patientid)) $JSONQuery->Query->PatientID = $user->patientid;
if (!empty($user->doctor_id)) $JSONQuery->Query->ReferringPhysicianName = '*' . $user->doctor_id .'*';
$user = Auth::user();
Debugbar::error($user);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:8042/studies/page');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request->getContent());
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$headers = array();
// $headers[] = 'Authorization:Bearer CURLTOKEN';
// $headers[] = 'Token:wxwzisme';
// $headers[] = 'Origin:https://www.sias.dev';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
$result = '{"error":"' . curl_strerror(curl_errno($ch)). '"}';
}
curl_close($ch);
return $result;
//return view('studies');
})->name('studies');
<?php
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
Route::middleware(['auth:sanctum', 'verified'])->get('/studies', function () {
$user = Auth::user();
Debugbar::error($user);
Debugbar::error($user->patientid);
return view('studies');
})->name('studies');
Route::middleware(['auth:sanctum', 'verified'])->post('api/getstudies', function (Request $request) {
$query = json_decode($request->getContent());
$user = Auth::user();
if (!empty($user->patientid)) $JSONQuery->Query->PatientID = $user->patientid;
if (!empty($user->doctor_id)) $JSONQuery->Query->ReferringPhysicianName = '*' . $user->doctor_id .'*';
$user = Auth::user();
Debugbar::error($user);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:8042/studies/page');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request->getContent());
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$headers = array();
// $headers[] = 'Authorization:Bearer CURLTOKEN';
// $headers[] = 'Token:wxwzisme';
// $headers[] = 'Origin:https://www.sias.dev';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
$result = '{"error":"' . curl_strerror(curl_errno($ch)). '"}';
}
curl_close($ch);
return $result;
//return view('studies');
})->name('studies');