2017-04-04 55 views
0

webhook向我發送一些數據給我提供的URl。我正試圖抓住這些數據。這是我使用的代碼: -解析來自webhooks發送的數據的數據

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/[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" 
} 

現在,我需要獲得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數據?

+0

有沒有'$ json_decode_data'變量在碼。有'$ data'變量。但是,'$ data'是'StdClass'的一個實例,但是您正試圖在代碼中使用數組。有兩個錯誤 - 1)使用不存在的變量和2)嘗試使用不存在的變量作爲數組。 – Mjh

回答

1

json_decode()默認會產生一個StdClass。如果你想要一個數組,添加一個真實的參數。然後引用時使用正確的變量,是$數據:

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

要獲得href的數據,你需要做這樣的事情

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