XML/php foreach loop not looping all data - php

It seems my foreach is looping the correct amount of times. However it's only populating the variables with content from the first loop.
I tried it 2 ways. but firstly this is the url to the XML feed
http://wowfeeds.wipeitau.com/GuildActivity.php?location=EU&rn=shadowsong&gn=antheas&output=XML&callback=? so you can see the structure.
the php codee issss
function GetAchievements(){
$achurl = "http://wowfeeds.wipeitau.com/GuildActivity.php?location=EU&rn=shadowsong&gn=antheas&limit=100&output=XML&callback=?";
$achxml = new SimpleXMLElement($achurl);
// Achievements
foreach ($achxml->ACTIVITYLIST->ACTIVITYITEM as $ach) {
$name = $ach['NAME'];
echo $name;
//$Achievments = "<p><img src='$achimg' /> <span class='red'>$achname</span> $achtext <span class='red'>$achobj</span></p>";
//echo $Achievments;
}
}
This seems to just return a blank.
However if I alter the code # $name = $ach['NAME'] to = $name =
function GetAchievements(){
$achurl = "http://wowfeeds.wipeitau.com/GuildActivity.php?location=EU&rn=shadowsong&gn=antheas&limit=100&output=XML&callback=?";
$achxml = new SimpleXMLElement($achurl);
// Achievements
foreach ($achxml->ACTIVITYLIST->ACTIVITYITEM as $ach) {
$name = $achxml->ACTIVITYLIST->ACTIVITYITEM->NAME;
echo $name;
//$Achievments = "<p><img src='$achimg' /> <span class='red'>$achname</span> $achtext <span class='red'>$achobj</span></p>";
//echo $Achievments;
}
}
Then it simply repeats the first entry the same number of times as entries.
EG.
Name.
Name.
Name.
This been driving me mad for 2 hours now. Please help :(

simplexml object is not an array, you might need to consider like this
$url = 'http://wowfeeds.wipeitau.com/GuildActivity.php?'.
'location=EU&rn=shadowsong&gn=antheas&output=XML&callback=?';
$achxml = simplexml_load_file($url);
foreach ($achxml->ACTIVITYLIST->ACTIVITYITEM as $ach)
{
$name = (string) $ach->NAME;
echo $name, "\n";
}
output :
Ichex
Azraelka
Brechnor
Rougwar
Bromious
Ziini
Ryoden
Ashlynne
Snappidagg
Flökræ
Flökræ
Sevenfold
Ashlynne
Bonewing
Goldstroke
Flökræ
Worgin
Bromious
Renevatio
Ziini
Flökræ
Flökræ
Strollomiona
Thorban
Ichex

Related

Call array_map multiple times in PHP ( Vlookup in CSV using php array)

I have a problem with RSS parsing in PHP. I need to change some tags in RSS and also assign values to some of the data according to schema provided by another website. I manage to change Country names to ISO with array_map but when I'm trying to reuse the function for another value it's not working. I'm using different .csv files for each array.
Any Ideas ?
Example:
Country name == ISO3 Code
Job Type == Code (Consultancy = 234,Internship = 456 etc).
[CODE]:
$field_country = substr($field_country, strrpos($field_country, "|")+1);
$field_city = $feed[$x]['field_city'];
$field_job_type = $feed[$x]['field_job_type'];
$category = $feed[$x]['category'];
$category = split('[|,]', $category);
$field_job_experience = $feed[$x]['category'];
$field_job_experience = substr($field_job_experience, strrpos($field_job_experience, "Job Level|")+10);
$job_number = $feed[$x]['job_number'];
$pubDate = date('c', strtotime($feed[$x]['pubDate']));
$closeDate = date('c', strtotime($feed[$x]['closeDate']));
echo '<title>'.$title.'</title>';
echo '<pubDate>'.$pubDate.'</pubDate>';
echo '<field_job_closing_date>'.$closeDate.'</field_job_closing_date>';
$csv = array_map('str_getcsv', file('countries.csv'));
$findName= $field_country;
foreach($csv as $values)
{
if($values[1]==$findName)
echo '<field_country>' .$values[2]. '</field_country>';
};
echo '<field_city>'.$field_city.'</field_city>';
$csv = array_map('str_getcsv', file('job_category.csv'));
$findName= $field_job_type;
foreach($csv as $values)
{
if($values[0]==$findName)
echo '<field_job_type>' .$values[1]. '</field_job_type>';
};
echo '<field_career_categories>'.$category[1].'</field_career_categories>';
echo '<field_job_experience>'.$field_job_experience.'</field_job_experience>';
echo '<field_source>1979</field_source>';
echo '<field_theme></field_theme>';
echo '</item>';
}
echo '</channel></rss></xml>';

Scraping html tables with different number of rows

