2017-04-04 4 views
0

Ein Webhook schickt mir einige Daten an die URl Ich habe zur Verfügung gestellt. Ich versuche, die Daten zu erfassen. Dies ist der Code ich verwende: -Parse Daten von Daten gesendet von Webhooks

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

Die Daten, die in der TXT-Datei gespeichert wurde, ist dies: -

{ 
    "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" 
} 

Nun, ich brauche die links->customer->href Daten erhalten Wie kann ich Dies?

Ich versuche dies: -

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

Nichts in der txt-Datei geschrieben zu werden. Wie kann ich die href-Daten analysieren?

+0

Es gibt keinen '$ json_decode_data' Variable in Ihrem tun Code. Es gibt '$ data' Variable. Aber '$ data' ist eine Instanz von' StdClass', obwohl Sie versuchen, ein Array in Ihrem Code zu verwenden. Es gibt zwei Fehler - 1.) Verwenden nicht vorhandener Variablen und 2) Versuchen, nicht vorhandene Variable als Array zu verwenden. – Mjh

Antwort

1

json_decode() erzeugt standardmäßig eine StdClass. Wenn Sie ein Array möchten, fügen Sie einen wahren Parameter hinzu. Dann die richtige Variable verwenden, $ Daten sein, wenn es Referenzierung:

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

Um Daten der href zu erhalten, müssen Sie so etwas wie diese

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