首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >shippo webhook的POSTed是什么数据?

shippo webhook的POSTed是什么数据?
EN

Stack Overflow用户
提问于 2017-08-31 07:53:17
回答 2查看 678关注 0票数 3

我想实现一个shippo网络钩子,以便了解我的货件的交付状态,他们的documentation有点不清楚…我不知道哪些信息将被传递给我的脚本

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

每当通过实时或测试URL请求我的脚本时,我得到的都是空数组,没有数据。请帮我弄清楚这件事。有没有从希波来的??

这是我到目前为止所知道的:

代码语言:javascript
复制
<?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');
    }


}

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

EN

回答 2

Stack Overflow用户

发布于 2017-08-31 09:43:22

事实上,我的脚本直接收到了JSON,感谢mootrichard分享了requestb.in工具,有了它,我能够看到所有的头文件和发送的数据,只是为了将来参考这是我得到的。

代码语言:javascript
复制
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的内容是:

代码语言:javascript
复制
    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] =>
)
票数 3
EN

Stack Overflow用户

发布于 2017-08-31 09:33:13

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

代码语言:javascript
复制
$data = json_decode($request->getContent(), true);

本文档来自Silex,但它使用与Symfony相同的组件来接收accept a JSON request body

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45971304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档