Exception when serializing to XML (but not to JSON) - php

I'm using Symfony 3.3 serializer. This code:
$serialized = $this->serializer->serialize($input, "json");
returns:
{
"title": "dsd",
"description": null,
"source": null,
"tags": null,
"objectId": null,
"attributes": {
"sdffds": "sdffsdfsd",
"fsfdsfd": "sdfsdfsdf"
},
"features": [],
"lifecycleStart": null,
"lifecycleEnd": null,
"attachments": {
"20170625194534-595012dee26c8": {
"original": "favicon (1).ico",
"filename": "favicon (1).ico",
"size": "318",
"title": null,
"description": null,
"reader": null
}
}
}
Unforutnately, running
$serialized = $this->serializer->serialize($input, "xml");
will throw an exception:
Uncaught PHP Exception DOMException: "Invalid Character Error" at /www/site/ui/vendor/symfony/symfony/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php line 454
I'm pretty sure I didn't include any funny character. Do you have any idea whether I'm doing anything wrong or could it be a symfony bug?

It's probably because of 20170625194534-595012dee26c8. XML element name cannot start with number. See: The Naming of Parts article on XML.com.

Related

Stripe, Retrieve Card Info from Event

This was working for me on one account and today I added my listener to a new Stripe account and now it is no longer working. When a customer adds a new payment method to their account, the event customer.source.created is fired. Here is my code that WAS working (this is an excerpt):
<?php
if($event->type == 'customer.source.created') {
$cardID = $event_json->data->object->id;
$customerID = $event_json->data->object->customer;
$brand = $event_json->data->object->brand;
$last4 = $event_json->data->object->last4;
$exp_month = $event_json->data->object->exp_month;
$exp_year = $event_json->data->object->exp_year;
}
?>
Here is a sample Stripe JSON response:
{
"id": "evt_1DLkAh2Y54K5YG39Lku1hJ1G",
"object": "event",
"api_version": "2018-09-24",
"created": 1539664123,
"data": {
"object": {
"id": "card_1DLkAf2Y54K5YG39C0jMpCOj",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "JCB",
"country": "JP",
"customer": "cus_DnCcf39Nqp4RwF",
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 12,
"exp_year": 2024,
"fingerprint": "t56gZmsPg6ztkHFH",
"funding": "credit",
"last4": "0000",
"metadata": {
},
"name": null,
"tokenization_method": null
}
},
"livemode": false,
"pending_webhooks": 2,
"request": {
"id": "req_L9pvt2RR3VSZoG",
"idempotency_key": null
},
"type": "customer.source.created"
}
The Stripe response code is 200. I have other events in my listener like charge.succeeded that are working just fine with pretty much the same coding methods. Right now, I can't get the $cardID to echo. So I'm wondering if Stripe changed something recently. I am running the latest version in Webhooks. I did try to revert to an older version but that didn't work either.
Ok this was a very stupid issue I didn't catch. The stripe account that I set up today was in an older version and there was an update available. I didn't catch that because I was working in test mode and you can only upgrade in live mode.
So, after upgrading, everything works perfectly. Hours of wasted time and something so simple.

Parse data from data sent by webhooks

A webhook is sending me some data to the URl I have provided. I am trying to catch the data. This is the code I am using:-
if ($this->input->server('REQUEST_METHOD') == 'POST')
{
file_put_contents('test.txt', file_get_contents('php://input'));
......................
}
The data which got saved in the txt file is this:-
{
"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"
}
Now, I need to get the links->customer->href data
How can I get this?
I tried this:-
$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']);
Nothing getting written in the txt file. How can I parse the href data?
json_decode() will produce a StdClass by default. If you want an array, add a true parameter. Then use the correct variable, being $data, when referencing it:
$data = json_decode(file_get_contents('php://input'), true);
To get data of the href, you need to do something like this
<?php
$data = json_decode(file_get_contents('test.txt'));
echo '<pre>';
print_r($data->links->customer->href);
?>

Displaying nested json in laravel 5.2

