tags:

views:

1261

answers:

4

I am trying to load an XML file from a different domain name as a string. All I want is an array of the text within the < title >< /title > tags of the xml file, so I am thinking since I am using php4 the easiest way would be to do a regex on it to get them. Can someone explain how to load the XML as a string? Thanks!

+2  A: 

first use file_get_contents('http://www.example.com/');

to get the file, insert in to var. after parse the xml the link is http://php.net/manual/en/function.xml-parse.php have example in the comments

Haim Evgi
Thanks you, I got this error: "Warning: file_get_contents(): URL file-access is disabled in the server configuration." Is there a way to do this with cURL I know that is enabled. Thanks!
John Isaacks
u can use it // fictional URL to an existing file with no data in it (ie. 0 byte file) $url = 'http://www.example.com/empty_file.txt'; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, false); // execute and return string (this should be an empty string '') $str = curl_exec($curl); curl_close($curl); // the value of $str is actually bool(true), not empty string '' var_dump($str);
Haim Evgi
i take it fromhttp://php.net/manual/en/function.curl-exec.php
Haim Evgi
A: 

You could use cURL like the example below. I should add that regex-based XML parsing is generally not a good idea, and you may be better off using a real parser, especially if it gets any more complicated.

You may also want to add some regex modifiers to make it work across multiple lines etc., but I assume the question is more about fetching the content into a string.

<?php

$curl = curl_init('http://www.example.com');

//make content be returned by curl_exec rather than being printed immediately                                 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($curl);

if ($result !== false) {
    if (preg_match('|<title>(.*)</title>|i', $result, $matches)) {
        echo "Title is '{$matches[1]}'";   
    } else {
        //did not find the title    
    }
} else {
    //request failed
    die (curl_error($curl)); 
}
Tom Haigh
A: 

I have this function as a snippet:

function getHTML($url) {
    if($url == false || empty($url)) return false;
    $options = array(
     CURLOPT_URL            => $url,     // URL of the page
     CURLOPT_RETURNTRANSFER => true,     // return web page
     CURLOPT_HEADER         => false,    // don't return headers
     CURLOPT_FOLLOWLOCATION => true,     // follow redirects
     CURLOPT_ENCODING       => "",       // handle all encodings
     CURLOPT_USERAGENT      => "spider", // who am i
     CURLOPT_AUTOREFERER    => true,     // set referer on redirect
     CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
     CURLOPT_TIMEOUT        => 120,      // timeout on response
     CURLOPT_MAXREDIRS      => 3,       // stop after 3 redirects
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    //Ending all that cURL mess...


    //Removing linebreaks,multiple whitespace and tabs for easier Regexing
    $content = str_replace(array("\n", "\r", "\t", "\o", "\xOB"), '', $content);
    $content = preg_replace('/\s\s+/', ' ', $content);
    $this->profilehtml = $content;
    return $content;
}

That returns the HTML with no linebreaks, tabs, multiple spaces, etc, only 1 line.

So now you do this preg_match:

$html = getHTML($url)
preg_match('|<title>(.*)</title>|iUsm',$html,$matches);

and $matches[1] will have the info you need.

Daniel S
A: 

If you're loading well-formed xml, skip the character-based parsing, and use the DOM functions:

$d = new DOMDocument;
$d->load("http://url/file.xml");
$titles = $d->getElementsByTagName('title');
if ($titles) {
    echo $titles->item(0)->nodeValue;
}

If you can't use DOMDocument::load() due to how php is set up, the use curl to grab the file and then do:

$d = new DOMDocument;
$d->loadXML($grabbedfile);
...
klktrk