验证mailchimp的URL的GET状态

问题描述

我已经创建了一个函数,该函数在mailchimp中创建一个批处理webhook,以便在批处理操作完成后将提交回调。如下所示:

// register mailchimp batch webhook
    $responseWH = wp_remote_post( 'https://' . substr($mcApi,strpos($mcApi,'-')+1) . '.api.mailchimp.com/3.0/batch-webhooks',array(
        'headers' => array(
        'method' => 'POST','Authorization' => 'Basic ' . base64_encode( 'user:'. $mcApi )
        ),'body' => json_encode(array(
                'url' => 'http://90660d72b8be.ngrok.io/wp-json/lubuvna/v2/batch/2810471250791421617231098394447326550803'
        ))

));

以上代码表示,当批处理操作完成时,MC应调用以下网址: http://90660d72b8be.ngrok.io/wp-json/lubuvna/v2/batch/2810471250791421617231098394447326550803

如果调用此url,则脚本将运行并通知我该操作已完成,并更新了wordpress中的帖子。

但是由于标头状态代码为404,mailchimp并未将URL添加到批处理Webhook中。相反,我收到以下错误消息:

{
    "type": "http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title": "Invalid Resource","status": 400,"detail": "The resource submitted Could not be validated. For field-specific details,see the 'errors' array.","instance": "05a4430f-c68f-46a5-82af-3b95332d6fe83","errors": [
        {
            "field": "url","message": "We Couldn't verify the URL is working. Please double check and try again. HTTP Code: 500"
        }
    ]
}

当Mailchimp尝试向指定的URL发出GET请求,同时添加批处理webhook时,如何在标头中返回200状态?他们需要一个有效的网址。 mailchimp batch webhook

我正在使用ngrok进行本地主机内的连接,当我添加http://90660d72b8be.ngrok.io/wp-content/plugins/my-plugin/inc/batch/import.PHP之类的URL时可以正常工作,因为该文件实际上存在于插件目录中

但是在添加URL http://90660d72b8be.ngrok.io/wp-json/lubuvna/v2/batch/2810471250791421617231098394447326550803

我可以看到mailchimp发出了获取请求,并且代码状态为404,如屏幕截图所示

enter image description here

解决方法

我以以下方式告终:

function myplugin_registered_rest_routes() {

    // batch operation finished
    register_rest_route('myplugin/v2','/batch/(?P<id>\d+)',array(
            array('methods' => ['POST','GET'],'callback' => 'my_awesome_func'
            )
        ) 
    );
}

add_action( 'rest_api_init','myplugin_registered_rest_routes' );

另一个数组选项,例如@ 04FS答案:

register_rest_route('myplugin/v2',array(
            array('methods' => 'GET','callback' => 'my_awesome_func',),array('methods' => 'POST','callback' => 'my_awesome_func'
            )
        ) 
    );

解析URL请求的另一个选项。使用wordpress API时不常见:

add_action('parse_request','register_endpoint',0);

function register_endpoint() {
    global $wp;

    if ($wp->request == 'wp-json/myplugin/v2/batch/2810471250791421617231098394447326550803') {
        header("HTTP/1.1 200 OK");
        exit;
    }
}