Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Grav CMS中向外部webapi发送PHP帖子?_Php_Grav - Fatal编程技术网

如何在Grav CMS中向外部webapi发送PHP帖子?

如何在Grav CMS中向外部webapi发送PHP帖子?,php,grav,Php,Grav,我对Grav CMS真的是个新手,我正在尝试找出向外部webapi发送post请求以传递表单数据的最佳方法 通常情况下,我会在表单提交后执行PHP代码,并向webapi发出post请求,阅读这里的一个问题,即应该使用插件分离所有自定义PHP逻辑 我应该使用插件向外部webapi发送表单post请求吗? 我只是想确保我使用插件的方向是正确的。你可以为此构建一个插件。下面是一个快速示例代码,您可以将表单发布到示例页面,在本例中,示例页面是yoursite.com/my form route <

我对Grav CMS真的是个新手,我正在尝试找出向外部webapi发送post请求以传递表单数据的最佳方法

通常情况下,我会在表单提交后执行PHP代码,并向webapi发出post请求,阅读这里的一个问题,即应该使用插件分离所有自定义PHP逻辑

我应该使用插件向外部webapi发送表单post请求吗?


我只是想确保我使用插件的方向是正确的。

你可以为此构建一个插件。下面是一个快速示例代码,您可以将表单发布到示例页面,在本例中,示例页面是
yoursite.com/my form route

<?php
namespace Grav\Plugin;

use \Grav\Common\Plugin;

class MyAPIPlugin extends Plugin
{
    public static function getSubscribedEvents()
    {
        return [
            'onPluginsInitialized' => ['onPluginsInitialized', 0]
        ];
    }

    public function onPluginsInitialized()
    {
        if ($this->isAdmin())
            return;

        $this->enable([
            'onPageInitialized' => ['onPageInitialized', 0],
        ]);
    }

    public function onPageInitialized()
    {
        // This route should be set in the plugin's setting instead of hard-code here.
        $myFormRoute = 'my-form-route';

        $page = $this->grav['page'];
        $currentPageRoute = $page->route();

        // This is not the page containing my form. Skip and render the page as normal.
        if ($myFormRoute != $currentPageRoute)
            return;

        // This is page containing my form, check if there is submitted data in $_POST and send it to external API.
        if (!isset($_POST['my_form']))
            return;

        // Send $_POST['my_form'] to external API here.
    }
}

这是一个老问题,但创建插件确实解决了我的问题,谢谢。