AfterShip / aftership-sdk-php

The PHP SDK of AfterShip API
65 stars 46 forks source link

Need help with webhooks #18

Closed mandargiftease closed 8 years ago

mandargiftease commented 8 years ago

I have written a simple PHP program to dump all the request data received on webhook into the log file. However, I am unable to extract or see JSON data in the request. Here is the sample code that I am using:

`<?php require_once("../app/Mage.php"); Mage::app(); $array = $_POST; $msg = print_r($array, true);

$msg="Server: ".PHP_EOL.print_r($_SERVER,true).PHP_EOL."Request: ".PHP_EOL.print_r($_REQUEST, true).PHP_EOL."POST:".PHP_EOL.print_r($_POST,true).PHP_EOL."GET".PHP_EOL.print_r($_GET).PHP_EOL."Files:".print_r($_FILES);

Mage::log($msg, null, 'aftershiptrackingstatus.log', true); $data = file_get_contents('php://input'); var_dump($data); $msg="File Contents: ".PHP_EOL.print_r(json_decode($data, true)); Mage::log($msg, null, 'aftershiptrackingstatus.log', true); $msg="Raw Contents: ".PHP_EOL.print_r($HTTP_RAW_POST_DATA); Mage::log($msg, null, 'aftershiptrackingstatus.log', true); `

However in log file I am seeing only the headers and not the body part:

2016-02-03T14:17:13+00:00 DEBUG (7): Server: Array ( [USER] => www-data [HOME] => /var/www [FCGI_ROLE] => RESPONDER [SCRIPT_FILENAME] => /var/www/util/aftership.php [MAGE_RUN_CODE] => default [MAGE_RUN_TYPE] => store [QUERY_STRING] => [REQUEST_METHOD] => POST [CONTENT_TYPE] => application/json [CONTENT_LENGTH] => 5127 [SCRIPT_NAME] => /util/aftership.php [REQUEST_URI] => /util/aftership.php [DOCUMENT_URI] => /util/aftership.php [DOCUMENT_ROOT] => /var/www [SERVER_PROTOCOL] => HTTP/1.1 [GATEWAY_INTERFACE] => CGI/1.1 [SERVER_SOFTWARE] => nginx/1.4.6 [REMOTE_ADDR] => 52.5.72.61 [REMOTE_PORT] => 47611 [SERVER_ADDR] => 10.173.20.120 [SERVER_PORT] => 80 [SERVER_NAME] => corporategifts.giftease.com [HTTPS] => [REDIRECT_STATUS] => 200 [HTTP_HOST] => ftp.giftease.com [HTTP_ACCEPT] => application/json [HTTP_CONTENT_TYPE] => application/json [HTTP_CONTENT_LENGTH] => 5127 [HTTP_VIA] => 1.1 localhost (squid/3.1.10) [HTTP_X_FORWARDED_FOR] => 10.0.0.160 [HTTP_CACHE_CONTROL] => max-age=259200 [HTTP_CONNECTION] => keep-alive [PHP_SELF] => /util/aftership.php [REQUEST_TIME_FLOAT] => 1454509033.7609 [REQUEST_TIME] => 1454509033 )

Request: Array ( )

POST: Array ( )

GET 1 Files:1 2016-02-03T14:17:13+00:00 DEBUG (7): File Contents: 1 2016-02-03T14:17:13+00:00 DEBUG (7): Raw Contents: 1

Can you help me with extracting the json data? Do you have any sample code for me to checkout?

sunnychow commented 8 years ago

Thanks @mandargiftease We will work on this and reply you when we have an example.

sunnychow commented 8 years ago

We are sorry for the late reply. For solving your problem, you could use the following code to implement the webhook

<?php

// How-To:
// 1. Copy this file in your HTTP server DocumentRoot.
// 2. The webhook link maybe: http://www.example.com/webhook.php, paste in Aftership Dashboard->Settings->Webhook.
// 3. Set the trigger in Webhook setting page, like "In Transit", "Delivered". Aftership will make POST request based on your settings.
// 4. To test your webhook, just click the "Update" button in Webhook Setting page, aftership will send a dummy POST request to your webhook.

// Aftership will make a POST request to your webhook link with 'Content-Type: application/json' header,
// please use the "php://input" to get the post body, body is a JSON String.
// Reference: http://stackoverflow.com/questions/2731297/file-get-contentsphp-input-or-http-raw-post-data-which-one-is-better-to
// file_get_contents, need PHP 4 >= 4.3.0, PHP 5, PHP 7.
$inputJSON = file_get_contents("php://input");

// you will get a JOSN string as this api doc mentioned, https://www.aftership.com/docs/api/4/webhook
print_r($inputJSON);

// TO-DO: your logic, your code
sunnychow commented 8 years ago

You could find the webhook example in 5.0.7 version .