将HTTP Post从VBA-Excel发送到Laravel

时间:2017-03-03 20:21:29

标签: vba laravel xmlhttprequest homestead

如何在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中的网址切换为&#34; http://httpbin.org/post&#34;我收到以下回复。这是否意味着我的应用程序没有正确接收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"
}

1 个答案:

答案 0 :(得分:0)

这比预期的要简单得多......我的控制器中没有响应,我修改了接收$ request的变量。现在我在Excel中收到响应,我的数据库已更新!

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;    
use App\Services\Twitter\TwitterService as Twit;   
use App\Excelinput;
use App\Http\Requests;
use Illuminate\Http\Request;

class ExcelController extends Controller
{
    protected $twitter;

    public function __construct(Twit $t){
        $this->twitter = $t;
    }

    public function store(Request $request)
    {
      $twitterhandle = $request->screen_name;

      $excel = new Excelinput;
      $excel->screen_name = $twitterhandle;
      $excel->save();

      $usershow = $this->twitter->getUsersShow(null, $twitterhandle, 'false');

      return response()->json($usershow);
    }
}