2

I'm calling a web service using SOAP in PHP which is returning the data which I am then converting to an array using:

$array = json_decode(json_encode($response), True);

This gives me the following when calling print_r($array):

{"Transactions":[{"MerchantAccount":"Test Account","Owner":"Test Owner","CustomerName":"Lisa Jobs"},
{"MerchantAccount":"Test Account","Owner":"Test Owner","CustomerName":"Lisa Jobs2"},
{"MerchantAccount":"Test Account","Owner":"Test Owner","CustomerName":"Lisa Jobs3"},
{"MerchantAccount":"Test Account","Owner":"Test Owner","CustomerName":"Lisa Jobs4"}]}

Could someone point me in the right direction so that I have an array full of Transactions please? (as in get rid of the first part of the string "Transactions"

I've tried print_r($array['Transactions']); but that is giving me an error:

Warning:  Illegal string offset

Any help would be greatly appreciated.

Here's the bare data from the WSDL:

Array
(
    [0] => stdClass Object
        (
            [TransactionsSinceTicksResult] => 

        {"Transactions":[
{"MerchantAccount":"Test","Owner":"Test Owner","CustomerName":"Lisa Bloggs"},
{"MerchantAccount":"Test","Owner":"Test Owner","CustomerName":"Lisa Bloggs2"},
{"MerchantAccount":"Test","Owner":"Test Owner","CustomerName":"Lisa Bloggs3"}

        ]}
    )
)

UPDATE:

Function:

function getTransactions($sinceTicks) {

    global $userEmailAddress;
    global $userPassword;
    global $accountGatewayUser;
    global $accountGatewayPassword;

    $format = 'JSON';

    $wsdl = 'https://apps.blahblahblah.co.uk/recurring-payments/Services/Merchant.asmx?WSDL';

    try {
        $soapclient = new SoapClient($wsdl);

        $params = array ( 
            'userEmailAddress' => $userEmailAddress,
            'userPassword' => $userPassword,
            'accountGatewayUser' => $accountGatewayUser,
            'accountGatewayPassword' => $accountGatewayPassword,
            'format' => $format,
            'sinceTicks' => $sinceTicks
        );

        $response = $soapclient->TransactionsSinceTicks($params);

        $array = json_decode($response[0]->TransactionsSinceTicksResult, True);

        return ($array['Transactions']); 


        }
    catch(SoapFault $error) {
        return json_encode($error);
    }

}

This is returning: Fatal error: Cannot use object of type stdClass as array in...

0

1 Answer 1

4

The issue would seem to be here why is the response being encoded?

$array = json_decode(json_encode($response), True);

My assumption is that your $response is json already so you're json encoding a json string when that is decoded you get the json string back... just try

$array = json_decode($response, TRUE);

//$array should be an array now

print_r($array['Transactions']); //should work

UPDATES BASED ON NEW WSDL

Based on the additional data you need to get to the transactions you can do something like:

$array = json_decode($response[0]->TransactionsSinceTicksResult, TRUE);

The data seems to be an array with 1 value hence '$response[0]'. Based on the output that is an object so you need to use the accessor ->.

My Test Logic

Below is my test logic creating and object that matches what you provide. I am not sure where your issue is but what I have here matches the data provided and does what is asked for.

<?php
$trs = [
  "Transactions"=> [
    [
      "MerchantAccount"=> "Test",
      "Owner"=> "Test Owner",
      "CustomerName"=> "Lisa Bloggs"
    ],
    [
      "MerchantAccount"=> "Test",
      "Owner"=> "Test Owner",
      "CustomerName"=> "Lisa Bloggs2"
    ],
    [
      "MerchantAccount"=> "Test",
      "Owner"=> "Test Owner",
      "CustomerName"=> "Lisa Bloggs3"
    ]
  ]
];

$response = [new stdClass];

$response[0]->TransactionsSinceTicksResult = json_encode($trs);

echo "<pre>";
print_r($response); //Shows the structure you provided.

//No Errors works fine
$array = json_decode($response[0]->TransactionsSinceTicksResult, TRUE);
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for your reply, If I don't decode / encode I get an error saying json_decode() expects parameter 1 to be string, but that maybe because the original data I'm getting is slightly different, so I've updated my original question.
Thanks for your reply again, unfortunately with that line I'm getting the error: Fatal error: Cannot use object of type stdClass as array in... I think we're on the right tracks though as you're correct, the data is in an array at [0] but I have no control of what it's sending me.
Thanks again for your help - I'm still getting the error I'm afraid. I've posted up the code just to double check that I'm doing things correctly.
@MichaelBellamy I put in what I used to test. I am not sure the issue but if your response structure is not the same then I am not going to be able to help much...
@MichaelBellamy I am glad you go something to work but what you have there I am going to be frank looks very odd... What is $response before you do anything to it ie print_r($response)
|

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.