I'm trying to pull data from this site http://www.citizencorps.fema.gov/cc/CertIndex.do?reportsForState&cert=&state=IN using php. Can anyone please tell me why my code below isn't working. Ideally I want to pull the Name, Point of contact, Phone number, email, and Brief Description if one exists then convert that data into a csv file.
<?php
require_once "support/simple_html_dom.php";
$url = "http://www.citizencorps.fema.gov/cc/CertIndex.do?reportsForState&cert=&state=IN";
$html = file_get_html($url);
foreach($html->find('tr') as $row) {
$name = $row->find('td', 0)->plaintext;
$poc = $row->find('td', 1)->plaintext;
$phone = $row->find('td', 2)->plaintext;
$email = $row->find('td', 3)->plaintext;
if(count($row->find('td', 4)->plaintext) > 0) {
$desc = find('td', 4)->plaintext;
}
print_r($name.'<br/>'. $poc.'<br/>'.$phone.'<br/>'.$email.'<br/>'.$desc);
}
?>

Saving objects in array back to JSON

I'm trying to filter out some unwanted Instagram images based on a review score that is a part of the photo caption.
I'm using the Instagram PHP API by cosenary for it and I have the data, and I have been able to filter out the entries with the correct syntax for a review score (*/10).
The problem is trying to get the objects back into json form so I can cache it so I dont have to keep making a million requests to Instagram.
I am trying to store the objects in an array and then encode the array back to json, but I keep getting the error in
Trying to get property of non-object in /Users/***/Sites/instagram/index.php on line 16
Here is what I have so far.
<?php
require_once('instagram.class.php');
$app_key = '<removed>';
$instagram = new Instagram($app_key);
$media = $instagram->getTagMedia('kittens');
$pattern = '/\d+\/+10/';
$fulldata = array();
if($media) {
do {
foreach ($media->data as $entry) {
if(is_object($entry) && preg_match($pattern, $entry->caption->text)) {
$fulldata[] = $entry;
}
}
} while ($media = $instagram->pagination($media));
}
?>
<pre>
<? echo json_encode($fulldata); ?>
</pre>
I'm not sure if that is_object($entry) does much.
This should work
<?php
require_once('instagram.php');
$app_key = '';
$instagram = new Instagram($app_key);
$media = $instagram->getTagMedia('cats');
$pattern = '/\d+\/+10/';
$fulldata = array();
if($media) {
do {
foreach ($media->data as $entry) {
$text = $entry->caption->text;
if(preg_match($pattern, $text)) {
$fulldata[] = $entry;
}
}
} while ($media = $instagram->pagination($media));
}
?>
<pre>
<? echo json_encode($fulldata); ?>
</pre>

Youtube feed array items in json showing as NULL

I am grabbing the comments from videos, but the "author name" and their id is giving NULL.
This is what I have.
$feedUrl='http://gdata.youtube.com/feeds/api/videos/'.$selected_video_id.'/comments?v=2&alt=json';
$data = json_decode(file_get_contents($feedUrl),true);
$info = $data["feed"];
$entry = $info["entry"];
$nEntry = count($entry);
for($i=0;$i<$nEntry;$i++){
$name = $entry[$i]['author']['name']['$t'];
$userId = $entry[$i]['author']['yt$userId']['$t'];
$content = $entry[$i]['content']['$t'];
$published_2 = $entry[$i]['published']['$t'];
}
Content and Published are collected fine, but the name and userID are not.
The feed does have the elements in it as have looked at it in the youtube data api demo beta. As well as it shows everything if you do a feed request in the browser.
http://gdata.youtube.com/feeds/api/videos/VIDEO_ID/comments
So am I missing something?
It should be:
$name = $entry[$i]['author'][0]['name']['$t'];
$userId = $entry[$i]['author'][0]['yt$userId']['$t'];
$content = $entry[$i]['content']['$t'];
$published_2 = $entry[$i]['published']['$t'];
Or better yet:
$feedUrl=file_get_contents('http://gdata.youtube.com/feeds/api/videos/ASO_zypdnsQ/comments?v=2&alt=json');
$json = json_decode($feedUrl, true);
foreach($json['feed']['entry'] as $entry) {
echo $entry['author'][0]['name']['$t']."<br>";
echo $entry['author'][0]['yt$userId']['$t']."<br>";
echo $entry['content']['$t']."<br>";
echo $entry['published']['$t']."<br>";
}
You can use foreach like the above :)

getting 2 elements from xml

how to retrieve both instances of element in an xml file
here is how i have been getting the others
$LargeImage = $xml->Items->Item->LargeImage->URL;
$author = $xml->Items->Item->ItemAttributes->Author;
echo ($author);
however for $author, there are 2 authors and the element is like this
Items->Item->ItemAttributes->
<Author>Ralph Bravaco</Author>
<Author>Shai Simonson</Author>
so my current code is only able to get back the first author
Try this:
foreach($xml->Items->Item->ItemAttributes->Author as $author) {
echo (string)$author.'<br>';
}
It will echo all authors irrespective of no. of authors.
$xml = new SimpleXMLElement($string);
$result = $xml->xpath('/Items/Item/ItemAttributes/Author');
while(list( , $node) = each($result)) {
echo $node,"\n";
}
I think you are asking for this --
echo $author = $xml->Items->Item->ItemAttributes->Author[0];
echo $author = $xml->Items->Item->ItemAttributes->Author[1];

Categories