Hi guys

Wondering if someone could give me a hand with another problem I'm currently having.

A little background first...
I'm trying to pick up a daily report which is in XML format via a URL. I then need to parse that data and insert it into a MYSQL table. The parsing and the inserting the data into the database works fine but I'm having trouble calling the actual file.

Here's a bit of the code:

$doc = new DOMDocument();
	$doc->load('http://www.xxxxxxxxxxxxx.co.uk/xxxxxxxxxxx.aspx?AccountID=xxxxxxxxxxxxx&CampaignID=xxxxxxxxxx&FromDate=03/07/2010 00:00:00&ToDate=04/07/2010 00:00:00&type=xml');
	$arrFeeds = array();
	foreach ($doc->getElementsByTagName('item') as $node) {
		$itemRSS = array ( 
			'title' => $node->getElementsByTagName('transdate')->item(0)->nodeValue
			);
		array_push($arrFeeds, $itemRSS);
		........
		........
	}

Doesn't seem to do anything when I run it but if I manually save the file in XML and then run the same code loading that XML file it works perfectly.

So, in my wisdom I thought that it might be a case that I can't use the URL in the code but can instead try to save the file automatically onto the server in XML format and then run the script and it should work fine.

Unfortunately no matter what I do I can't get the file to save on my server.

Here's what I have but it's probably all wrong!

$url = "http://www.xxxxxxxxxxxxx.co.uk/xxxxxxxxxxx.aspx?AccountID=xxxxxxxxxxxxx&CampaignID=xxxxxxxxxx&FromDate=03/07/2010 00:00:00&ToDate=04/07/2010 00:00:00&type=xml";


$file = fopen($url,"rb");
if($file){

  $newfile = fopen("./reports/xxxx.xml", "wb");

  if($newfile){
    while(!feof($file)){

    fwrite($newfile,fread($file,1024 * 8),1024 * 8);

    }
  }
}

Would anyone kindly be able to point me in the right direction?

Recommended Answers

All 6 Replies

Check your phpinfo to see if allow_url_fopen is enabled.

allow_url_fopen

This option enables the URL-aware fopen wrappers that enable accessing URL object like files. Default wrappers are provided for the access of remote files using the ftp or http protocol, some extensions like zlib may register additional wrappers.

Note: This setting can only be set in php.ini due to security reasons.

Check your phpinfo to see if allow_url_fopen is enabled.

allow_url_fopen

This option enables the URL-aware fopen wrappers that enable accessing URL object like files. Default wrappers are provided for the access of remote files using the ftp or http protocol, some extensions like zlib may register additional wrappers.

Note: This setting can only be set in php.ini due to security reasons.

Many thanks for the quick reply. I can confirm it is already enabled.

This worked with an XML file from another site:
Are you getting any errors from PHP?

<?PHP

$url = 'http://www.dpreview.com/feeds/news.xml';
//$url = "http://www.xxxxxxxxxxxxx.co.uk/xxxxxxxxxxx.aspx?AccountID=xxxxxxxxxxxxx&CampaignID=xxxxxxxxxx&FromDate=03/07/2010 00:00:00&ToDate=04/07/2010 00:00:00&type=xml";

$file = fopen($url,"rb");
if($file){

$newfile = fopen("./reports/xxxx.xml", "wb");

  if($newfile){
    while(!feof($file)){

    $out .= fread($file,1024 * 8);
	
    }
  fwrite($newfile,$out);
  }
}


echo '<textarea rows="40" cols="90">'.htmlentities($out).'</textarea>';

?>

No PHP errors.

From your example... although it displayed the result it didn't save any file to the server. From my URL I don't even get the results displaying :(

Perhaps it is an authenitcation issue. Do you need to be logged in to view that URL?
My example should do both, as long as you have write permissions to that folder/directory.

The URL is available without having to log in to their system.

I'm beginning to think there's something wrong with the format of the file. I've just gone to the link directly on my browser and the structure is:

<?xml version="1.0" encoding="utf-8"?>
<xml version="2.0">
	<channel>
		<item>
		.....
		</item>
	</channel>
</xml>

Which can't be right can it? Do you think that's what is causing the problem?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.