1

one of the application is sending a webhook callback as HTTP Post, from which I need to parse a JSON blob dump (custom_fields in the below URL). Here is one such POST from a server application:

http://localhost/webhook/?unit_price=0.00&currency=Free&buyer_name=Shivraj&payment_id=34565c17000F04251234&custom_fields=%7B%22Field_87630%22%3A+%7B%22value%22%3A+%22uname1%22%2C+%22label%22%3A+%22user_name%22%2C+%22required%22%3A+%22on%22%2C+%22type%22%3A+%22char%22%7D%2C+%22Field_63520%22%3A+%7B%22value%22%3A+%221234%22%2C+%22label%22%3A+%22auction_id%22%2C+%22required%22%3A+true%2C+%22type%22%3A+%22char%22%7D%2C+%22Field_76855%22%3A+%7B%22value%22%3A+%22tx1%22%2C+%22label%22%3A+%22tx_type%22%2C+%22required%22%3A+%22on%22%2C+%22type%22%3A+%22char%22%7D%7D&amount=10.00&fees=0.00&status=Credit&quantity=1

In my code:

$custom_fields= json_decode($_REQUEST['custom_fields'], true);
$f1 = $custom_fields['Field_63520']['value'];
$f2 = $custom_fields['Field_87630']['value'];
$f3 = $custom_fields['Field_76855']['value'];

I get error/warning,

PHP Warning:  Illegal string offset 'Field_63520' in webhook.php on line 70
PHP Warning:  Illegal string offset 'value' in webhook.php on line 70
...
f1:{
f2:{
f3:{

when I print the $custom_fields, I get:

{\"Field_87630\": {\"value\": \"uname1\", \"label\": \"user_name\", \"required\": \"on\", \"type\": \"char\"}, \"Field_63520\": {\"value\": \"1234\", \"label\": \"auction_id\", \"required\": true, \"type\": \"char\"}, \"Field_76855\": {\"value\": \"tx1\", \"label\": \"tx_type\", \"required\": \"on\", \"type\": \"char\"}}

I am kind of stuck here without knowing how to proceed. Any suggestions would help. Thanks.

1
  • please print your decoded array. Commented Dec 19, 2015 at 9:54

1 Answer 1

1

You have to remove backslash from your JSON data. Use stripslashes() function to remove backslash

Your code should be :

<?php
$custom_fields = json_decode(stripslashes($_REQUEST['custom_fields']),true);
$f1 = $custom_fields['Field_63520']['value'];
$f2 = $custom_fields['Field_87630']['value'];
$f3 = $custom_fields['Field_76855']['value'];
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.