Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have read a number of Stack Overflow questions regarding this, but I still can't figure it out. I am getting the below JSON from a webhook from a service I do not control. I read the data into a PHP variable like so:

$postData = file_get_contents('php://input');

I output the $postData to a logfile to see the contents of the POST request, and it is as follows:

{
  "v": 1,
  "matches_filters": {
    "current": [
      1,
      10
    ]
  },
  "meta": {
    "v": 1,
    "action": "updated",
    "object": "deal",
    "id": 8918,
    "company_id": 84041,
    "user_id": 114674,
    "host": "stonecreekinsurance.pipedrive.com",
    "timestamp": 1446087218,
    "permitted_user_ids": [
      114674,
      112820,
      113033,
      117102,
      121983,
      121984,
      123453,
      123454,
      154367,
      163389,
      163390,
      171541,
      234921,
      323973,
      342136,
      440511,
      524205,
      592218,
      592222,
      640371,
      804841
    ],
    "trans_pending": false,
    "is_bulk_update": false,
    "matches_filters": {
      "current": [
        1,
        10
      ]
    }
  },
  "retry": 0,
  "current": {
    "id": 8918,
    "user_id": 114674,
    "person_id": 7406,
    "org_id": null,
    "stage_id": 7,
    "title": "test CallIn-EQ",
    "value": 0,
    "currency": "USD",
    "add_time": "2015-10-28 23:48:04",
    "update_time": "2015-10-29 02:53:38",
    "stage_change_time": "2015-10-29 02:53:38",
    "active": true,
    "deleted": false,
    "status": "open",
    "next_activity_date": null,
    "next_activity_time": null,
    "next_activity_id": null,
    "last_activity_id": null,
    "last_activity_date": null,
    "lost_reason": null,
    "visible_to": "3",
    "close_time": null,
    "pipeline_id": 1,
    "won_time": null,
    "lost_time": null,
    "products_count": null,
    "files_count": null,
    "notes_count": 1,
    "followers_count": 1,
    "email_messages_count": null,
    "activities_count": null,
    "done_activities_count": null,
    "undone_activities_count": null,
    "reference_activities_count": null,
    "participants_count": 1,
    "6edaf0488b13f9ab3090ce95e172e46224ef4742": null,
    "e10aff12a449abfa1ae4826c65d23b9adcf82d10": null,
    "41a5a8d9851c7aece2df16a7dfdbd3e2e8fcaac3": null,
    "41da351210adc097ebe0c1cda00f3781f1b4855b": null,
    "9d7875cfc9caa3e9b2aeef85f76eef2fdd65ebcd": "CallIn-EQ.com",
    "expected_close_date": null,
    "309c6f2061511f371c8f3036de6236b6f9ae3d5c": "9",
    "20527ff9d907372c0df49f84caaa9a27ffe2fd0a": "(925)297-1234",
    "17418ab9392d5820ce58de9efd8b03b428e4b389": "1234 Main St, Lafayette, CA 94549",
    "17418ab9392d5820ce58de9efd8b03b428e4b389_lat": 37.8962173,
    "17418ab9392d5820ce58de9efd8b03b428e4b389_long": -122.0599365,
    "17418ab9392d5820ce58de9efd8b03b428e4b389_subpremise": "",
    "17418ab9392d5820ce58de9efd8b03b428e4b389_street_number": "1234",
    "17418ab9392d5820ce58de9efd8b03b428e4b389_route": "South Main Street",
    "17418ab9392d5820ce58de9efd8b03b428e4b389_sublocality": "",
    "17418ab9392d5820ce58de9efd8b03b428e4b389_locality": "Walnut Creek",
    "17418ab9392d5820ce58de9efd8b03b428e4b389_admin_area_level_1": "California",
    "17418ab9392d5820ce58de9efd8b03b428e4b389_admin_area_level_2": "Contra Costa County",
    "17418ab9392d5820ce58de9efd8b03b428e4b389_country": "United States",
    "17418ab9392d5820ce58de9efd8b03b428e4b389_postal_code": "94596",
    "17418ab9392d5820ce58de9efd8b03b428e4b389_formatted_address": "1234 S Main St, Walnut Creek, CA 94596, USA",
    "3eb9ff628e565bf9062b47344d64b71a28aefdae": null,
    "046e70b421ff4227c2fe4330f446f7b6f25bd165": null,
    "173d3e7db13eebd4c38b0a4044ce91e2af018553": null,
    "327fd7256c8d9f2c4040c1c8b162a1fd4498875f": "12",
    "fce5121c7c9a121fbcc181935b8c87d57de9540c": null,
    "69768f3830c572afb53961d16be388b6b1f7716b": null,
    "b843b47ebd27941af18d352d5b42f1550f04cf14": null,
    "stage_order_nr": 2,
    "person_name": "Test test",
    "org_name": null,
    "next_activity_subject": null,
    "next_activity_type": null,
    "next_activity_duration": null,
    "next_activity_note": null,
    "formatted_value": "$0",
    "weighted_value": 0,
    "formatted_weighted_value": "$0",
    "rotten_time": "2015-11-08 02:53:38",
    "owner_name": "Tom Lynch",
    "cc_email": "censored",
    "org_hidden": false,
    "person_hidden": false
  },
  "previous": {
    "id": 8918,
    "user_id": 114674,
    "person_id": 7406,
    "org_id": null,
    "stage_id": 6,
    "title": "test CallIn-EQ",
    "value": 0,
    "currency": "USD",
    "add_time": "2015-10-28 23:48:04",
    "update_time": "2015-10-29 02:45:36",
    "stage_change_time": "2015-10-29 02:45:36",
    "active": true,
    "deleted": false,
    "status": "open",
    "next_activity_date": null,
    "next_activity_time": null,
    "next_activity_id": null,
    "last_activity_id": null,
    "last_activity_date": null,
    "lost_reason": null,
    "visible_to": "3",
    "close_time": null,
    "pipeline_id": 1,
    "won_time": null,
    "lost_time": null,
    "products_count": null,
    "files_count": null,
    "notes_count": 1,
    "followers_count": 1,
    "email_messages_count": null,
    "activities_count": null,
    "done_activities_count": null,
    "undone_activities_count": null,
    "reference_activities_count": null,
    "participants_count": 1,
    "6edaf0488b13f9ab3090ce95e172e46224ef4742": null,
    "e10aff12a449abfa1ae4826c65d23b9adcf82d10": null,
    "41a5a8d9851c7aece2df16a7dfdbd3e2e8fcaac3": null,
    "41da351210adc097ebe0c1cda00f3781f1b4855b": null,
    "9d7875cfc9caa3e9b2aeef85f76eef2fdd65ebcd": "CallIn-EQ.com",
    "expected_close_date": null,
    "309c6f2061511f371c8f3036de6236b6f9ae3d5c": "9",
    "20527ff9d907372c0df49f84caaa9a27ffe2fd0a": "(925)297-1234",
    "17418ab9392d5820ce58de9efd8b03b428e4b389": "1234 Main St, Lafayette, CA 94549",
    "17418ab9392d5820ce58de9efd8b03b428e4b389_lat": 37.8962173,
    "17418ab9392d5820ce58de9efd8b03b428e4b389_long": -122.0599365,
    "17418ab9392d5820ce58de9efd8b03b428e4b389_subpremise": "",
    "17418ab9392d5820ce58de9efd8b03b428e4b389_street_number": "1234",
    "17418ab9392d5820ce58de9efd8b03b428e4b389_route": "South Main Street",
    "17418ab9392d5820ce58de9efd8b03b428e4b389_sublocality": "",
    "17418ab9392d5820ce58de9efd8b03b428e4b389_locality": "Walnut Creek",
    "17418ab9392d5820ce58de9efd8b03b428e4b389_admin_area_level_1": "California",
    "17418ab9392d5820ce58de9efd8b03b428e4b389_admin_area_level_2": "Contra Costa County",
    "17418ab9392d5820ce58de9efd8b03b428e4b389_country": "United States",
    "17418ab9392d5820ce58de9efd8b03b428e4b389_postal_code": "94596",
    "17418ab9392d5820ce58de9efd8b03b428e4b389_formatted_address": "1234 S Main St, Walnut Creek, CA 94596, USA",
    "3eb9ff628e565bf9062b47344d64b71a28aefdae": null,
    "046e70b421ff4227c2fe4330f446f7b6f25bd165": null,
    "173d3e7db13eebd4c38b0a4044ce91e2af018553": null,
    "327fd7256c8d9f2c4040c1c8b162a1fd4498875f": "12",
    "fce5121c7c9a121fbcc181935b8c87d57de9540c": null,
    "69768f3830c572afb53961d16be388b6b1f7716b": null,
    "b843b47ebd27941af18d352d5b42f1550f04cf14": null,
    "stage_order_nr": 1,
    "person_name": "Test test",
    "org_name": null,
    "next_activity_subject": null,
    "next_activity_type": null,
    "next_activity_duration": null,
    "next_activity_note": null,
    "formatted_value": "$0",
    "weighted_value": 0,
    "formatted_weighted_value": "$0",
    "rotten_time": "2015-11-08 02:45:36",
    "owner_name": "Tom Lynch",
    "cc_email": "censored",
    "org_hidden": false,
    "person_hidden": false
  },
  "event": "updated.deal"
}

