[PHP]Using Curl with DOMDocument

Associate
Joined
21 May 2003
Posts
1,365
Is it possible to get around remote files being unavailable using Curl?

My webhost have turned off allow_url_fopen so I can't use a DOMDocument->load() directly, but is it possible to get the data using curl and then using the core DOM functions to grab the result as an XML object?
 
PHP:
<?php

$cURL = curl_init('http://site.com/');
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
$dom = new DomDocument();
$dom->loadHTML(curl_exec($cURL));

?>

Should work.
 
Yep, that'll work, or for a pure PHP (and therefore more portable) solution try using Snoopy.

Code:
require_once 'snoopy.php';
$snoopy = new snoopy;
$snoopy->fetch('http://foo.com/');

$dom = new DomDocument();
$dom->loadHTML($snoopy->results);
 
Cheers guys, i'll give it a try tomorrow.

Just out of interest is having allow_url_fopen turned on a massive security threat? I think it's on by default on most PHP installs?
 
LazyManc said:
Cheers guys, i'll give it a try tomorrow.

Just out of interest is having allow_url_fopen turned on a massive security threat? I think it's on by default on most PHP installs?

Only if you code with bad security on your scripts.
 
Beansprout said:
Of which there are commonly zillions running on a typical shared hosting environment :(

I use suPHP, gives PHP to my unix user account and no one elses. Im on a shared host but no one can get near my scripts because PHP runs under my user. Other users PHP cant even access my scripts.
 
Jaffa_Cake said:
I use suPHP, gives PHP to my unix user account and no one elses. Im on a shared host but no one can get near my scripts because PHP runs under my user. Other users PHP cant even access my scripts.
Doesn't matter, you (or any other use) could still be hit by your/their own scripts (infact even more so with suphp) :)

'tis a pain :(
 
I've never used the XML features of PHP before, so could someone give me a quick run through of how to grab stuff please? I've tried the example at http://weblog.bignerdranch.com/?p=8 but childNodes doesn't seem to be returning anything.

Code:
<?php
	$xmlString = 
	'<Voucher>
		<VoucherID>1234567</VoucherID>
		<VoucherCode>ABCDEFGHIJKLM</VoucherCode>
	</Voucher>';
	
	$Dom->loadXML($xmlString);
	
	$voucher = $Dom->getElementsByTagName('Voucher');

	
?>

In the above example, how do I get VoucherID or VoucherCode values to assign to php vars?
 
Back
Top Bottom