Getting the data out of this array...

[7] => Array ( [name] => ITEM [attrs] => Array ( )
                                        [children] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [name] => MEDIA:CONTENT
                                                        [attrs] => Array
                                                            (
                                                                [URL] => http://l.yimg.com/iu/api/res/1.2/XsIZGoFrlt7QyYKQGKJ8ig--/YXBwaWQ9eXZpZGVvO2NoPTkxMDtjcj0xO2N3PTkxMDtkeD0xO2R5PTE7Zmk9dWxjcm9wO2g9MTMwO3E9MTAwO3c9MTMw/http://media.zenfs.com/en_us/Sports/ap/201503141400504539867
                                                                [HEIGHT] => 130
                                                                [WIDTH] => 130
                                                                [TYPE] => image/jpeg
                                                            )

                                                    )

                                                [1] => Array
                                                    (
                                                        [name] => MEDIA:CREDIT
                                                        [attrs] => Array
                                                            (
                                                                [ROLE] => provider
                                                            )

                                                        [cdata] => The Associated Press
                                                    )

                                                [2] => Array
                                                    (
                                                        [name] => MEDIA:TEXT
                                                        [attrs] => Array
                                                            (
                                                                [TYPE] => html
                                                            )

                                                        [cdata] => Minnesota Vikings wide receiver Greg Jennings (15) runs the ball for a touchdown against the Detroit Lions during an NFL football game at Ford Field in Detroit, Sunday, Dec. 14, 2014. (AP Photo/Rick Osentoski)
                                                    )

                                                [3] => Array
                                                    (
                                                        [name] => TITLE
                                                        [attrs] => Array
                                                            (
                                                            )

                                                        [cdata] => Vikings let receiver Greg Jennings go after Wallace trade (Yahoo Sports)
                                                    )

                                                [4] => Array
                                                    (
                                                        [name] => LINK
                                                        [attrs] => Array
                                                            (
                                                            )

                                                        [cdata] => http://us.rd.yahoo.com/sports/rss/nfl/SIG=135klaavv/*http%3A//sports.yahoo.com/news/vikings-let-receiver-greg-jennings-wallace-trade-185836946--nfl.html
                                                    )

                                                [5] => Array
                                                    (
                                                        [name] => DESCRIPTION
                                                        [attrs] => Array
                                                            (
                                                            )

                                                        [cdata] => Minnesota Vikings wide receiver Greg Jennings (15) runs the ball for a touchdown against the Detroit Lions during an NFL football game at Ford Field in Detroit, Sunday, Dec. 14, 2014. (AP Photo/Rick Osentoski)</blockquote>

That is what I have but it’s in the Cdata tag… I can see it but not sure how to get the data I’m after… I’ve tried a few different things but no go so some suggestions would be very much appreciated and helpful! :slight_smile:

What I’m after is:

the url:

[URL] => http://l.yimg.com/iu/api/res/1.2/XsIZGoFrlt7QyYKQGKJ8ig--/YXBwaWQ9eXZpZGVvO2NoPTkxMDtjcj0xO2N3PTkxMDtkeD0xO2R5PTE7Zmk9dWxjcm9wO2g9MTMwO3E9MTAwO3c9MTMw/http://media.zenfs.com/en_us/Sports/ap/201503141400504539867

The text:

[name] => MEDIA:TEXT
[attrs] => Array
(
[TYPE] => html
)

                                                        [cdata] => Minnesota Vikings wide receiver Greg Jennings (15) runs the ball for a touchdown against the Detroit Lions during an NFL football game at Ford Field in Detroit, Sunday, Dec. 14, 2014. (AP Photo/Rick Osentoski)
                                                    )

and the link:

[4] => Array
(
[name] => LINK
[attrs] => Array
(
)

                                                        [cdata] => http://us.rd.yahoo.com/sports/rss/nfl/SIG=135klaavv/*http%3A//sports.yahoo.com/news/vikings-let-receiver-greg-jennings-wallace-trade-185836946--nfl.html
                                                    )

I’m sure it’s probably FAR easier than I am making it :slight_smile:

SO I have the Cdata loaded but getting the info is another matter…

Thank you all!!