I ran the above JSON through jsonlint.com and it comes up as valid. I tried using a substr($postData,3) as suggested in some other SO questions to get rid of a BOM encoding on a string, but that just cut off the first 3 characters of my JSON string.

var_dump(json_decode($postData,true) ); just returns blank. I can see that an Array is created by outputting json_decode($postData,true) to a variable and then logging that, but it is completely empty. json_last_error() returns 0, and no warning or error shows up in the php error_log.

I am at a loss as to why the JSON string is not properly getting into an array with json_decode... and why json_last_error() would be 0 and the array still be empty... Any help would be appreciated.

share|improve this question
1  
3v4l.org/08C8h – ɹɐqʞɐ zoɹǝɟ Oct 29 '15 at 3:27
    
Try this public function __decode_data() { $raw_data = fopen('php://input','r'); $json_data = fgets($raw_data); $decoded = json_decode($json_data, true); return $decoded; } – 0yeoj Oct 29 '15 at 3:31
    
What version of PHP are you running on your server? – Kirk Logan Oct 29 '15 at 3:55
    
@ɹɐqʞɐ zoɹǝɟ, I understand that it works like that, when I copy/paste the string from my log output to a string variable it works fine. My issue is that something is not working when I read the data into the variable (I log the variable to the log file, not the input so I am making sure I know what the contents of the variable is) and then pass the variable to json_decode, or use json_decode on the input itself. This made me think it was an encoding issue, but the BOM fix I've read elsewhere was also not working. – Sleepybear Oct 29 '15 at 19:01
    
@KirkLogan Our server has PHP Version 5.4.45 – Sleepybear Oct 29 '15 at 19:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.