Reputation: 1262
I would like to implement a shippo webhook in order to know the delivery status of my shipments, their documentation is a little unclear... I don't know what information will be passed to my script
I have setup a test URL and a live one and have added those to my account, in API -> Webhooks.
Whenever my script is requested either via the live or test URLs I get empty arrays, no data. Please help me figure this out. Anyone from Shippo??
Here is what I have so far:
<?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('[email protected]')
->setTo('[email protected]')
->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');
}
}
As you can see I should be able to catch some incoming data but I get nothing but empty arrays..
Upvotes: 3
Views: 886
Reputation: 1262
Indeed my script is receiving straight up JSON, thank you to mootrichard for sharing the requestb.in tool, with it I was able to see all the headers and data sent, just for future reference this is what I got.
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');
}
}
And the contents of shippo.txt is:
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] =>
)
Upvotes: 3
Reputation: 8276
According to their documentation they're just sending you a straight JSON response, not a key/value pair of data that you can get from the request parameters. You would want to do something like this instead:
$data = json_decode($request->getContent(), true);
This documentation is from Silex but it's using the same components as Symfony to receive a accept a JSON request body.
Upvotes: 2