Retrieve data from url and save in php


I am trying to retrieve the html from file get contents in php then save it to a php file so I can include it into my homepage.

Unfortunately my script isn't saving the data into the file. I also need to verwrite this data on a daily basis as it will be setup with a cron job.

Can anyone tell me where I am going wrong please? I am just learning php :-)

<?php $richSnippets = file_get_contents('http://website.com/data'); $filename = 'reviews.txt'; $handle = fopen($filename,"x+"); $somecontent = echo $richSnippets; fwrite($handle,$somecontent); echo "Success"; fclose($handle); ?>


A couple of things,

http://website.com/data gets a 404 error, it doesn't exist.

Change your code to

$site = 'http://www.google.com'; $homepage = file_get_contents($site); $filename = 'reviews.txt'; $handle = fopen($filename,"w"); fwrite($handle,$homepage); echo "Success"; fclose($handle);

Remove $somecontent = echo $richSnippets; it doesn't do anything.

if you have the proper permissions it should work.

Be sure that your pointing to an existing webpage.


When cURL is enabled you can use the following function

function get_web_page( $url ){ $options = array( 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 => 10, // stop after 10 redirects ); $ch = curl_init( $url ); curl_setopt_array( $ch, $options ); $content = curl_exec( $ch ); curl_close( $ch ); return $content; }

Now change

$homepage = file_get_contents($site);

in to

$homepage = get_web_page($site);


You should use <strong>/</strong> instead of ****

$homepage = file_get_contents('http://website.com/data');

Also this part

$somecontent = echo $richSnippets;

I don't see $richSnippets above... it's probably not declared?

You probably want to do this:



  • Removes spaces between html tags?
  • How can I create a new expression for use with Thymeleaf and Spring?
  • Why is fread() stopping at a string of 0s?
  • How do I write to a text file in MATLAB?
  • reading off multiple CSV files
  • Transfer from Server > Server > Client without download to server
  • How to process a JPEG binary data in OpenCV?
  • Casting a list of children to a list of parent types
  • Remove 2 vertical borders from HTML table - how?
  • Solr config, encrypting data config file
  • Best way to store an image from a url in php?
  • Pass Arbitrary Sized 2 Dimension Array
  • libcurl compile errors
  • Pass pointer array to function
  • get src attribute of an image based on class or id
  • Serve file to user over http via php
  • HttpServletRequest getLocale returns OS locale not browser locale
  • Security issues with PHP's Readfile method
  • Graphics.CopyFromScreen [Web application] + The handle is invalid
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • how to upload multiple files in c# windows application
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • x64 applications using gdi+: what are the consequences on performance?
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • Django rest serializer Breaks when data exists
  • Typescript - Unable to get 'import' statement to function
  • Get data from AJAX - How to
  • How to install a .deb file on a jailbroken iphone programmatically?
  • Handling un-mapped Rest path
  • How to rebase a series of branches?
  • Is there any way to access browser form field suggestions from JavaScript?
  • Azure Cloud Service Web Role web pages do not load
  • Bug in WPF DataGrid
  • what is the difference between the asp.net mvc application and asp.net web application
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • reshape alternating columns in less time and using less memory