Samia Ruponti
Samia Ruponti

Reputation: 4038

How to use cURL to save html files from array?

I'm trying save some files using cURL. I have managed to achieve this for a single url, but when I'm creating an array to keep multiple url's in it, then it is not working. I'm not getting any errors either even after turning on error reporting. The files are simply not being saved. My code in given below. Any help is much appreciated.

<?php
error_reporting(E_ALL);
$var = "http://www.example.com/trailer/";

$urls = array("1423/10_Things_I_Hate_About_You/","1470/10.5__Apocalypse/");

$ch = curl_init();

foreach ($urls as $i => $url) {

$conn[$i]=curl_init($url);
curl_setopt($conn[$i], CURLOPT_URL, $url .$var);
curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($conn[$i], CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
$out = curl_exec($conn[$i]);

// close cURL resource, and free up system resources
curl_close($conn[$i]);

$fp = fopen($url . "index.html", 'w');
fwrite($fp, $out);
fclose($fp);
}

Upvotes: 0

Views: 1435

Answers (1)

Homer6
Homer6

Reputation: 15159

How does file_get_contents and file_put_contents work for you? You could combine the following into a single line.

<?php
    $homepage = file_get_contents( 'http://www.example.com/' );
    file_put_contents( '/my/filename.html', $homepage );
?>

Alternatively, I wrote a curl wrapper class. Here is some sample usage:

https://github.com/homer6/altumo/blob/master/source/php/Http/OutgoingHttpRequest.md

$http_response =  $client->sendAndGetResponseMessage( true );

is the operative line.

Upvotes: 1

Related Questions