繁体   English   中英

哪些数据已过帐到Shippo Webhook?

[英]What data is POSTed to a shippo webhook?

我想实现一个Shippo Webhook,以便了解我的货件的交付状态,他们的文档有点不清楚...我不知道什么信息会传递给我的脚本

我已经在API-> Webhooks中设置了一个测试URL和一个实时URL,并将其添加到我的帐户中。

每当通过实时或测试URL请求我的脚本时,我都会得到空数组,没有数据。 请帮我解决这个问题。 七宝的人吗?

这是我到目前为止的内容:

<?php

namespace MW\PublicBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;


class ShippoController extends Controller
{
    /**
     * @Route("/shippo/", name="shippo_web_hook")
     * @Method("GET|POST")
     */
    public function webHookAction(Request $request)
    {
        if ($request->getMethod() == 'POST'){
            $post = $request->request->all();
        } elseif ($request->getMethod() == 'GET'){
            $post = $request->query->all();
        }
        file_put_contents(__DIR__ . '/shippo.txt', print_r($post,true));

        $mailer = $this->get('swiftmailer.mailer.transactional');
        $messageObject = \Swift_Message::newInstance()
            ->setSubject('Shippo Webhook Posted DATA')
            ->setFrom('emai@example.com')
            ->setTo('email@example.com')
            ->setBody(print_r($post,true) . "\n" . print_r($_REQUEST,true) . "\n" . print_r($_POST,true));
        try {
            $mailer->send($messageObject);

        } catch (\Exception $e){

        }

        return new Response('OK');
    }


}

如您所见,我应该能够捕获一些传入的数据,但是除了空数组之外,我什么也没有。

确实,我的脚本正在直接接收JSON,感谢mootrichard共享了requestb.in工具,使用它我能够看到所有发送的标头和数据,仅供以后参考。

namespace MW\PublicBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;


class ShippoController extends Controller
{
    /**
     * @Route("/shippo/", name="shippo_web_hook")
     * @Method("GET|POST")
     */
    public function webHookAction(Request $request)
    {
        $headers = $request->headers->all();
        $content = $request->getContent();
        if (!empty($content))
        {
            $post = json_decode($content, true);
        }
        if (isset($headers['x-shippo-event'][0]) && $headers['x-shippo-event'][0] == 'track_updated' &&
            (isset($headers['content-type'][0]) && $headers['content-type'][0] == 'application/json')){

            if (count($post) > 0) {
                file_put_contents(__DIR__ . '/shippo.txt', print_r($headers, true) . "\n\n\n" . print_r($post, true));

            }


        }





        return new Response('OK');
    }


}

而shippo.txt的内容是:

    Array
(
    [host] => Array
        (
            [0] => ******
        )

    [user-agent] => Array
        (
            [0] => python-requests/2.9.1
        )

    [content-length] => Array
        (
            [0] => 1021
        )

    [accept] => Array
        (
            [0] => */*
        )

    [accept-encoding] => Array
        (
            [0] => gzip, deflate
        )

    [content-type] => Array
        (
            [0] => application/json
        )

    [shippo-api-version] => Array
        (
            [0] => 2014-02-11
        )

    [x-forwarded-for] => Array
        (
            [0] => **.**.***.**
        )

    [x-original-host] => Array
        (
            [0] => *****
        )

    [x-shippo-event] => Array
        (
            [0] => track_updated
        )

    [x-php-ob-level] => Array
        (
            [0] => 0
        )

)



Array
(
    [messages] => Array
        (
        )

    [carrier] => usps
    [tracking_number] => 123
    [address_from] => Array
        (
            [city] => Las Vegas
            [state] => NV
            [zip] => 89101
            [country] => US
        )

    [address_to] => Array
        (
            [city] => Spotsylvania
            [state] => VA
            [zip] => 22551
            [country] => US
        )

    [eta] => 2017-09-05T01:35:10.231
    [original_eta] => 2017-09-05T01:35:10.231
    [servicelevel] => Array
        (
            [token] => usps_priority
            [name] => Priority Mail
        )

    [metadata] => Shippo test webhook
    [tracking_status] => Array
        (
            [status] => UNKNOWN
            [object_created] => 2017-08-31T01:35:10.240
            [status_date] => 2017-08-31T01:35:10.240
            [object_id] => ac0e0c060d6e43b295c460414ebc831f
            [location] => Array
                (
                    [city] => Las Vegas
                    [state] => NV
                    [zip] => 89101
                    [country] => US
                )

            [status_details] => testing
        )

    [tracking_history] => Array
        (
            [0] => Array
                (
                    [status] => UNKNOWN
                    [object_created] => 2017-08-31T01:35:10.240
                    [status_date] => 2017-08-31T01:35:10.240
                    [object_id] => ac0e0c060d6e43b295c460414ebc831f
                    [location] => Array
                        (
                            [city] => Las Vegas
                            [state] => NV
                            [zip] => 89101
                            [country] => US
                        )

                    [status_details] => testing
                )

        )

    [transaction] =>
)

根据他们的文档,他们只是向您发送直接的JSON响应,而不是您可以从请求参数中获取的键/值对数据。 您可能想要执行以下操作:

$data = json_decode($request->getContent(), true);

该文档来自Silex,但是它使用与Symfony相同的组件来接收JSON请求主体

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM