3

I'm sending a string of data via CURL using data from post, and receiving a response in a string as well which is in the form on an array. I'm trying to convert such response into an actual Array. Heres the code I have so far:

the response string im getting is this:

Test transaction_id="2401_02-29-12_22:28:34_0" action="payment" result="success" to_from="to" amount="205.00" gross="205.00" net="-0.85" custom="" business="[email protected]" item_name="XXXXX.XXX" item_code="Product Name 3," quantity="1" transaction_type="authorized payment" transaction_status="pending" transaction_date="2012-02-29 22:28:34" processing_rate="0.85" discount_fee="0.00" first_name="TESTNAME" last_name="TESTLNAME" address="TESTADDRESS" city="TESTCITY" state_or_province="AL" country="US" zip_or_postal_code="12345" phone="1234562002" email="[email protected]" shipment="yes" shipping_address="TESTADDRESS" shipping_city="TESTCITY" shipping_state_or_province="AL" shipping_country="US" shipping_zip_or_postal_code="12345" processing_time="0.2187"

And the code im using to get such response is

<?php

// Get variables from POST array into a string
$post_str = "action=payment&business="      .urlencode($this->input->post('business'))
    ."&vericode="                   .urlencode($this->input->post('vericode'))
    ."&item_name="                  .urlencode($this->input->post('item_name'))
    ."&item_code="                  .urlencode($this->input->post('item_code'))
    ."&quantity="                   .urlencode($this->input->post('quantity'))
    ."&amount="                     .urlencode($this->input->post('amount'))
    ."&cc_type="                    .urlencode($this->input->post('cc_type'))
    ."&cc_number="                  .urlencode($this->input->post('cc_number'))
    ."&cc_expdate="                 .urlencode($this->input->post('cc_expdate_year')).urlencode($this->input->post('cc_expdate_month'))
    ."&cc_security_code="           .urlencode($this->input->post('cc_security_code'))
    ."&shipment="                   .urlencode($this->input->post('shipment'))
    ."&first_name="                 .urlencode($this->input->post('first_name'))
    ."&last_name="                  .urlencode($this->input->post('last_name'))
    ."&address="                    .urlencode($this->input->post('address'))
    ."&city="                       .urlencode($this->input->post('city'))
    ."&state_or_province="          .urlencode($this->input->post('state_or_province'))
    ."&zip_or_postal_code="         .urlencode($this->input->post('zip_or_postal_code'))
    ."&country="                    .urlencode($this->input->post('country'))
    ."&shipping_address="           .urlencode($this->input->post('shipping_address'))
    ."&shipping_city="              .urlencode($this->input->post('shipping_city'))
    ."&shipping_state_or_province=" .urlencode($this->input->post('shipping_state_or_province'))
    ."&shipping_zip_or_postal_code=".urlencode($this->input->post('shipping_zip_or_postal_code'))
    ."&shipping_country="           .urlencode($this->input->post('shipping_country'))
    ."&phone="                      .urlencode($this->input->post('phone'))
    ."&email="                      .urlencode($this->input->post('email'))
    ."&ip_address="                 .urlencode($this->input->post('ip_address'))
    ."&website_unique_id="          .urlencode($this->input->post('website_unique_id'));

// Send URL string via CURL
$backendUrl = "https://www.veripayment.com/integration/index.php";
$this->curl->create($backendUrl);
$this->curl->post($post_str);

// get response from API
$return_str = $this->curl->execute();
?>

1 Answer 1

1

If the response you're receiving has elements separated by a space you might use

function parse($response)
{
    $result = array();
    $resparray = explode(' ', $response);
    if ($resparray)
    {
      foreach ($resparray as $resp) {
        $keyvalue = explode('=', $resp);
        $result[$keyvalue[0]] =  str_replace('"', '', $keyvalue[1]);
      }
    }
    return $result;
}

Edited and corrected, Here's the output

Array ( [transaction_id] => 2401_02-29-12_22:28:34_0 [action] => payment [result] => success [to_from] => to [amount] => 205.00 [gross] => 205.00 [net] => -0.85 [custom] => [business] => [email protected] [item_name] => XXXXX.XXX [item_code] => Product [Name] => [3,"] => [quantity] => 1 [transaction_type] => authorized [payment"] => [transaction_status] => pending [transaction_date] => 2012-02-29 [22:28:34"] => [processing_rate] => 0.85 [discount_fee] => 0.00 [first_name] => TESTNAME [last_name] => TESTLNAME [address] => TESTADDRESS [city] => TESTCITY [state_or_province] => AL [country] => US [zip_or_postal_code] => 12345 [phone] => 1234562002 [email] => [email protected] [shipment] => yes [shipping_address] => TESTADDRESS [shipping_city] => TESTCITY [shipping_state_or_province] => AL [shipping_country] => US [shipping_zip_or_postal_code] => 12345 [processing_time] => 0.2187 
Sign up to request clarification or add additional context in comments.

3 Comments

Carlos I'm getting some errors on some of the indexes. It seems like its skiping some of the values: A PHP Error was encountered Severity: Notice Message: Undefined offset: 1 Filename: admin/test.php Line Number: 65 the line is : $result[$keyvalue[0]] = str_replace('"', '', $keyvalue[1]);
This method fails if any of the strings that were passed have spaces in them. For example, look at the part transaction_type="authorized payment". Your code turned it into [transaction_type] => authorized [payment"] => . It looks like making this approach work would require you to use regular expressions or something so you can split the string only at spaces that aren't inside quotes. I feel like there must be a better way...
I added an if statement inside the foreach, so it skips the errors, now im getting Array ( [transaction_id] => 2364_03-01-12_14:29:40_0 [action] => payment [result] => failed [errors] => 98 [errors_meaning] => (98) [customer_errors_meaning] => [processing_time] => 0.0372 ) which are the the main keys i needed. thanks

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.