I have a WCF Operation GetColors which returns a list of colors as GetColorsResult. I am getting the result fine, but how do I loop through GetColorsResult in php and echo each element?
I am doing:
<?php
header('Content-Type: text/plain');
echo "WCF Test\r\n\r\n";
// Create a new soap client based on the service's metadata (WSDL)
$client = new SoapClient('http://localhost:8181/Colors.svc?wsdl',array(
'login' => "test", 'password' => "test"));
$retval = $client->GetColors();
//Need to loop throuh $retval here
echo $retval->GetColorsResult; //This throws error.
?>
Is there way to control the name of the result, for example, I did not specify WCF to return GetColorsResult, it appended Result to my method call. Likewise, it appends Response to GetColors for the Response (GetColorsResponse)
Output when doing print_r($retval):
stdClass Object
(
[GetColorsResult] => stdClass Object
(
[Color] => Array
(
[0] => stdClass Object
(
[Code] => 1972
[Name] => RED
)
[1] => stdClass Object
(
[Code] => 2003
[Name] => BLUE
)
[2] => stdClass Object
(
[Code] => 2177
[Name] => GREEN
)
)
)
)
regarding to your print_r this should give you all the values:
<?php
$colorResult = $retval->GetColorsResult;
foreach($colorResult->Color as $color){
echo $color->Code . " " . $color->Name . "<br />";
}
?>
is this what you needed?
BR,
TJ
EDIT:
If you just need it for debugging purposes, you should use print_r.
Have a look here: print_r PHP Documentation
Related
I am fetching data from JSON in the form of PHP array.
But I am not able to fetch it due to strange OBJECT titles.
E.g. here is simply example
[car] => stdClass Object
(
[model] => stdClass Object
(
[year] => 2018
[company] => Honda
[condition] => Good
)
)
Now I can fetch "condition" like this $car->model->condition;
But in my case, the JSON is like this
[car] => stdClass Object
(
[field_set_key="profile",username="sammy"] => stdClass Object
(
[year] => 2018
[company] => Honda
[condition] => Good
)
)
I am not able to fetch "condition" value due to this strange object [field_set_key="profile",username="sammy"]
What should I do?
$car->[ ?????? ]->condition;
Using the {} syntax you can address that like this
echo $car->{'field_set_key="profile",username="sammy"'}->condition;
Its ugly and it would really be better if you got the people producing this data to fix their code.
Example
$car = new stdClass;
$b = new stdClass;
$b->year = 2018;
$b->company = 'Honda';
$b->condition = 'good';
$car->{'field_set_key="profile",username="sammy"'} = $b;
print_r($car) . PHP_EOL;
echo 'The condition of the '
. $car->{'field_set_key="profile",username="sammy"'}->year . ' '
. $car->{'field_set_key="profile",username="sammy"'}->company
. ' is ' . $car->{'field_set_key="profile",username="sammy"'}->condition;
RESULTS
stdClass Object
(
[field_set_key="profile",username="sammy"] => stdClass Object
(
[year] => 2018
[company] => Honda
[condition] => good
)
)
The condition of the 2018 Honda is good
Having real issues with this. I want to be able to get a value from this data which is returned via an API.
ie get value by
$CM_user_customfields['Organisation'],
$CM_user_customfields->Organisation.
is that even possible? I have tried loops and rebuilding the array but i always end up with a similar results and perhaps overthinking it.
I can't use the [int] => as the number of custom fields will be changing a lot.
$CM_user_customfields = $CM_details->response->CustomFields ;
echo '<pre>' . print_r( $CM_user_customfields, true ) . '</pre>';
// returns
Array
(
[0] => stdClass Object
(
[Key] => Job Title
[Value] => Designer / developer
)
[1] => stdClass Object
(
[Key] => Organisation
[Value] => Jynk
)
[2] => stdClass Object
(
[Key] => liasoncontact
[Value] => Yes
)
[3] => stdClass Object
...
many thanks, D.
I recommend convert to associative array first:
foreach($CM_user_customfields as $e) {
$arr[$e->Key] = $e->Value;
}
Now you can access it as:
echo $arr['Organisation'];
You can also achieve it by: (PHP 7 can convert stdClass and will do the trick)
$arr = array_combine(array_column($CM_user_customfields, "Key"), array_column($CM_user_customfields, "Value")));
I'l try to get the movie title and info from the omdb API. This is my code:
<?php
$enter = $_GET["enter"];
$content = file_get_contents("https://www.omdbapi.com/?s=$enter&r=xml");
$xml = simplexml_load_string($content);
if($xml) {
echo "<h2>" .$xml->title. "</h2>";
}
else
{
echo "Nothing found. Add the info manualy";
}
?>
The "enter" value is from the search form with AJAX. He create only an empty h2 tag. How can i get also the data from the API?
Thank you,
Julian
You should familiarize yourself with the structure of the xml to know how to access its elements. print_r(get_object_vars($xml)) will show you a structure like this:
Array
(
[#attributes] => Array
(
[totalResults] => 3651
[response] => True
)
[result] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[title] => World War Z
[year] => 2013
[imdbID] => tt0816711
[type] => movie
[poster] => https://images-na.ssl-images-amazon.com/images/M/MV5BMTg0NTgxMjIxOF5BMl5BanBnXkFtZTcwMDM0MDY1OQ##._V1_SX300.jpg
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[title] => Captain America: Civil War
[year] => 2016
[imdbID] => tt3498820
[type] => movie
[poster] => https://images-na.ssl-images-amazon.com/images/M/MV5BMjQ0MTgyNjAxMV5BMl5BanBnXkFtZTgwNjUzMDkyODE#._V1_SX300.jpg
)
)
...
...
...
[9] => SimpleXMLElement Object
(
[#attributes] => Array
(
[title] => War
[year] => 2007
[imdbID] => tt0499556
[type] => movie
[poster] => https://images-na.ssl-images-amazon.com/images/M/MV5BMTgzNTA4MTc3OF5BMl5BanBnXkFtZTcwOTA0ODk0MQ##._V1_SX300.jpg
)
)
)
)
So you receive an array with results where you need to pick from. Alternatively if you know the exact title the API has the t=title option which only returns a single result (see documentation).
So assuming you use the s=title option which returns multiple results, you can use something like this to pick information from the first result:
<?php
$enter = $_GET["enter"];
$content = file_get_contents("https://www.omdbapi.com/?s=$enter&r=xml");
$xml = simplexml_load_string($content);
# show the structure of the xml
# print_r(get_object_vars($xml));
if($xml) {
print "<h2>" .$xml->result[0]['title']. "</h2>";
print "<br>imdbID=" . $xml->result[0]['imdbID'] ;
} else {
echo "Nothing found. Add the info manualy";
}
?>
I have a web service running in Visual Studio 2015 with a method in it called GetTables(). In PHP now, I'm trying to get the response from the above method call. Here is my code in PHP:
<?php
$wsdl_url = 'http://localhost:1336/DisplayInfoWebService.svc?wsdl';
$client = new SOAPClient($wsdl_url);
$res = $client->GetTables();
echo '<pre>';
print_r($res);
echo '</pre>';
?>
The above PHP returns the following:
stdClass Object
(
[GetTablesResult] => stdClass Object
(
[tablename] => Array
(
[0] => stdClass Object
(
[name] => alcopops
)
[1] => stdClass Object
(
[name] => beers
)
[2] => stdClass Object
(
[name] => ciders
)
)
)
)
What I'm trying to do is get those values (alcopops,beers,ciders) and parse them into a PHP array so I can use them later. How can I do it?
Do you mean $res->GetTablesResult->tablename or $res->GetTablesResult->tablename[0]->name? Or perhaps even something like:
$builtArray = [];
foreach($res->GetTablesResult->tablename as $table) {
$builtArray[] = $table->name;
}
var_dump($builtArray);
I have convert my object data to an array and now i am trying to extract certain parts from the multi dimensional array however i am having some problems. Assistance is appreciated, thank you.
/* PHP SDK v4.0.0 */
/* make the API call */
$request = new FacebookRequest(
$session,
'GET',
'/89647580016/feed'
);
$response = $request->execute();
$graphObject = $response->getGraphObject()->AsArray();
/* handle the result */
// print data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
The following is the output:
Array
(
[data] => Array
(
[0] => stdClass Object
(
[id] => 89647580016_10153019927930017
[from] => stdClass Object
(
[name] => Central Casting Los Angeles
[category] => Local Business
[category_list] => Array
(
[0] => stdClass Object
(
[id] => 176831012360626
[name] => Professional Services
)
)
[id] => 89647580016
)
[message] => ***NON Union Submissions***
Must be registered with Central Casting!
Jessica is currently booking a TV show working tomorrow Friday June 26th with a possible recall Monday June 29th in LA. These will be Night Calls, so you must be okay working late into the night.
She is looking for Caucasian looking men, who appear to be in their 30's-50's, who appear to be very upscale, who have business suits.
If this is you please call submission line 818-260-3952. Thank you!
[actions] => Array
(
[0] => stdClass Object
(
[name] => Comment
[link] => https://www.facebook.com/89647580016/posts/10153019927930017
)
[1] => stdClass Object
(
[name] => Like
[link] => https://www.facebook.com/89647580016/posts/10153019927930017
)
[2] => stdClass Object
(
[name] => Share
[link] => https://www.facebook.com/89647580016/posts/10153019927930017
)
)
How can i print all the ['message'] ? I tried:
foreach ($graphObject as $key => $value) {
echo '<br>'.$key['message'];
}
But i got a error. Thank you for your help.
Your array has some keys which elements are actually StdClass. You can't reffer to it as $key['message'] but as: $key->message.
Also, don't forget to include your error message. An error is extremely generic and we can't/won't help you if there is no indication of what is wrong.
I didn't notice your foreach. As you are iterating only on $graphObject first level, you will get as $key data and as value another whole array which each key has a StdClass. On your foreach, run it as foreach ($graphObject['data'] as $key => $value) , then use it as echo $value->message