Avatar of rhandalthor
rhandalthorFlag for Russian Federation

asked on 

Curl php script to grab nodes fails

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

PHPWeb DevelopmentApache Web Server

Avatar of undefined
Last Comment
rhandalthor
SOLUTION
Avatar of Zyloch
Zyloch
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of rhandalthor
rhandalthor
Flag of Russian Federation image

ASKER

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...
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of rhandalthor
rhandalthor
Flag of Russian Federation image

ASKER

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
ASKER CERTIFIED SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of rhandalthor
rhandalthor
Flag of Russian Federation image

ASKER

<?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
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo