将 Laravel 应用程序与短信发送器 Twilio 结合使用。我将使用以下 LoginController 将电话号码插入表中
public function submit(Request $request)
{
$request->validate([
'phone' => 'required|numeric|min:10'
]);
$user = User::firstOrCreate([
'phone' => $request->phone
]);
if(!$user) {
return response()->json(['message' => 'Could not process a user with that Phone number.'], 401);
}
$user->notify(new LoginNeedsVerification());
return response()->json(['message' => 'Text message notification sent.']);
}
我的 api.php 是
Route::post('/login', [LoginController::class, 'submit']);
并且我正在使用 httpie 命令提示符,当我尝试使用 httpie 命令时
http POST http://localhost/api/login phone=7714523658
并且 cmd 收到以下错误消息,但电话号码插入到表格中
HTTP/1.1 500 Internal Server Error
Access-Control-Allow-Origin: *
Cache-Control: no-cache, private
Connection: close
Content-Type: application/json
Date: Mon, 08 Jan 2024 06:00:14 GMT
Date: Mon, 08 Jan 2024 06:00:14 GMT
Host: localhost:8000
X-Powered-By: PHP/8.2.0
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 58
{
"exception": "Twilio\Exceptions\EnvironmentException",
"file": "F:\2024\ride share app\backend\vendor\twilio\sdk\src\Twilio\Http\CurlClient.php",
"line": 41,
"message": "SSL certificate problem: unable to get local issuer certificate",
"trace": [
{
"class": "Twilio\Http\CurlClient",
"file": "F:\2024\ride share app\backend\vendor\twilio\sdk\src\Twilio\Rest\Client.php",
"function": "request",
"line": 265,
"type": "->"
},
{
"class": "Twilio\Rest\Client",
"file": "F:\2024\ride share app\backend\vendor\twilio\sdk\src\Twilio\Domain.php",
"function": "request",
"line": 63,
"type": "->"
},
{
"class": "Twilio\Domain",
"file": "F:\2024\ride share app\backend\vendor\twilio\sdk\src\Twilio\Version.php",
"function": "request",
"line": 56,
"type": "->"
},
{
"class": "Twilio\Version",
"file": "F:\2024\ride share app\backend\vendor\twilio\sdk\src\Twilio\Version.php",
"function": "request",
"line": 211,
"type": "->"
},
{
"class": "Twilio\Version",
"file": "F:\2024\ride share app\backend\vendor\twilio\sdk\src\Twilio\Rest\Api\V2010\Account\MessageList.php",
"function": "create",
"line": 72,
"type": "->"
},
{
"class": "Twilio\Rest\Api\V2010\Account\MessageList",
"file": "F:\2024\ride share app\backend\vendor\laravel-notification-channels\twilio\src\Twilio.php",
"function": "create",
"line": 107,
"type": "->"
},
{
"class": "NotificationChannels\Twilio\Twilio",
"file": "F:\2024\ride share app\backend\vendor\laravel-notification-channels\twilio\src\Twilio.php",
"function": "sendSmsMessage",
"line": 43,
"type": "->"
},
{
"class": "NotificationChannels\Twilio\Twilio",
"file": "F:\2024\ride share app\backend\vendor\laravel-notification-channels\twilio\src\TwilioChannel.php",
"function": "sendMessage",
"line": 59,
"type": "->"
},
{
"class": "NotificationChannels\Twilio\TwilioChannel",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Notifications\NotificationSender.php",
"function": "send",
"line": 148,
"type": "->"
},
{
"class": "Illuminate\Notifications\NotificationSender",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Notifications\NotificationSender.php",
"function": "sendToNotifiable",
"line": 106,
"type": "->"
},
{
"class": "Illuminate\Notifications\NotificationSender",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Support\Traits\Localizable.php",
"function": "Illuminate\Notifications\{closure}",
"line": 19,
"type": "->"
},
{
"class": "Illuminate\Notifications\NotificationSender",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Notifications\NotificationSender.php",
"function": "withLocale",
"line": 101,
"type": "->"
},
{
"class": "Illuminate\Notifications\NotificationSender",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Notifications\NotificationSender.php",
"function": "sendNow",
"line": 79,
"type": "->"
},
{
"class": "Illuminate\Notifications\NotificationSender",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Notifications\ChannelManager.php",
"function": "send",
"line": 39,
"type": "->"
},
{
"class": "Illuminate\Notifications\ChannelManager",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Notifications\RoutesNotifications.php",
"function": "send",
"line": 18,
"type": "->"
},
{
"class": "App\Models\User",
"file": "F:\2024\ride share app\backend\app\Http\Controllers\LoginController.php",
"function": "notify",
"line": 26,
"type": "->"
},
{
"class": "App\Http\Controllers\LoginController",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Routing\Controller.php",
"function": "submit",
"line": 54,
"type": "->"
},
{
"class": "Illuminate\Routing\Controller",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php",
"function": "callAction",
"line": 43,
"type": "->"
},
{
"class": "Illuminate\Routing\ControllerDispatcher",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Routing\Route.php",
"function": "dispatch",
"line": 259,
"type": "->"
},
{
"class": "Illuminate\Routing\Route",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Routing\Route.php",
"function": "runController",
"line": 205,
"type": "->"
},
{
"class": "Illuminate\Routing\Route",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Routing\Router.php",
"function": "run",
"line": 806,
"type": "->"
},
{
"class": "Illuminate\Routing\Router",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"function": "Illuminate\Routing\{closure}",
"line": 144,
"type": "->"
},
{
"class": "Illuminate\Pipeline\Pipeline",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Routing\Middleware\SubstituteBindings.php",
"function": "Illuminate\Pipeline\{closure}",
"line": 50,
"type": "->"
},
{
"class": "Illuminate\Routing\Middleware\SubstituteBindings",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"function": "handle",
"line": 183,
"type": "->"
},
{
"class": "Illuminate\Pipeline\Pipeline",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php",
"function": "Illuminate\Pipeline\{closure}",
"line": 159,
"type": "->"
},
{
"class": "Illuminate\Routing\Middleware\ThrottleRequests",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php",
"function": "handleRequest",
"line": 125,
"type": "->"
},
{
"class": "Illuminate\Routing\Middleware\ThrottleRequests",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php",
"function": "handleRequestUsingNamedLimiter",
"line": 87,
"type": "->"
},
{
"class": "Illuminate\Routing\Middleware\ThrottleRequests",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"function": "handle",
"line": 183,
"type": "->"
},
{
"class": "Illuminate\Pipeline\Pipeline",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"function": "Illuminate\Pipeline\{closure}",
"line": 119,
"type": "->"
},
{
"class": "Illuminate\Pipeline\Pipeline",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Routing\Router.php",
"function": "then",
"line": 805,
"type": "->"
},
{
"class": "Illuminate\Routing\Router",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Routing\Router.php",
"function": "runRouteWithinStack",
"line": 784,
"type": "->"
},
{
"class": "Illuminate\Routing\Router",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Routing\Router.php",
"function": "runRoute",
"line": 748,
"type": "->"
},
{
"class": "Illuminate\Routing\Router",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Routing\Router.php",
"function": "dispatchToRoute",
"line": 737,
"type": "->"
},
{
"class": "Illuminate\Routing\Router",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php",
"function": "dispatch",
"line": 200,
"type": "->"
},
{
"class": "Illuminate\Foundation\Http\Kernel",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"function": "Illuminate\Foundation\Http\{closure}",
"line": 144,
"type": "->"
},
{
"class": "Illuminate\Pipeline\Pipeline",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php",
"function": "Illuminate\Pipeline\{closure}",
"line": 21,
"type": "->"
},
{
"class": "Illuminate\Foundation\Http\Middleware\TransformsRequest",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull.php",
"function": "handle",
"line": 31,
"type": "->"
},
{
"class": "Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"function": "handle",
"line": 183,
"type": "->"
},
{
"class": "Illuminate\Pipeline\Pipeline",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php",
"function": "Illuminate\Pipeline\{closure}",
"line": 21,
"type": "->"
},
{
"class": "Illuminate\Foundation\Http\Middleware\TransformsRequest",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TrimStrings.php",
"function": "handle",
"line": 40,
"type": "->"
},
{
"class": "Illuminate\Foundation\Http\Middleware\TrimStrings",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"function": "handle",
"line": 183,
"type": "->"
},
{
"class": "Illuminate\Pipeline\Pipeline",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php",
"function": "Illuminate\Pipeline\{closure}",
"line": 27,
"type": "->"
},
{
"class": "Illuminate\Foundation\Http\Middleware\ValidatePostSize",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"function": "handle",
"line": 183,
"type": "->"
},
{
"class": "Illuminate\Pipeline\Pipeline",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance.php",
"function": "Illuminate\Pipeline\{closure}",
"line": 99,
"type": "->"
},
{
"class": "Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"function": "handle",
"line": 183,
"type": "->"
},
{
"class": "Illuminate\Pipeline\Pipeline",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Http\Middleware\HandleCors.php",
"function": "Illuminate\Pipeline\{closure}",
"line": 62,
"type": "->"
},
{
"class": "Illuminate\Http\Middleware\HandleCors",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"function": "handle",
"line": 183,
"type": "->"
},
{
"class": "Illuminate\Pipeline\Pipeline",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Http\Middleware\TrustProxies.php",
"function": "Illuminate\Pipeline\{closure}",
"line": 39,
"type": "->"
},
{
"class": "Illuminate\Http\Middleware\TrustProxies",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"function": "handle",
"line": 183,
"type": "->"
},
{
"class": "Illuminate\Pipeline\Pipeline",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"function": "Illuminate\Pipeline\{closure}",
"line": 119,
"type": "->"
},
{
"class": "Illuminate\Pipeline\Pipeline",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php",
"function": "then",
"line": 175,
"type": "->"
},
{
"class": "Illuminate\Foundation\Http\Kernel",
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php",
"function": "sendRequestThroughRouter",
"line": 144,
"type": "->"
},
{
"class": "Illuminate\Foundation\Http\Kernel",
"file": "F:\2024\ride share app\backend\public\index.php",
"function": "handle",
"line": 51,
"type": "->"
},
{
"file": "F:\2024\ride share app\backend\vendor\laravel\framework\src\Illuminate\Foundation\resources\server.php",
"function": "require_once",
"line": 16
}
]
}
如何解决这个问题?
您似乎收到了 404 Not Found 错误,这意味着您尝试访问的路由未定义或未找到。该问题可能与您发出 HTTP 请求的方式有关。
这是使用httpie发出POST请求的正确方法:
http POST localhost/api/login phone=772541258
确保您使用正确的端点 (/api/login) 并且您的服务器在本地主机上运行。另外,请确保您使用正确的方法 (POST)。
如果您仍然遇到问题,您可以检查以下内容:
web.php
或 api.php
文件中的路线定义以确保它
与您尝试访问的端点匹配。LoginController
类中没有拼写错误或语法错误。如果问题仍然存在,您可能需要检查 Laravel 日志
(storage/logs/laravel.log)
是否有任何其他错误消息,这些消息可以提供有关该问题的更多信息。
首先使用以下命令检查路线列表
php artisan route:list
如果您在列表中找不到您的路线,请清除路线缓存
php artisan cache:clear
php artisan route:cache
这是代码
use App\Notifications\LoginNeedsVerification;
use Illuminate\Http\Request;
use App\Models\User;
public function submit(Request $request)
{
$request->validate([
'phone' => 'required|numeric|min:10'
]);
$user = User::firstOrCreate([
'phone' => $request->phone
]);
if (!$user->wasRecentlyCreated) {
return response()->json(['message' => 'Could not process a user with that Phone number.'], 401);
}
$user->notify(new LoginNeedsVerification());
return response()->json(['message' => 'Text message notification sent.']);
现在尝试使用postman。它应该可以工作。最后你可以使用httpie。
希望你一切顺利。 您遇到的错误,HTTP 404 Not Found,表明您尝试访问的方向不存在或未在您的 Laravel 应用程序中描述。
在您的情况下,您使用的是后续的 HTTPie 命令:
http POST localhost/api/login phone=772541258
确保您从正确的目录运行命令,并且 Laravel 开发服务器已启动并正在运行。另外,请确保您为 API 端点使用正确的 URL。
如果您的 Laravel 开发服务器运行在
localhost
并且您使用的是默认的 Laravel 开发服务器端口 (8000
),则正确的 URL 应该是:
http POST http://localhost:8000/api/login phone=772541258
如果您使用不同的端口,请务必调整 URL。
此外,您可能想通过在 Laravel 项目目录中运行以下命令来检查您的路由是否已正确注册:
php artisan route:list
此命令将显示 Laravel 应用程序中所有已注册路由的列表,您可以检查您的
/api/login
路由是否已列出。
如果问题仍然存在,请检查您的 Laravel 路由并确保在
web.php
或 api.php
文件中定义该路由,具体取决于您的设置。如果您仍然遇到问题,则 Laravel 应用程序配置或服务器设置可能存在问题。