Page 1 of 1

unexpected '$item' (T_VARIABLE), expecting ')'

Posted: Mon Jul 20, 2015 7:43 am
by anjshr7
Parse error: syntax error, unexpected '$item' (T_VARIABLE), expecting ')' in D:\Program Files\wamp\www\New folder\ndtv_news.php on line 18

ndtv_news.php

Code: Select all

<?php
//fetches articles from NDTV news
function fetch_news(){

    $data = file_get_contents('http://feeds.bcci.co.uk/news/rss.xml');
    $data = simplexml_load_string($data);

    $articles = array();
    foreach ($data->channel->item as $item){
    $media = $item->children('http://search.yahoo.com/mrss');
    $image = array();

    foreach($media->attribute() as $key => $value){
        $image[$key] = (string)$value;
    }

$articles[] = array(
    'title'       => (srting)$item->title,
    'description' => (srting)$item->description,
    'link'        => (srting)$item->link,
    'date'        => (srting)$item->pubDate,
    'image'       => $image);
}

return $articles;
}

?>

Re: unexpected '$item' (T_VARIABLE), expecting ')'

Posted: Mon Jul 20, 2015 7:45 am
by Celauran
You have a typo where you're trying to cast to 'srting' rather than 'string'

Re: unexpected '$item' (T_VARIABLE), expecting ')'

Posted: Mon Jul 20, 2015 7:53 am
by anjshr7
Now i am getting ..


Warning: simplexml_load_string(): Entity: line 34: parser error : EntityRef: expecting ';' in D:\Program Files\wamp\www\New folder\ndtv_news.php on line 6

Trying to get property of non-object in D:\Program Files\wamp\www\New folder\ndtv_news.php on line 9

Re: unexpected '$item' (T_VARIABLE), expecting ')'

Posted: Mon Jul 20, 2015 8:17 am
by Celauran
Have you checked what you're getting back from file_get_contents? That URL does not appear to be valid.

Re: unexpected '$item' (T_VARIABLE), expecting ')'

Posted: Mon Jul 20, 2015 8:21 am
by anjshr7
ohh... now i checked its working but showing differt error...

Fatal error: Call to undefined method SimpleXMLElement::attribute() in D:\Program Files\wamp\www\New folder\ndtv_news.php on line 12

Re: unexpected '$item' (T_VARIABLE), expecting ')'

Posted: Mon Jul 20, 2015 8:23 am
by Celauran
That's pretty straightforward; you're calling a method that doesn't exist. http://php.net/manual/en/class.simplexmlelement.php

Re: unexpected '$item' (T_VARIABLE), expecting ')'

Posted: Mon Jul 20, 2015 1:56 pm
by anjshr7
Can u please tell me in the code how to do because its 1st tym i am using xml.....

Re: unexpected '$item' (T_VARIABLE), expecting ')'

Posted: Fri Jul 24, 2015 5:41 pm
by Pazuzu156
anjshr7 wrote:Can u please tell me in the code how to do because its 1st tym i am using xml.....
I replied to another post you made, personally I wouldn't make duplicate posts for the same problem.

Refer to the comment I posted there for an example using code:
viewtopic.php?f=1&t=141570&p=700729#p700729