【发布时间】:2017-03-05 13:43:05
【问题描述】:
如何在 Laravel 控制器中从 VBA-Excel 捕获 HTTP Post 以便更新模型?我正在 Homestead 上运行我的 laravel 应用程序。下面是我迄今为止尝试过但没有成功的演示。
VBA-EXCEL
Sub httpPost()
Dim XMLHTTP As Object
Dim result As String
Dim argumentString As String
argumentString = "screen_name=ET"
Set XMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
XMLHTTP.Open "POST", _
"http://exceltwitterbot.app:8000/request", False
XMLHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
XMLHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
XMLHTTP.send argumentString
MsgBox XMLHTTP.responsetext
Set XMLHTTP = Nothing
End Sub
Web.php
Route::post('/request', [
'uses' => 'ExcelController@store',
'as' => 'request.store',
]);
ExcelController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ExcelController extends Controller
{
public function store(Request $request)
{
$name = $request->only('screen_name');
$excel = new Excelinput;
$excel->screen_name = $name;
$excel->save();
}
}
这是我在 excel 中的 MSGBOX 中看到的……似乎不对……
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="robots" content="noindex,nofollow" />
<style>
/* Copyright (c) 2010, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: http://developer.yahoo.com/yui/license.html */
html{color:#000;background:#FFF;}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td{margin:0;padding:0;}table{border-collapse:collapse;border-spacing:0;}fieldset,img{border:0;}address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:normal;}li{list-style:none;}caption,th{text-align:left;}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal;}q:before,q:after{content:'';}abbr,acronym{border:0;font-variant:normal;}sup{vertical-align:text-top;}sub{vertical-align:text-bottom;}input,textarea,select{font-family:inherit;font-size:inherit;font-weight:inherit;}input,textarea,select{*font-size:100%;}legend{color:#000;}
html { backg
如果我将 VBA 中的 URL 切换为“http://httpbin.org/post”,我会收到以下响应。这是否意味着我的应用程序没有正确接收 POST?如何正确配置 Laravel 以便它可以接受来自 VBA 的 HTTP POST?
{
"args": {},
"data": "",
"files": {},
"form": {
"screen_name": "ET"
},
"headers": {
"Accept": "*/*",
"Accept-Language": "en-US,en;q=0.5",
"Content-Length": "14",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
},
"json": null,
"origin": "24.36.19.160",
"url": "http://httpbin.org/post"
}
【问题讨论】:
-
在你的控制器中,试试这个,看看你会得到什么:
dd($request->all());。我猜数据没有像你预期的那样发布。 -
“没有成功”——究竟发生了什么或没有发生什么?
-
您还有一个错字,可能对您没有任何帮助,缺少报价:
$name = $request->only('screen_name'); -
你通过服务器认证了吗?
-
当我说没有成功时,我的意思是我的数据库没有更新。我不相信 dd 在这种情况下有效,因为 HTTP 请求来自 VBA(我可能会误会,因为我不确定如何调用它)。
标签: vba laravel xmlhttprequest homestead