Saswat
Saswat

Reputation: 12846

Parse data from data sent by webhooks

A webhook is sending me some data to the URl I have provided. I am trying to catch the data. This is the code I am using:-

if ($this->input->server('REQUEST_METHOD') == 'POST')
{
    file_put_contents('test.txt', file_get_contents('php://input'));
    ......................
}

The data which got saved in the txt file is this:-

{
  "created_at": "2017-04-04 12:03:07 UTC",
  "href": "http://api.groovehq.com/v1/tickets/131",
  "links": {
    "customer": {
      "id": "0454984580",
      "href": "http://api.groovehq.com/v1/customers/[email protected]"
    },
    "drafts": {
      "href": "http://api.groovehq.com/v1/tickets/131/drafts"
    },
    "state": {
      "href": "http://api.groovehq.com/v1/tickets/131/state"
    },
    "messages": {
      "href": "http://api.groovehq.com/v1/tickets/131/messages"
    }
  },
  "number": 131,
  "priority": "low",
  "resolution_time": null,
  "state": "unread",
  "title": "gh",
  "updated_at": "2017-04-04 12:03:07 UTC",
  "system_updated_at": "2017-04-04 12:03:07 UTC",
  "assigned_group_id": null,
  "assigned_group": null,
  "closed_by": null,
  "tags": [

  ],
  "mailbox": "Inbox",
  "mailbox_id": "1923237790",
  "message_count": 1,
  "summary": "Complaint Date: 2017-4-22 Service Provider: Airtel Type of Complaint: Billing NCC need to do: Investigate and resolve the issue Complaint Details: Vb",
  "type": "API",
  "snoozed_until": null,
  "last_message": "Complaint Date: 2017-4-22<br />\nService Provider: Airtel<br />\nType of Complaint: Billing<br />\nNCC need to do: Investigate and resolve the issue<br />\nComplaint Details: Vb",
  "assignee": null,
  "app_url": "https://matrixdroid.groovehq.com/groove_client/tickets/44746020",
  "app_customer_url": "https://matrixdroid.groovehq.com/groove_client/contacts/customers/17295897",
  "customer_name": "[email protected]",
  "last_message_plain_text": "Complaint Date: 2017-4-22\nService Provider: Airtel\nType of Complaint: Billing\nNCC need to do: Investigate and resolve the issue\nComplaint Details: Vb"
}

Now, I need to get the links->customer->href data How can I get this?

I tried this:-

$json_data = file_get_contents('php://input');
$json_decode_data = json_decode($json_data);
file_put_contents('test.txt', $json_decode_data['links']['customer']['href']);

Nothing getting written in the txt file. How can I parse the href data?

Upvotes: 1

Views: 1597

Answers (2)

Mittul At TechnoBrave
Mittul At TechnoBrave

Reputation: 1242

To get data of the href, you need to do something like this

<?php 
$data = json_decode(file_get_contents('test.txt'));
echo '<pre>';
print_r($data->links->customer->href);
?>

Upvotes: 0

markdwhite
markdwhite

Reputation: 2449

json_decode() will produce a StdClass by default. If you want an array, add a true parameter. Then use the correct variable, being $data, when referencing it:

$data = json_decode(file_get_contents('php://input'), true);

Upvotes: 2

Related Questions