如何使用 cURL 从数组中保存 html 文件


How to use cURL to save html files from array?

我正在尝试使用 cURL 保存一些文件。我已经设法为单个 url 实现了这一点,但是当我创建一个数组以在其中保留多个 url 时,它不起作用。即使在打开错误报告后,我也没有收到任何错误。这些文件根本没有被保存。我的代码在下面给出。任何帮助都非常感谢。

<?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);
}

file_get_contents和file_put_contents如何为您工作?您可以将以下内容合并为一行。

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

或者,我写了一个 curl 包装器类。下面是一些示例用法:

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

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

是操作线。