将哪些数据发布到 shippo webhook?
Posted
技术标签:
【中文标题】将哪些数据发布到 shippo webhook?【英文标题】:What data is POSTed to a shippo webhook? 【发布时间】:2017-08-30 23:53:17 【问题描述】:我想实现一个shippo webhook来了解我的货物的交付状态,他们的documentation有点不清楚......我不知道什么信息会传递给我的脚本
我已经设置了一个测试 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');
如您所见,我应该能够捕获一些传入的数据,但我得到的只是空数组..
【问题讨论】:
如何处理响应?如果您插入来自 requestb.in 的链接,您应该能够看到运行测试 webhook 示例时响应的样子。我唯一一次遇到与 PHP 类似的情况是因为它第二次尝试对响应进行 json_encode() 处理。 【参考方案1】:确实,我的脚本直接接收 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] =>
)
【讨论】:
【参考方案2】:根据他们的文档,他们只是向您发送一个直接的 JSON 响应,而不是您可以从请求参数中获取的键/值对数据。你会想做这样的事情:
$data = json_decode($request->getContent(), true);
本文档来自 Silex,但它使用与 Symfony 相同的组件来接收accept a JSON request body。
【讨论】:
以上是关于将哪些数据发布到 shippo webhook?的主要内容,如果未能解决你的问题,请参考以下文章
通过 Braintree + Braintree Webhooks 从 PayPal Payments Pro(w/h 定期计费)+ IPN 迁移到 PayPal Payments