从webhooks发送的数据中解析数据

时间:2017-04-04 12:20:39

标签: php webhooks file-put-contents

webhook正在向我发送一些数据给我提供的URl。我想抓住这些数据。这是我正在使用的代码: -

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

保存在txt文件中的数据是: -

{
  "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/ncc2017customer@gmail.com"
    },
    "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": "ncc2017customer@gmail.com",
  "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"
}

现在,我需要获取links->customer->href数据 我怎么能得到这个?

我试过了: -

$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']);

没有在txt文件中写入任何内容。我该如何解析href数据?

2 个答案:

答案 0 :(得分:2)

json_decode()默认会生成一个StdClass。如果需要数组,请添加true参数。然后在引用它时使用正确的变量$ data:

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

答案 1 :(得分:0)

要获取href的数据,您需要执行类似的操作

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