0

I am trying to use some WSDL webservice to do some work with each parameters. This is working absolutely fine; however, once the script is executed, I would like to have "log" emailed to me.

All works fine when I use PRINT, or ECHO inside the loop (this will display all values of different variables from the loop). However, outside of the loop, this will only display ONE variable.

Is there a way to store all variables into array inside of loop so this can be used later on, outside of loop, for example emailing this?

This is what I have tried:

<?php

// API request and response 
$requestParams = array(
  'Request' => 'Hello'
);

$client = new SoapClient('https://example.com/webservice.asmx?WSDL');
$response = $client->Command($requestParams);

//Load response as XML
$xml  = simplexml_load_string($response);
$rows = $xml->children('rs', TRUE)->data->children('z', TRUE)->row;

foreach ($rows as $row) {
$attributes = $row->attributes();

/* XML document contains two columns, first with attribute TO, 
   second with attribute Ref. This will extract required data  */

$To = (string) $attributes->To;  
$Ref= (string) $attributes->Ref;

// Here are few more lines in code to do some other work with each variable 

// All works absolutely fine until this line

/* I would liket to store all variables so I can use them to email 
   them as a log in one email */

$ToLog .= "<br>$To</br>";
$RefLog .="<br>$Ref</br>";
}

$to      = "[email protected]";
$subject = "Script successfully executed";

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$message = $ToLog . $RefLog

mail($to, $subject, $message, $headers);

?>

3 Answers 3

1

I think you just need to define both variable before starting the loop, like:

$ToLog = "";
$RefLog = "";

Then if you can put whatever in this variable in side the loop you will get it after the loop, You do not need to take an array.

You do not need to take an array.

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

Comments

1

try like this

$values = array();
foreach ($rows as $row) {
    $values[] = $row->attributes(); //stores the each values to the array
}

print_r($values);

3 Comments

Thanks, this works. Could you please let me know how can I put this into message? I have tried $message = ""; $message = "<pre>" ; $message.="Number\tPNR"; $message.= array_map(function ($values) { echo "\n", $values['To'], "\t\t", $values['Ref']; }, $values); But I am getting Notice: Array to string conversion
$values is it a multidimention array.
could you post array $values values.
0

Try something like this

$finalArray = array();
foreach($rows as $row)
{
   $finalArray[] = $row["someIndex"];
}

then finalArray should contains all variables from foreach :)

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.