Fetching and saving the contents of a remote file
Submit a query to a search engine and save the results to a local file
1. Create a form that submits a query to a search engine.
2. Script should automatically save the query results to a file. Each query should be saved to separate files.
Figuring out the url of the remote file/search engine that you will submit your query to:
say you have a query 'lord of war' or 'queen elizabeth,' when you type it into a search engine such as Yahoo or Google, notice that the address of the page contains your query term and it is being submitted to the search engine via the get method: url?more_info_after_question_mark.
If you only look for the variable that equals the query you submitted, you would have a shorter url i.e.
http://search.yahoo.com/search?p=queen%20elizabeth
http://www.google.com/search?q=queen+elizabeth
http://search.yahoo.com/search?p=lord%20of%20war
http://www.google.com/search?q=lord+of+war
So your form gets a query from a user, the form is submitted and you can use the appropriate php file function such as fopen or file_get_contents to get the result of the query submission like so
$url_contents = "http://search.yahoo.com/search?p=".$_POST['query'];
// See PHP: file get contents - Manual note about urlencode
$query_file_content = file_get_contents($url_contents, "decide on file mode") or die("cannot open url");
You then write the contents of the $query_file_content variable to a file using the fwrite function like so
// your form could also ask the user what name to use for the saved file of query results
$file = fopen($_POST['filename'], 'decide on file mode')
fwrite($file, $query_file_content)
fclose($file)
Solution:
1. Create a form that submits a query to a search engine.
HTML Code
<FORM METHOD="POST" ACTION="fetchcontenturl.php"> Enter search term: <INPUT TYPE="text" SIZE="30" NAME="query"> Enter a filename: <input name="filename" type="text" id="filename"> <p> <INPUT TYPE="submit" name="submit" VALUE="Submit"> <INPUT TYPE="reset" VALUE="Clear Form"> </FORM>
2. Script should automatically save the query results to a file. Each query should be saved to separate files.
PHP Code
<?php // submit query to search engine $url_contents = "http://search.yahoo.com/search?p=".urlencode($_POST['query']); $query_file_content = file_get_contents($url_contents, "r") or die("cannot open url"); // save results to a local file // depending on how your server is configured you may need to give queryfiles folder 777 permission // could aslo use another file mode to create file i.e. w+ $file = fopen('queryfiles/'.$_POST['filename'], 'w+'); fwrite ($file, $query_file_content); fclose($file); echo "Your Search Has Been Saved To: <a href=queryfiles/".$_POST['filename'].">".$_POST['filename']."</a>"; ?>