I have a nested json file
{
"event_type": "INCOMING_BTC",
"event_uid": "5515c5601f7b3",
"datetime": "2015-03-27 21:02:37",
"resources": [
{
"resource_type": "transaction",
"resource": {
"id": 105062,
"datetime": "2015-03-27 21:02:23",
"description": "Money from Xapo Tip",
"order_type": "payment_received",
"from": {
"type": "btc_address",
"id": "1AqF787aPHgPRZ81kdQSeEwW46yjyrAaxR"
},
"to": {
"destination_type": "btc_address",
"destination_id": "1N65Bz88zKUDPKhUUsx8f9Qwsuo96Hqz7S",
"to_account": 1276
},
"generic_type": "credit",
"amount": "0.0000001",
"currency": "BTC",
"status": "completed",
"txConfidence": 1,
"rejection_reason": null,
"notes": null,
"base_currency": "USD",
"exchange_rate": null,
"exchange_amount": null
}
},
{
"resource_type": "address",
"resource": {
"id": "1N65Bz88zKUDPKhUUsx8f9Qwsuo96Hqz7S",
"address": "1N65Bz88zKUDPKhUUsx8f9Qwsuo96Hqz7S",
"meta_data": null,
"label": null,
"total_received": "0.00000350",
"created_at": "2015-03-08 14:50:59",
"address_type": "multisig",
"id_account": "1276"
}
}
]
}
And I have saved this file in public folder with name xapojson.txt
In my routes file I have done json_decode to decode this data to a variable 'transaction' and passed it to view
Route::get('/', function () {
$transaction = json_decode(file_get_contents('xapojson.txt'));
return view('transaction', compact('transaction'));
});
Now in transaction view I have to display the data from this nested json.
I have tried out lots of variation and searched on google and stackoverflow but nothing worked.
I found this somewhat helpful Check this out. But it also do not get into more nesting.
In the view I have to display these datas:-
resources[0]->resource->from->type
resources[0]->resource->id
resources[0]->resource->from->id
resources[0]->resource->status
resources[0]->resource->amount
resources[0]->resource->currency
resources[0]->resource->to->destination_id
datetime
Please help me on displaying the above fields.
Try using
$transaction = json_decode(file_get_contents('xapojson.txt'), true);
Well when I made this code work in simple php, I extracted the data by
$xapo_json = file_get_contents('xapojson.txt');
$xapo_json_decoded = json_decode($xapo_json);
$from_type = $xapo_json_decoded->resources[0]->resource->from->type;
$transacid = $xapo_json_decoded->resources[0]->resource->id;
$from = $xapo_json_decoded->resources[0]->resource->from->id;
$status = $xapo_json_decoded->resources[0]->resource->status;
$amount = $xapo_json_decoded->resources[0]->resource->amount;
$currency = $xapo_json_decoded->resources[0]->resource->currency;
$to = $xapo_json_decoded->resources[0]->resource->to->destination_id;
$datetime = $xapo_json_decoded->datetime;
Then in the routes file i changed
return view('transaction', compact('transaction'));
to
return View::make('transaction')->with('transaction',$transaction);
And in view I used this
{{ $transaction->resources[0]->resource->from->id }}
Voila, It works
you can do it with the below code ( am taking div as example you can change it to table or whatever
#foreach($transaction->resources as $resource)
#if(is_set($resource->from))
<div>{{$resource->from->type}}</div>
#endif
<div>{{$resource->id}}</div>
#if(is_set($resource->from))
<div>{{$resource->from->id}}</div>
#endif
<div>{{$resource->status}}</div>
<div>{{$resource->amount}}</div>
<div>{{$resource->currency}}</div>
<div>{{$resource->to->destination_id}}</div>
#endforeach

Can't get username of last follower from array

Trying to get the name of the latest Twitch-follower with help from a snipper I found online. However I get an error message which I can't understand
$json_array2 = json_decode(file_get_contents('https://api.twitch.tv/kraken/channels/'.strtolower($channelName).'/follows?limit=1'), true);
$latestFollower = $json_array2['follows']['user']['name'];
Typing in the URL in my broser I get this so I'm thinking follows->user->name should be right but I guess not :(
{
"follows": [
{
"created_at": "2014-07-09T23:30:59Z",
"_links": {
"self": "https://api.twitch.tv/kraken/users/username17376/follows/channels/sodapoppin"
},
"user": {
"_id": 65845277,
"name": "username17376",
"created_at": "2014-07-08T03:59:16Z",
"updated_at": "2014-07-08T04:14:14Z",
"_links": {
"self": "https://api.twitch.tv/kraken/users/username17376"
},
"display_name": "Username17376",
"logo": null,
"bio": null,
"type": "user"
}
}
],
"_total": 339627,
"_links": {
"self": "https://api.twitch.tv/kraken/channels/sodapoppin/follows?direction=DESC&limit=1&offset=0",
"next": "https://api.twitch.tv/kraken/channels/sodapoppin/follows?direction=DESC&limit=1&offset=1"
}
}
Should be
$json_array2['follows'][0]['user']['name'];
Based on your JSON
{
"follows": [
{
Which shows that follows is an array. The first object in the the array (index 0) is an object that has the property "user"

How do I turn a POSTed JSON value into PHP?

As a test, this JSON data is being POSTed to my website:
{
"order": {
"id": null,
"created_at": null,
"status": "new",
"total_btc": {
"cents": 100000000,
"currency_iso": "BTC"
},
"total_native": {
"cents": 2263,
"currency_iso": "USD"
},
"custom": "123456789",
"button": {
"type": "buy_now",
"name": "Test Item",
"description": null,
"id": null
},
"transaction": {
"hash": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
"confirmations": 0
}
}
}
Since it is being sent server-to-server I cannot visibly see the data. I have tried sending the $_POST array to a text file, but it comes up blank. What I think I need to do is:
$data = json_decode($jsonData);
But how do I set the variable $jsonData?
You can try to use wrappers for reading raw POST query.
$data = file_get_contents("php://input");
Have you tried this, store the sting obtained to a variable and then decoding it?
$postedJsonData= '{"order":{"id":null,"created_at":null,"status":"new","total_btc":
{"cents":100000000,"currency_iso":"BTC"},"total_native":
{"cents":2263,"currency_iso":"USD"},"custom":"123456789","button":
{"type":"buy_now","name":"Test Item","description":null,"id":null},"transaction":
{"hash":"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b","confirmations":0}}}';
var_dump(json_decode($postedJsonData, true));
The true parameter would return an associative array
$data = json_decode($jsonData, True);

Categories