【问题标题】:Using guzzle to send post request to laravel based apps使用 guzzle 向基于 laravel 的应用程序发送 post 请求
【发布时间】:2017-12-25 19:34:51
【问题描述】:

我的本​​地主机中有 2 个 laravel 应用程序(用于测试目的)...我希望我的第一个 laravel 应用程序将 POST 数据发送到我的第二个 laravel 应用程序,然后将这些数据保存到 mysql 数据库。

这是我的第一个 laravel 应用路线

Route::get('post_request', function()
{  

    $client = new \GuzzleHttp\Client();
    $url = "http://bkculv5.local/api_post";

    $response = $client->post($url,[
        'json'=>[
            'judul' => 'title','content' => 'konten'
        ]]);

});

这是我的第二条 laravel 应用路线

Route::post('/api_post', function(Request $request){
    $judul = $request->judul;
    $content = $request->content;

    $artikel = new App\Artikel;
    $artikel->judul = $judul;
    $artikel->content = $content;

    $artikel->save();
});

我得到的是

服务器错误:POST http://bkculv5.local/api_post 导致 500 Internal Server Error 响应:

当我将myfirstapplaravel.local/api_post 放入浏览器时

知道为什么吗?还是我需要像在 ajax 中使用 post 一样使用 X-CSRF-TOKEN?

更新

这是我在 apache xampp 中的错误日志

[Thu Jul 20 15:26:35.201109 2017] [core:error] [pid 1500:tid 604] (EAI 11002)APR does not understand this error code: AH00549: Failed to resolve server name for 36.86.63.180 (check DNS) -- or specify an explicit ServerName
[Thu Jul 20 15:26:35.439807 2017] [ssl:warn] [pid 1500:tid 604] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Thu Jul 20 15:26:35.487845 2017] [mpm_winnt:notice] [pid 1500:tid 604] AH00354: Child: Starting 150 worker threads.

【问题讨论】:

标签: php mysql laravel guzzle


【解决方案1】:

在你的 Windows 主机文件中添加你的 Laravel 应用 IP,也不要忘记 CORS。希望有帮助。有时 https 可能会导致问题,但他们的库中总是有 API 可以跳过安全检查(仅用于开发目的)。

$client = new Guzzle\Http\Client();
$client->setDefaultOption('verify', '/path/to/cacert.pem');

或者,如果您需要禁用 SSL 验证:

$client->setDefaultOption('verify', false);

【讨论】:

  • 是的,我已经将 IP 放入我的主机文件“127.0.0.1 bkculv5.local”
  • 尝试 CORS 并跳过 https 安全检查。当我在 Laravel 中使用 guzzle 时,我也遇到了这个错误。
  • 我解决了 https 问题:github.com/guzzle/guzzle/issues/394
猜你喜欢
  • 2020-07-14
  • 2018-03-21
  • 2020-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-18
  • 2019-10-07
  • 2013-03-09
相关资源
最近更新 更多