Laravel 10虚拟主机POST请求遇cURL Error 7问题求助
Laravel虚拟主机环境下cURL error 7连接远程端口5000问题求助
问题概述
Laravel应用部署到虚拟主机后,向远程服务器http://**.**.***.***:5000发起POST请求时触发cURL error 7,本地开发环境及Postman测试均正常,仅虚拟主机环境下无法连接。
目标
让运行在虚拟主机的Laravel应用能够成功向远程服务器http://**.**.***.***:5000发送POST请求。
环境详情
- Laravel版本:10
- 虚拟主机:
http://subdomain.domain.com
控制器代码
public function send(Request $request) { // Validate the request data (you can add more validation rules as needed) $this->validate($request, [ 'message_session' => 'required', 'whatsapp_user_id' => 'required', 'sender_id' => 'required', 'messageTime' => 'required', 'phone' => 'required', ]); try { // Start a database transaction DB::beginTransaction(); // Create a new WhatsappMessages instance and populate it with the request data $whatsappMessage = new WhatsappMessages(); $whatsappMessage->message_session = $request->input('message_session'); $whatsappMessage->whatsapp_user_id = $request->input('whatsapp_user_id'); $whatsappMessage->sender_id = $request->input('sender_id'); $whatsappMessage->messageTime = $request->input('messageTime'); $whatsappMessage->messageText = $request->input('message'); if (auth()->user()->id == $request->input('sender_id')) { // Define the URL of the endpoint $url = env('MESSAGE_API_URL') . '/send-message'; // Prepare the data to send in the request $data = [ 'to' => $request->input('phone'), 'message' => $request->input('message'), ]; // Make a POST request to the endpoint $response = Http::post($url, $data); // Check if the request was successful (status code 200) if ($response['status']==200) { // Save the instance to the database $whatsappMessage->save(); // Commit the database transaction DB::commit(); // Return a JSON response indicating that the message was sent return response()->json(['message' => 'Message sent successfully']); } else { // The request to the external service was not successful // Roll back the database transaction DB::rollBack(); // Return a JSON response indicating the failure to send the message return response()->json(['message' => 'Failed to send message to WhatsApp'], 500); } } else { // Save the instance to the database $whatsappMessage->save(); // Commit the database transaction DB::commit(); // Return a JSON response indicating that the message was received return response()->json(['message' => 'Message received successfully']); } } catch (\Exception $e) { // An exception occurred // Roll back the database transaction DB::rollBack(); // Log the exception for debugging Log::error($e); // Return a JSON response indicating an error return response()->json(['message' => 'An error occurred while processing the request'], 500); } }
问题细节
当代码中存在$response = Http::post($url, $data);这一行时,虚拟主机环境下立即报错;移除该行后,代码运行完全正常。
错误信息
cURL error 7: Failed to connect to **.**.***.*** port 5000 after 1003 ms: Couldn't connect to server (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://**.**.***.***:5000/send-message
已尝试的排查步骤
- 确认远程服务器
http://**.**.***.***:5000配置正确,Postman及本地控制器均可正常访问该接口,仅虚拟主机环境无法建立连接。
请求帮助
怀疑问题与虚拟主机的网络配置或端口限制有关,恳请有类似cURL连接问题排查经验的朋友提供指导和解决方案。
内容的提问来源于stack exchange,提问作者Fahad Ishan
相关产品推荐
相关产品推荐