What do you mean it’s “in the cdata tag”? How does the code that prints this out look?

Honestly I just gave up on it. The img src tag was within the CDATA. You get that using the
[just an example] …$des= $item->description
But because buried in the CDATA I got the image and description [which I didn’t want]

2 reasons why I was trying to capture the image was because I didn’t want the description text and the image was a direct link to the article but if you clicked on the image it takes you away from the site… I use the target="_blank" approach so it opens a new window without taking you away form the site…

So I skirted the description tag altogether and used this:

[php]
//$url=$params->get(‘feed’);
$url=‘http://http://sports.yahoo.com/nfl/rss.xml’;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url); // get the url contents
$data = curl_exec($ch); // execute curl request
curl_close($ch);
$xml = simplexml_load_string($data);
foreach ($xml->channel->item as $item){
$title = $item->title;
foreach ($title as $t){
$link=$item->link;
foreach ($link as $l) {
$date = $item->pubDate;
$d = date(“l, n-j-Y”, strtotime($date));

[/php]

Which returns the link and title but no image…
I used the preg_match to get and strip the CDATA tag and that’s how I got what I posted… but I’ve tried so many different things that in that confusing mess I cannot reproduce what I posted LOL Sad…but that’s what happens when you take yourself in circles for hours…

[php]
$type=(‘http://sports.yahoo.com/nfl/rss.xml’);
$xml = simplexml_load_file($type, ‘SimpleXMLElement’, LIBXML_NOCDATA);
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);
foreach ($xml->channel->item as $e){
$imgpattern = (’/(<img[^>]+>)/i’);
preg_match($imgpattern, $e, $matches);
echo “

”;
print_r ($e);
}

[/php]

This returns

[title] => AP source: Reggie Bush agrees to contract with 49ers (Yahoo Sports) [link] => http://us.rd.yahoo.com/sports/rss/nfl/SIG=130iijmgr/*http%3A//sports.yahoo.com/news/ap-source-reggie-bush-agrees-contract-49ers-003648474--nfl.html [description] => [NO link to the img file, it just shows the image] Free-agent running back Reggie Bush has reached a deal with the San Francisco 49ers, a person with knowledge of the agreement said Saturday. Bush visited the team Wednesday and could be seen touring outside Levi's Stadium with new San Francisco coach Jim Tomsula. Bush joins a Niners team that lost Frank Gore in free agency to the Colts, left guard Mike Iupati to the Arizona Cardinals and a pair of cornerbacks who are now gone, too - Chris Culliver and Perrish Cox. [pubDate] => Sat, 14 Mar 2015 18:58:28 PDT [category] => NFL [guid] => urn:newsml:sports.yahoo,lego:19780928:top,article,ca4017f0-31dc-3f1a-baf5-b38754c12869-l:1

If I change it to "print_r($matches);

it returns this:

Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( )

[php]<?php

$xml = simplexml_load_file(‘http://sports.yahoo.com/nfl/rss.xml’, ‘SimpleXMLElement’, LIBXML_NOCDATA);

// we’ll use DOMDocument to parse the HTML in item description
// parsing html with regex is generally not recommended, please use a real html parser instead
$doc = new DOMDocument();

$items = array();
foreach ($xml->channel->item as $item) {
$doc->loadHTML($item->description);

// get first image in item description
$image = $doc->getElementsByTagName('img')->item(0);

// add to items array
$items[] = array(
	'text' => $image ? $image->getAttribute('alt') : (string) $item->description, // if item description doesn't contain an image we'll use it as the text
	'link' => (string) $item->link,
	'image' => $image ? $image->getAttribute('src') : ''
);

}

echo ‘

’;
print_r($items);[/php]

[code]Array
(
[0] => Array
(
[text] => Detroit Lions running back Reggie Bush (21) runs against Chicago Bears defenders in the first half of an NFL football game Sunday, Dec. 21, 2014, in Chicago. (AP Photo/Nam Y. Huh)
[link] => http://us.rd.yahoo.com/sports/rss/nfl/SIG=131pqidj0/*https%3A//sports.yahoo.com/news/ap-source-reggie-bush-agrees-contract-49ers-003648474--nfl.html
[image] => https://s.yimg.com/iu/api/res/1.2/t7.iH2258TR6Dso3gyxDyQ--/YXBwaWQ9eXZpZGVvO2NoPTc1Mztjcj0xO2N3PTc1MztkeD0xO2R5PTE7Zmk9dWxjcm9wO2g9MTMwO3E9MTAwO3c9MTMw/http://media.zenfs.com/en_us/Sports/ap/201503141754644520952
)

[1] => Array
    (
        [text] => Minnesota Vikings wide receiver Greg Jennings (15) runs the ball for a touchdown against the Detroit Lions during an NFL football game at Ford Field in Detroit, Sunday, Dec. 14, 2014. (AP Photo/Rick Osentoski)
        [link] => http://us.rd.yahoo.com/sports/rss/nfl/SIG=1369linfb/*https%3A//sports.yahoo.com/news/vikings-let-receiver-greg-jennings-wallace-trade-185836946--nfl.html
        [image] => https://s.yimg.com/iu/api/res/1.2/XsIZGoFrlt7QyYKQGKJ8ig--/YXBwaWQ9eXZpZGVvO2NoPTkxMDtjcj0xO2N3PTkxMDtkeD0xO2R5PTE7Zmk9dWxjcm9wO2g9MTMwO3E9MTAwO3c9MTMw/http://media.zenfs.com/en_us/Sports/ap/201503141400504539867
    )

[2] => Array
    (
        [text] => Kansas City Chiefs strong safety Ron Parker (38) before their game against the Oakland Raiders during the first half of their NFL football game in Kansas City, Mo., Sunday, Dec. 14, 2014. (AP Photo/Reed Hoffmann)
        [link] => http://us.rd.yahoo.com/sports/rss/nfl/SIG=12sd5sedd/*https%3A//sports.yahoo.com/news/ap-source-chiefs-ron-parker-5-30m-deal-181015122--nfl.html
        [image] => https://s.yimg.com/iu/api/res/1.2/cf2z9Y7xbGzTruOVdAwenA--/YXBwaWQ9eXZpZGVvO2NoPTQyOTtjcj0xO2N3PTQyOTtkeD0xO2R5PTE7Zmk9dWxjcm9wO2g9MTMwO3E9MTAwO3c9MTMw/http://media.zenfs.com/en_us/Sports/ap/201503141309473909537
    )

[3] => Array
    (
        [text] => OWINGS MILLS, Md. (AP) -- The Baltimore Ravens and safety Kendrick Lewis have reached agreement on a three-year contract.
        [link] => http://us.rd.yahoo.com/sports/rss/nfl/SIG=12n7jc6g1/*https%3A//sports.yahoo.com/news/ravens-agree-terms-kendrick-lewis-214937992--nfl.html
        [image] => 
    )

[4] => Array
    (
        [text] => (AP)
        [link] => http://us.rd.yahoo.com/sports/rss/nfl/SIG=136cq00ea/*https%3A//sports.yahoo.com/news/steelers-roethlisberger-agree-contract-extension-201339761--nfl.html
        [image] => https://s.yimg.com/iu/api/res/1.2/yKjShF2INe5e8L3mJd7q.g--/YXBwaWQ9eXZpZGVvO2NoPTk1OTtjcj0xO2N3PTk1OTtkeD0yNDA7ZHk9MTtmaT11bGNyb3A7aD0xMzA7cT0xMDA7dz0xMzA-/http://media.zenfs.com/en_us/Sports/ap/201503131515549275726
    )

[5] => Array
    (
        [text] => FILE - In this Dec. 21, 2014, file photo, Oakland Raiders running back Darren McFadden carries the ball during an NFL football game in Oakland, Calif. The Dallas Cowboys and McFadden agreed on a contract Friday, March, 13, 2015, a day after DeMarco Murray bolted for NFC East rival Philadelphia on a big contract that Dallas owner Jerry Jones said he couldn't match because of the strain it would have put on the salary cap. (AP Photo/Marcio Jose Sanchez, File)
        [link] => http://us.rd.yahoo.com/sports/rss/nfl/SIG=130grd7id/*https%3A//sports.yahoo.com/news/ap-source-mcfadden-agrees-contract-cowboys-154747200--nfl.html
        [image] => https://s.yimg.com/iu/api/res/1.2/DoyrroDtALHhopRJWS44lw--/YXBwaWQ9eXZpZGVvO2NoPTExODc7Y3I9MTtjdz0xMTg3O2R4PTE7ZHk9MTtmaT11bGNyb3A7aD0xMzA7cT0xMDA7dz0xMzA-/http://media.zenfs.com/en_us/Sports/ap/201503131303470135374
    )
    ...[/code]

Yes sir that’s it!!! I’m playing with cdata and namespace… so right now I see how you did that!! That’s awesome!

I’m rereading it right now…

Ok so you got the first one then made an array… I got it… THIS is what I didn’t play with at all I imagine I would have gotten around to it sooner or later…LOL

getElementsByTagName('img')

So is it the

LIBXML_NOCDATA
that triggers all that? I know I read it did but I had tried preg_replace to strip the cdata tag but that was WAY more work then it was worth and frustrating LOL

Thank yoU!

Trying to understand all this:

''text' => $image ? $image->getAttribute('alt') : (string) $item->description, // if item description doesn't contain an image we'll use it as the text 'link' => (string) $item->link, 'image' => $image ? $image->getAttribute('src') : ''

Lots of stuff in there I’ve never seen… $image ? image… not sure what that means

Honestly not sure, I just took your code, removed all unnecessary lines and started parsing the data you got from the xml service.

Ternary operator

[php]condition ? if true : if false[/php]

so this
[php]‘image’ => $image ? $image->getAttribute(‘src’) : ‘’[/php]

means:
set the array key ‘image’ to…
if $image is truthy (not 0, null, false, etc), then set it to the src attribute of $image
if not, set it to an empty string.

It’s a nice way to set defaults in your script. For a form would often do this
[php]$email = isset($_POST[‘email’]) ? $_POST[‘email’] : null;
$password = isset($_POST[‘password’]) ? $_POST[‘password’] : null;[/php]

Doing this we know later in the script that $email and $password do exist, so we won’t run into any trouble using them.

Oh ok. That’s quite a cool little thing. I see it allows you write one line instead of two or more… VERY cool!

Thank you for your help! This is really very educational and VERY interesting and I’ll find a way to use it! :slight_smile:

LIBXML_NOCDATA is used to set CDATA as text nodes :slight_smile:

Thanks Jim!!

It’s throwing a strange error…

Fatal error: Call to a member function getAttribute() on null on line 117
This is the line: $imgs=$image->getAttribute('src');

[php]
$xml = simplexml_load_file($type, ‘SimpleXMLElement’, LIBXML_NOCDATA);
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);
// we’ll use DOMDocument to parse the HTML in item description
// parsing html with regex is generally not recommended, please use a real html parser instead
$doc = new DOMDocument();
$items = array();
foreach ($xml->channel->item as $item) {
$doc->loadHTML($item->description);
// get first image in item description
$image = $doc->getElementsByTagName(‘img’)->item(0);
// add to items array
$items[] = array(
// if item description doesn’t contain an image we’ll use it as the text
‘text’ => $image ? $image->getAttribute(‘alt’) : (string) $item->description,
‘link’ => (string) $item->link,
‘image’ => $image ? $image->getAttribute(‘src’) : ‘’
);

$title=$item->title;
foreach ($title as $titles){
$link= $item->link;
foreach ($link as $links){
$imgs=$image->getAttribute(‘src’);
$date=$item->pubDate;
$ArtDate = date(“l, n-j-Y h:i A”, strtotime($date));
?>

<?= $ArtDate ?>

<?= $titles ?>
height=140 width=140>

Read More
<?php } } } ?> [/php]

OH I found the problem just have to figure out how to fix it…

it’s here:

'image' => $image ? $image->getAttribute('src') : ''

Because one of the ‘items’ has no image… :wink:

This fixed it!

[php]
if (is_object($image)){
$imgs=$image->getAttribute(‘src’);
}
[/php]

Goes on without errors to the end of xml file.

Thanks again Jim!

Sponsor our Newsletter | Privacy Policy | Terms of Service