-1

I have a large JSON object from Stripe that I am trying to get the customer field data.

the data appears to be unserialized, $result[16]; will access the 17th character space and not element 16 (just trying to get arbitrary output). I tried putting json_decode in a few places, that would block the data from hitting my variable.

I am thinking var_export trashed the encoding, but I am unsure of how to restore it to a workable format.

code:

$request = file_get_contents('php://input');
$array = json_decode($request,true);
$result2 = $array;
$file = 'stripedump.txt';
$current = file_get_contents($file);
$current .= $result2;
file_put_contents($file, $current);

JSON Object:

{
  "created": 1326853478,
  "livemode": false,
  "id": "evt_00000000000000",
  "type": "invoice.created",
  "object": "event",
  "request": null,
  "pending_webhooks": 1,
  "api_version": "2013-02-13",
  "data": {
    "object": {
      "date": 1436647384,
      "id": "in_00000000000000",
      "period_start": 1436647384,
      "period_end": 1436647384,
      "lines": {
        "data": [
          {
            "id": "sub_6agAX0pPxijkpq",
            "object": "line_item",
            "type": "subscription",
            "livemode": true,
            "amount": 0,
            "currency": "usd",
            "proration": false,
            "period": {
              "start": 1436647384,
              "end": 1439239384
            },
            "subscription": null,
            "quantity": 1,
            "plan": {
              "interval": "month",
              "name": "plan1",
              "created": 1435896032,
              "amount": 749,
              "currency": "usd",
              "id": "1",
              "object": "plan",
              "livemode": false,
              "interval_count": 1,
              "trial_period_days": 30,
              "metadata": {
              },
              "statement_descriptor": "Ex-Mailer.com",
              "statement_description": "Ex-Mailer.com"
            },
            "description": null,
            "discountable": true,
            "metadata": {
            }
          }
        ],
        "total_count": 1,
        "object": "list",
        "url": "/v1/invoices/in_6agAuGmTHHa8Qi/lines"
      },
      "subtotal": 0,
      "total": 0,
      "customer": "cus_00000000000000",
      "object": "invoice",
      "attempted": false,
      "closed": false,
      "forgiven": false,
      "paid": true,
      "livemode": false,
      "attempt_count": 0,
      "amount_due": 0,
      "currency": "usd",
      "starting_balance": 0,
      "ending_balance": null,
      "next_payment_attempt": 1436650984,
      "webhooks_delivered_at": null,
      "charge": null,
      "discount": null,
      "application_fee": null,
      "subscription": null,
      "tax_percent": null,
      "tax": null,
      "metadata": {
      },
      "statement_descriptor": null,
      "description": null,
      "receipt_number": null,
      "statement_description": null
    }
  }
}

command used to initiate request:

gentoo-mini htdocs # curl -i -X POST -H "Content-Type: application/json" -d "{    "created": 1326853478,    "livemode": false,    "id": "evt_00000000000000",    "type": "invoice.created",    "object": "event",    "request": null,    "pending_webhooks": 1,    "api_version": "2013-02-13",    "data": {      "object": {        "date": 1436647384,        "id": "in_00000000000000",        "period_start": 1436647384,        "period_end": 1436647384,        "lines": {          "data": [            {              "id": "sub_6agAX0pPxijkpq",              "object": "line_item",              "type": "subscription",              "livemode": true,              "amount": 0,              "currency": "usd",              "proration": false,              "period": {                "start": 1436647384,                "end": 1439239384              },              "subscription": null,              "quantity": 1,              "plan": {                "interval": "month",                "name": "plan1",                "created": 1435896032,                "amount": 749,                "currency": "usd",                "id": "1",                "object": "plan",                "livemode": false,                "interval_count": 1,                "trial_period_days": 30,                "metadata": {                },                "statement_descriptor": "Ex-Mailer.com",                "statement_description": "Ex-Mailer.com"              },              "description": null,              "discountable": true,              "metadata": {              }            }          ],          "total_count": 1,          "object": "list",          "url": "/v1/invoices/in_6agAuGmTHHa8Qi/lines"        },        "subtotal": 0,        "total": 0,        "customer": "cus_00000000000000",        "object": "invoice",        "attempted": false,        "closed": false,        "forgiven": false,        "paid": true,        "livemode": false,        "attempt_count": 0,        "amount_due": 0,        "currency": "usd",        "starting_balance": 0,        "ending_balance": null,        "next_payment_attempt": 1436650984,        "webhooks_delivered_at": null,        "charge": null,        "discount": null,        "application_fee": null,        "subscription": null,        "tax_percent": null,        "tax": null,        "metadata": {        },        "statement_descriptor": null,        "description": null,        "receipt_number": null,        "statement_description": null      }    }  }" http://nyctelecomm.com/hooker/
HTTP/1.1 100 Continue

HTTP/1.1 200 OK
Date: Sat, 11 Jul 2015 23:33:46 GMT
Server: Apache/2.4.12 (FreeBSD) PHP/5.6.10
X-Powered-By: PHP/5.6.10
Content-Length: 0
Content-Type: text/html; charset=UTF-8

How do I access the 'customer:' element in this JSON object?

NIX
  • 107
  • 3
  • 14

1 Answers1

3

Have you tried returning an associative array from the json string?

$array = json_decode($request,true); // return assoc array
echo $array["data"]["object"]["customer"];

var_export is much the same as var_dump, both functions create a representation of a given variable. In this case, there is no need to use var_export.

PHP: var_export

PHP: json_decode

manta
  • 1,663
  • 12
  • 12
  • I cannot use print_r to see anything, but '$result2 = $array["data"]["object"]["customer"];' fails to populate the customer info. the data is now blocked because of JSON decode http://paste.ee/p/PoUsk – NIX Jul 11 '15 at 22:27
  • commented out unneeded code http://paste.ee/p/pGy5K you dont need `var_export` – manta Jul 11 '15 at 22:36
  • still empty http://paste.ee/p/a0nYe . If I change $result2= $array it's still empty. json_decode appears to block the data. – NIX Jul 11 '15 at 22:49
  • Added `json_last_error` http://paste.ee/p/92BXv please check the variable `$request` is used in the `json_decode` function. – manta Jul 11 '15 at 23:00
  • echo anything won't work. stand by while I figure out how to write it to the log file. – NIX Jul 11 '15 at 23:26
  • there is no error on the page. I can't get any error output into the dumpfile. As soon as I add json_decode into the equation (with or without var_export), data stops completely. this is my present code and I should be getting the entire JSON object. Nothing http://paste.ee/p/hrcEL – NIX Jul 11 '15 at 23:46
  • #1 my json was broke. #2 $result = utf8_encode($request); put into the mix dumped the data. it works now. – NIX Jul 12 '15 at 00:23