homepage Welcome to WebmasterWorld Guest from 54.196.201.253
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP script to Load and Cache a copy of XML database from specific URL
load webpage and store content in server
imofloripa




msg:4609019
 3:23 pm on Sep 11, 2013 (gmt 0)

I have a database which delayes a lot to be loaded in xml, let's say the DYNAMIC URL is www.mysite.com/index.php?component=com_xml, I need a PHP script that loads this URL and save a copy of the content of the loaded webpage after loading (it takes more than 120 seconds to load the database), and stores a copy of this xml file in a specific folder of my website, with the name db.xml.

I tried many ways to accomplish this but i am NO coder and I really need help.

Thanks so much.

 

JD_Toims




msg:4609025
 3:43 pm on Sep 11, 2013 (gmt 0)

Have you tried file_put_contents();

[php.net...]

imofloripa




msg:4609037
 4:18 pm on Sep 11, 2013 (gmt 0)

thank you, i had a look to that, but i really have not enough programming skill. Could you give me some lines of code? I would appreciate really a lot... Thanks.

JD_Toims




msg:4609040
 4:37 pm on Sep 11, 2013 (gmt 0)

Basically all you have to do is store the page info in a variable, then set the location and put the variable as the info to be saved to the location you set, so:

file_put_contents('/the-full-server-path/to-where/you-want-the-file-saved/file.ext',$the_variable_you_set_with_the_page_info);

If you can't figure out how to store the page info in a variable within the script creating the file you could just create a simple open/save file with PHP.

<?php

# This will open the actual xml page on your site with
# PHP and store the info in a variable. [Obviously you
# Have to change the location to the URL of the actual
# XML page people view.]

$the_variable_you_set_with_the_page_info=file_get_contents('http://www.yoursite.com/your-xml-file-to-save.xml');

# Then you can save the file info where ever you would like.
# For this one you'll have to find your server path which is
# usually something like /home/public_html/the-directory-to-use

file_put_contents('/the-full-server-path/to-where/you-want-the-file-saved/file.ext',$the_variable_you_set_with_the_page_info);

?>

imofloripa




msg:4609082
 7:18 pm on Sep 11, 2013 (gmt 0)

thank you i will edit this code and try! thanks so much, I will post here my results! Thanks!

imofloripa




msg:4609092
 8:13 pm on Sep 11, 2013 (gmt 0)

i tried this but it is not working.

maybe the problem is the url of the dinamically generated xml page, because it is not a file.xml, but it is a dynamic accress, see:

<?php

# This will open the actual xml page on your site with
# PHP and store the info in a variable. [Obviously you
# Have to change the location to the URL of the actual
# XML page people view.]

$olx=file_get_contents('http://www.mysite.com.br/index.php?option=com_source&format=xml&feed=olx');

# Then you can save the file info where ever you would like.
# For this one you'll have to find your server path which is
# usually something like /home/public_html/the-directory-to-use

file_put_contents('/home/mypath/mysite.com.br/xml/olx.xml',$olx);

?>

imofloripa




msg:4609093
 8:17 pm on Sep 11, 2013 (gmt 0)

correction: i think my htaccess is blocking it... i am investigationg...

imofloripa




msg:4609095
 8:24 pm on Sep 11, 2013 (gmt 0)

actually, my htaccess was blocking something but even when i change htaccess and put a sstandard one the script does not work and i get

Warning: file_get_contents(http://www.mysite.com.br/index.php?option=com_source&format=xml&feed=olx) [function.file-get-contents]: failed to open stream: HTTP request failed! in /home/myserver/mysite.com.br/olx.php on line 8

JD_Toims




msg:4609099
 8:41 pm on Sep 11, 2013 (gmt 0)

actually, my htaccess was blocking something but even when i change htaccess and put a sstandard one the script does not work and i get

Okay, the generic .htaccess is probably not creating the .xml file while the non-generic .htaccess is likely blocking the standard PHP user-agent, so let's try setting the user-agent to something that shouldn't be blocked and using the custom .htaccess file.

<?php
ini_set('user_agent', 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)');

# This will open the actual xml page on your site with
# PHP and store the info in a variable. [Obviously you
# Have to change the location to the URL of the actual
# XML page people view.]

$olx=file_get_contents('http://www.mysite.com.br/index.php?option=com_source&format=xml&feed=olx');

# Then you can save the file info where ever you would like.
# For this one you'll have to find your server path which is
# usually something like /home/public_html/the-directory-to-use

file_put_contents('/home/mypath/mysite.com.br/xml/olx.xml',$olx);

?>

imofloripa




msg:4609104
 9:25 pm on Sep 11, 2013 (gmt 0)

you are really my hero... now it is working like a charme, and I learnt many things.. thank you so much.

imofloripa




msg:4609114
 10:10 pm on Sep 11, 2013 (gmt 0)

hum... when putting it in production site i get

Warning: file_get_contents() [function.file-get-contents]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /home/myuser/mysite.com.br/olx.php on line 10

do you think there is any way to accomplish this without allowing allow_url_fopen in php.ini?

thanks again...

JD_Toims




msg:4609137
 11:32 pm on Sep 11, 2013 (gmt 0)

Glad I could help!

do you think there is any way to accomplish this without allowing allow_url_fopen in php.ini?

No, but if you put the script doing the opening/saving in it's own directory EG /for-my-save-script-only/open-save-script.php and also put a php.ini [there are generic ones all over the place online if you don't have access to yours] in the same directory EG /for-my-save-script-only/php.ini you can change the allow_url_fopen setting [or any other] in the specific directory's php.ini file and it should only have any effect on the open-save-script.php and not the whole site.

Note: If you don't have access to the php.ini for your server, you might try contacting your host and letting them know there's a setting you need to change for one of your scripts to work properly and ask them to send you a copy of the one they normally use to put in a specific directory on your site.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved