3

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

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

每当通过实时或测试 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');
    }


}

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

4

2 回答 2

3

事实上,我的脚本直接接收 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] =>
)
于 2017-08-31T01:43:22.337 回答
2

根据他们的文档,他们只是向您发送直接的 JSON 响应,而不是您可以从请求参数中获取的键/值对数据。你会想做这样的事情:

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

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

于 2017-08-31T01:33:12.650 回答