The below PHP code is a slight variation of the above code. It not only downloads the contents of the specified URL but also saves it to a file.
| /** | |
| * Initialize the cURL session | |
| */ | |
| $ch = curl_init(); | |
| /** | |
| * Set the URL of the page or file to download. | |
| */ | |
| curl_setopt($ch, CURLOPT _URL, ‘http://news.google.com/news?hl=en&topic=t& output=rss’); | |
| /** | |
| * Ask cURL to return the contents in a variable instead of simply echoing them to the browser. | |
| */ | |
| curl_setopt($ch, CURLOPT _RETURNTRANSFER, 1); | |
| /** | |
| * Execute the cURL session | |
| */ | |
| $contents = curl_exec ($ch); | |
| /** | |
| * Close cURL session | |
| */ | |
| curl_close ($ch); | |
Here we have used another of the cURL options, CURLOPT_FILE. Obtain a file handler by creating a new file or opening an existing one and then pass this file handler to the curl_set_opt function.
cURL will now write the contents to a file as it downloads a web page or file.
No comments:
Post a Comment