Curl php script to grab nodes fails

rhandalthor
rhandalthor used Ask the Experts™
on
Following cURL Xpath script found on web -http://www.devdevote.com/server-script/php/get-xml-data-with-xpath-curl - I want to use as a base for grabbing H1s fails with undefined variable on line 30. Why?
<?php
function get_url($url)
{a
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_HEADER, 0);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($curl, CURLOPT_URL, $url);
     $data = curl_exec($curl);
     curl_close($curl);
     return $data;
}

function get_url_data()
{
     $xml_content = get_url("http://wordpress.org/development/feed/");
     $dom = new DOMDocument();
     @$dom->loadXML($xml_content);
     $xpath = new DomXPath($dom);
     $content_title = $xpath->query('//channel//title/text()');
     return $content_title;
}

function print_url_data()
{
     $content = get_url_data();
     foreach ($content as $value)
     {
          $output .= $value->nodeValue . "<br />";
     }
     return $output;
}

echo print_url_data();
?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2004
Commented:
Try to initialize $output = ''; before the foreach loop (and remove the a floating after the bracket on line 3).

Author

Commented:
No idea hot to do that. $output gets data from loop so how could I start it before the loop? Not a PHP genius just yet so please bear with me...
As Zyloch said. Line 3 of your code has an a after the { that shouldn't be there.

Set output to nothing ($output='';) as per line 3 of the code below.

function print_url_data()
{
     $output = '';
     $content = get_url_data();
     foreach ($content as $value)
     {
          $output .= $value->nodeValue . "<br />";
     }
     return $output;
}

Open in new window


How to Generate Services Revenue the Easiest Way

This Tuesday! Learn key insights about modern cyber protection services & gain practical strategies to skyrocket business:

- What it takes to build a cloud service portfolio
- How to determine which services will help your unique business grow
- Various use-cases and examples

Author

Commented:
Did not know you could initialize a variable like that.

<?php
function get_url($url)
{
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_HEADER, 0);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($curl, CURLOPT_URL, $url);
     $data = curl_exec($curl);
     curl_close($curl);
     return $data;
}

function get_url_data()
{
     $xml_content = get_url("http://wordpress.org/development/feed/");
     $dom = new DOMDocument();
     @$dom->loadXML($xml_content);
     $xpath = new DomXPath($dom);
     $content_title = $xpath->query('//channel//title/text()');
     return $content_title;
}


function print_url_data()
{
     $output = '';
     $content = get_url_data();
     foreach ($content as $value)
     {
          $output .= $value->nodeValue . "<br />";
     }
     return $output;
}

echo print_url_data();
?>

Open in new window

Still gave me PHP Parse error:  syntax error, unexpected T_VARIABLE in /opt/local/www/php/curl.php on line 25
I don't get that error using that code. I don't receive an output either. You also need to change the URL you're getting, as it has been redirected.:
NOTE, I have removed the @$dom suppression.

<?php
function get_url($url)
{
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_HEADER, 0);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($curl, CURLOPT_URL, $url);
     $data = curl_exec($curl);
     curl_close($curl);
     return $data;
}

function get_url_data()
{
     #$xml_content = get_url("http://wordpress.org/development/feed/");
     $xml_content = get_url("http://wordpress.org/news/feed/");
     $dom = new DOMDocument();
     $dom->loadXML($xml_content);
     $xpath = new DomXPath($dom);
     $content_title = $xpath->query('//channel//title/text()');
     return $content_title;
}


function print_url_data()
{
     $output = '';
     $content = get_url_data();
     foreach ($content as $value)
     {
          $output .= $value->nodeValue . "<br />";
     }
     return $output;
}

echo print_url_data();
?>

Open in new window

Author

Commented:
<?php
function get_url($url)
{
     $curl = curl_init(); // start url grabber
     curl_setopt($curl, CURLOPT_HEADER, 0);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($curl, CURLOPT_URL, $url);
     $data = curl_exec($curl);
     curl_close($curl);
     return $data;
}

function get_url_data()
{
     #$xml_content = get_url("http://wordpress.org/development/feed/");
     $xml_content = get_url("http://wordpress.org/news/feed/");
     $dom = new DOMDocument();
     $dom->loadXML($xml_content);
     $xpath = new DomXPath($dom); 
     $content_title = $xpath->query('//channel//title/text()');
     return $content_title;
}


function print_url_data()
{
     $output = ''; // to initialize the variable
     $content = get_url_data();
     foreach ($content as $value)
     {
          $output .= $value->nodeValue . "<br />";
     }
     return $output;
}

echo print_url_data();
?>

Open in new window


works. Do not know why I got the error before. Thanks for all the help. Will now need to read on how to work with Xpath to grab H1. Thanks again

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial