0

i just want to get the name of 'channel' tag i.e. CHANNEL...the script works fine when i use it to parse the rss from Google..............but when i use it for some other provider it gives an output '#text' instead of giving 'channel' which is the intended output.......the following is my script plz help me out.

$url = 'http://ibnlive.in.com/ibnrss/rss/sports/cricket.xml';
    $get =  perform_curl($url);
    $xml = new DOMDocument();
    $xml -> loadXML($get['remote_content']);  
  $fetch = $xml -> documentElement;
  $gettitle = $fetch -> firstChild -> nodeName; 
  echo $gettitle; 
  function perform_curl($rss_feed_provider_url){

       $url = $rss_feed_provider_url;
       $curl_handle = curl_init();

       // Do we have a cURL session?
       if ($curl_handle) {
          // Set the required CURL options that we need.
          // Set the URL option.
          curl_setopt($curl_handle, CURLOPT_URL, $url);
          // Set the HEADER option. We don't want the HTTP headers in the output.
          curl_setopt($curl_handle, CURLOPT_HEADER, false);
          // Set the FOLLOWLOCATION option. We will follow if location header is present.
          curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true);
          // Instead of using WRITEFUNCTION callbacks, we are going to receive the remote contents as a return value for the curl_exec function.
          curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);

          // Try to fetch the remote URL contents.
          // This function will block until the contents are received.
          $remote_contents = curl_exec($curl_handle);

          // Do the cleanup of CURL.
          curl_close($curl_handle);

          $remote_contents = utf8_encode($remote_contents);

          $handle = @simplexml_load_string($remote_contents);
          $return_result = array();
          if(is_object($handle)){
              $return_result['handle'] = true;
              $return_result['remote_content'] = $remote_contents;
              return $return_result;
          }
          else{
              $return_result['handle'] = false;
              $return_result['content_error'] = 'INVALID RSS SOURCE, PLEASE CHECK IF THE SOURCE IS A VALID XML DOCUMENT.';
              return $return_result;
          }

        } // End of if ($curl_handle)
      else{
        $return_result['curl_error'] = 'CURL INITIALIZATION FAILED.';
        return false;   
      }
   } 

2 Answers 2

2

it gives an output '#text' instead of giving 'channel' which is the intended output it happens because the $fetch -> firstChild -> nodeType is 3, which is a TEXT_NODE or just some text. You could select channel by

echo $fetch->getElementsByTagName('channel')->item(0)->nodeName;

and

$gettitle = $fetch -> firstChild -> nodeValue;
var_dump($gettitle); 

gives you

string(5) "
    "

or spaces and a new line symbol which happens to appear between the xml tags due to formatting.

ps: and RSS feed by your link fails validation at http://validator.w3.org/feed/

Sign up to request clarification or add additional context in comments.

Comments

0

Take a look at the XML - it's been pretty printed with whitespace so it is being parsed correctly. The first child of the root node is a text node. I'd suggest using SimpleXML if you want an easier time of it, or use XPath queries on your DomDocument to obtain the tags of interest.

Here's how you'd use SimpleXML

$xml = new SimpleXMLElement($get['remote_content']);
print $xml->channel[0]->title;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.