1

I am trying to take the POST fields and parse them without having to hard code the string elements (like find 'amount' find the next comma, pull out the value in between). The ....php code on the receiving end is using

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

I try to parse simple strings with JSON and they work fine, but once i add those extra { } sections digging down deeper, it starts to break. This isn't JSON, as the people who sent me the below code do not have content-type assigned. So is this format with the { } something i can easily parse with a command into an easy to read array? Or am I stuck hard-coding each string I'm looking for?

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://....php"
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_SSL_VERIFYPEER => true,
  CURLOPT_SSL_VERIFYHOST => 2,
  CURLOPT_CAINFO => (getcwd() . "/certs/cacert.pem"),
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => 
        "\n{\n       'event_meta_data': {\n           
    'event_type': 'transaction_card_sale',\n
       'version': '1.0',\n
   },\n
   'event_data': {\n
       'request': {\n
           'gateway_mid': '2122',\n
           'payment_channel': 'TBD',\n
           'amount': '1.80',\n
           'amount_cents': 180,\n
           'card_brand': 'MASTERCARD',\n
           'card_expiration': '1020',\n
           'card_last_four': '5100',\n 
          'card_name': 'foo bar',\n  
         'invoice_number': '123456',\n 
          'customer_id': '12349876',\n  
         ...More...  \n} \n} \n}",

));

(I know the { } may not line up, its not the whole section, but that should be enough to get started)

I've tried this with the values above being in single quotes ' and double quotes ". I feel like i've tried to internet search anything possibly related, but haven't come up with anything correct. Thanks.

On the receiving end:

 $data = file_get_contents('php://input');
 echo $data."<BR>";

prints:

{ 'event_meta_data': { 'event_type': 'transaction_card_sale', 
'version': '1.0', }, 'event_data': { 'request': { 'gateway_mid': 
'2122', 'payment_channel': 'TBD', 'amount': '1.80', 'amount_cents': 
180, 'card_brand': 'MASTERCARD', 'card_expiration': '1020', 
'card_last_four': '5100', 'card_name': 'foo bar', 'invoice_number': 
'123456', 'customer_id': '12349876', 'custom_fields': { 'AccountID': 
'', 'AccountIDList': '', 'AmountList': '', 'CorrespondenceUUID': '', 
'MerchantID': '', 'PaymentTakenBy': '', 'FacilityName': '', } }, 
'timestamp': '2016-08-05T00:15:31.769049', 'result': { 'result_code': 
'0', 'pn_ref': '12345', 'auth_code': 'VTLMC1', 'host_code': '12345', 
'host_url': '', 'message': 'APPROVAL VTLMC1', 'message1': '', 
'message2': '', 'resp_msg': 'Approved', 'get_avs_result': '0', 
'get_avs_result_txt': 'Issuer did not perform AVS', 
'get_commercial_card': 'False', 'get_cv_result': 'M', 
'get_cv_result_txt': 'Match', 'get_get_orig_result': '', 
'get_street_match_txt': 'Service Not Requested', 'get_zip_match_txt': 
'Service Not Requested', } } }
5
  • whatever the response is it is not valid json. where is this response generated? Commented Sep 15, 2016 at 13:28
  • I ended up writing a function to parse the values of the labels that i need, but i'd still like to know if there was an easier way. Or even what this type of POST is called, its not JSON, its not xml, so is it just a string with braces { }? Commented Sep 15, 2016 at 13:55
  • The user submits a Credit Card form at the 3rd party, they then will use the CURL/PHP code i showed above to call my ...php, with the POST values shown above. I need to parse those values and put it into my database so we can confirm the credit card payment happened. Commented Sep 15, 2016 at 13:58
  • what does this credit card company ~ 3rd party tell you about the data they return? Do they claim to return json? Commented Sep 15, 2016 at 14:30
  • I dont have a direct line of communication to their programmers, i'm going through one of the project manager type guys. I will ask, its looking more and more like they just took the array/json style and create a text string that works similarly to the eye, but not an official json object. I am parsing the values hardcoded like: $amount = parseString($data,"'amount'"); (where i wrote a parseString function) which works, was just trying to make sure there wasn't a knowledge gap regarding this curly braces { } style POST. Commented Sep 15, 2016 at 16:28

1 Answer 1

1
  1. Remove the comma in the last item of an JSON array, i found 3 occurences
  2. you can not use single qoutes for encapsulation, use double qoutes
  3. Use http://jsonlint.com to validate your JSON data

    'version': '1.0' ,

    'FacilityName': '',

    'get_zip_match_txt': 'Service Not Requested',

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I think the problem is its not valid JSON, and they're not telling me it should be JSON since their example doesn't say "context-type" = JSON. The JSON decode works for smaller examples, but once they start adding those extra braces { } sections, it breaks.
please checkout the revised awnser
Thanks. I think I understand how to convert the stuff to a valid JSON object, but since i've been told that string above is what they are going to send me, i dont have the option of having them fix it to a valid JSON object.

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.