I am reading some xml which has come back from amazon's api. Most times amazon provides a list of similar products in their xml, but not always. If there are similar products I echo our them as a set of links. The problem is that not all products have similar products in the xml. I would like to change this code so that it checks if there are similar products nodes first and then if there are it continues to render them out as links as below but if not it just does nothing. The code below currently gives the following error message if there are no similar products in the xml:
Warning: Invalid argument supplied for foreach() in /public_html/product.php on line 26
if there are no similar product nodes in the xml returned from amazon.
Thanks in advance.
foreach ($result->Items->Item->SimilarProducts->SimilarProduct as $SimilarProduct) {
echo "<li>" . $SimilarProduct->Title . "</li/> \n";
}
You can do this test before the foreach (note the if):
if ($result->Items->Item->SimilarProducts->SimilarProduct !== null)
{
foreach ($result->Items->Item->SimilarProducts->SimilarProduct as $SimilarProduct) {
echo "<li>" . $SimilarProduct->Title . "</li/> \n";
}
}
Anyway this test works if there're no errors or null vars before the 'SimilarProduct' (for example if 'Item' is null the script will fail anyway).
Note: I suggest you to not do this kind of things, I mean this repeated access to vars (that is ->...->...->) because you will have a low control on the situation and on what's happening.
Related
I'm using the google admin api class to check for organization units. First level works, but second level fails. I've tried putting it in single quotes as per other posts, but still won't find it, and I get Yes No everytime. This seems to have started happening about two months ago. Anything that would have changed?
// This returns all of them
//$units = $google_client->getOrgUnits('my_customer', ["type" => "ALL"]);
if ($google_client->OrgUnitExists('my_customer', "A")) {
echo "Yes\r\n";
if ($google_client->OrgUnitExists('my_customer', "A/B")) {
echo "Yes\r\n";
}
else {
echo "No\r\n";
}
}
else {
echo "No\r\n";
}
According to the documenation for the orgunits.get request:
orgUnitPath > The full path of the organizational unit or its unique ID.
From the description of your question it looks like you are not supplying an accurate path to the request, hence the "Yes No" response.
Instead, you can also pass the id of the organization unit.
Reference
Directory API orgunits.get.
so basically Im trying to get steam player inventory in PHP from geting json content but I cannot figure out how to do it, espcially I didn't work with json a lot before. I have a problem with what I should pick in PHP to get what I want from the JSON.
PHP:
$inventoryJsonUrl = 'http://steamcommunity.com/inventory/'.$steamID.'/730/2?l=english&count=5000';
$inventoryJsonGet = file_get_contents($inventoryJsonUrl);
$inventory = json_decode($inventoryJsonGet, TRUE);
for ($i=0; $i < count($inventory['assets']) ; $i++) {
echo $inventory['assets'];
}
And lets say the $inventoryJsonURL is now http://steamcommunity.com/inventory/76561198260345960/730/2?l=english&count=5000
And I have problem with getting what I want, I mean lets say that in the for loop I want to have name of the item/skin, id of that item and some more things. But I don't know how and what I suppose to pick to get that.
Sorry for bad bad English.
Thanks in advance.
You can make use of PHP's foreach loop.
$inventories = json_decode($inventoryJsonGet , TRUE);
// you can check the structure of your array by
// print_r($inventories)
foreach ($inventories['descriptions'] as $key => $description) {
echo '<pre>';
echo $description['appid'];
echo $description['market_name'];
echo '</pre>';
}
The endpoint contains two lists: assets and descriptions. It's hard to offer some help if you don't really know what you are looking for. I think what you are looking for is descriptions, as there is all the data. See here for the first item: https://www.dropbox.com/s/z736vu6boh9rfi6/steam1.gif?dl=0
Seems as tho that is a shotgut from Counterstrike GO.
Also, this article may help you a bit: Getting someone's Steam inventory
And as a start, I suggest to beautify the content of that json, so you have a better overview of what's in there. I usually use https://codebeautify.org/jsonviewer but there are several other.
I am having trouble figuring out how to correct an issue I am having with the following code. I am trying to list the names and emails of all the people in my Active Directory. This code works. However, I also get the following warning when it is executed.
Warning: Cannot use a scalar value as an array.
From what I have read online I need to set " $name['mail']['0'] = "Not Found";" to an array. My question is how would I go about doing this. I have tried every way I could think of with no success. If anyone could provide me with some feedback it would be greatly appreciated.
foreach ($results as $name) {
if (!isset($name['mail']['0'])){
$name['mail']['0'] = "Not Found";
}
$allnames[$name['cn']['0']]['mail'] = $name['mail']['0'];
It looks like you are trying to assign the "Not Found" value BACK into the results of the ldap query you are running.
When I wrote code to get user information from ldap, I always inserted values into a different array (getting the fields I wanted) and then displayed THAT array out in the HTML:
foreach ($results as $name)
{
if (isset($name['mail']['0']))
// Check if it IS set, rather than not set and then append the data.
{
$allnames[$name['cn']['0']]['mail'] = $name['mail']['0'];
}
}
Then after you have all the fields you want you display the $allnames array.
Having said that, when I construct the array of user information, I make the array much much simpler - knowing what I want to display out, more along the lines of this:
foreach ($results as $name)
{
if (isset($name['mail']['0']))
// Check if it IS set, rather than not set and then append the data.
{
$allnames['mail'] = $name['mail']['0'];
}
}
You (or at least I) don't need to follow the ldap structure when making the array of information I have gathered, but rather I want to get specific fields from the directory and then display them by what they are - for example when I output the value in mail I might want to hyperlink it as an email address, so I keep it in an array element that I know by key and refer to later.
I'm trying to use an xml file and retrieve all links from it. so far I have:
$xmlHeadline = $xml->channel[0]->item[3]->link;
print($xmlHeadline);
This works ok to print the single headline link for item[3] in the xml. But as you can see it is 2 levels deep. Then next one will be at channel[0]->item[4]->link. There is no channel 1, just channel[0].
All the examples on the internet only deal with 1 level deep. They all used a foreach loop, but I am not sure if that can be used here...
How can I cycle through all item's in the xml and echo all links?
Try
foreach ($xml->channel[0]->item as $item) {
echo $item->link;
}
or
foreach ($xml->xpath('/channel/item/link') as $link) {
echo $link;
}
I think you want a DOM parser, that will allow you to load the xml as a structured hierarchy and then use a function like getElementById http://php.net/manual/en/domdocument.getelementbyid.php to parse the xml and get the specific items you want at any depth.
If you provide the structure of the xml file I may be able to help with the specific use of a DOM function.
$str = '<channels><channel>
<item><link>google.com</link></item>
<item><link>google.com.cn</link></item>
<item><link>google.com.sg</link></item>
<item><link>google.com.my</link></item>
</channel></channels>';
$xml = simplexml_load_string($str);
$objs = $xml->xpath('//channel/item/link');
PS: Please include some example of your xml
I have a problem when using simplexml to read a xml document that I am getting back from a webservice call.
Reading the data is fine however one node called UserArea contains a nested XMLdocument which contains namespaces.
From this question on SO I've looked at how to deal with child nodes. However when I call the node that has this nested XML in it I get null back.
The data looks like this:
<UserArea>
<rm:EngineVersion>4.2.0.62</rm:EngineVersion>
<rm:DocumentFormat>305</rm:DocumentFormat>
<rm:Industry>AUT</rm:Industry>
<rm:Department>GEN</rm:Department>
<rm:HighestDegree year="2004" major="COMPUTER PROGRAMMING">BACHELORS</rm:HighestDegree>
<rm:ExperienceSummary>
<rm:Experience>
<rm:ExperienceKind>Summary</rm:ExperienceKind>
<rm:Years>11</rm:Years>
<rm:Detail>A total of 11 years of work experience.</rm:Detail>
</rm:Experience>
<rm:Experience>
<rm:ExperienceKind>HighestIndustry</rm:ExperienceKind>
<rm:Years>5</rm:Years>
<rm:Industry>AUT</rm:Industry>
<rm:Detail>Highest industry-related experience is 5 years in automotive </rm:Detail>
</rm:Experience>
</rm:ExperienceSummary>
</UserArea>
I am out of ideas because the code:
foreach($myObject->UserArea->children as $userAreaXML){
foreach($userAreaXML->ExperianceSummary as $summary){
echo $summary->Detail;
}
}
just doesn't work.
You might want to read http://www.sitepoint.com/blogs/2005/10/20/simplexml-and-namespaces/ .. Couldn't be explained much clearer.
This code will print out the details
$experiences = $myObject->ExperienceSummary->Experience;
foreach($experiences as $experience) {
echo $experience->Detail . "<br>";
}