file_get_contents from multiple url(来自多个 url 的 file_get_contents)
本文介绍了来自多个 url 的 file_get_contents的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将页面内容保存到来自多个 url 的文件中.
i want to save page content to files from multiple url.
首先我有来自数组的网站 url
For start i have sites url from array
$site = array(
'url' => 'http://onesite.com/index.php?c='.$row['code0'].'&o='.$row['code1'].'&y='.$row['code2'].'&a='.$row['cod3'].'&sid=', 'selector' => 'table.tabel tr'
);
为了保存文件,我尝试过:
For saveving files I have try:
foreach($site as $n) {
$referer = 'reffername';
$header[] = "Accept: text/xml,application/xml,application/json,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$opts = array('http'=>array('method'=>"GET",
'header'=>implode('
',$header)."
".
"Referer: $referer
",
'user_agent'=> "Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.0.2) Gecko/2008092313 Ubuntu/9.25 (jaunty) Firefox/3.8"));
$context = stream_context_create($opts);
$data = file_get_contents($site["url"], false, $context);
$file = md5('$id');
file_put_contents($file, $data);
$content = unserialize(file_get_contents($file));
}
推荐答案
基本的 cURL 多脚本:
Basic cURL multi script :
// Your URL array that hold links to files
$urls = array();
// cURL multi-handle
$mh = curl_multi_init();
// This will hold cURLS requests for each file
$requests = array();
$options = array(
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_USERAGENT => 'paste your user agent string here',
CURLOPT_HEADER => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true
);
//Corresponding filestream array for each file
$fstreams = array();
$folder = 'content/';
if (!file_exists($folder)){ mkdir($folder, 0777, true); }
foreach ($urls as $key => $url)
{
// Add initialized cURL object to array
$requests[$key] = curl_init($url);
// Set cURL object options
curl_setopt_array($requests[$key], $options);
// Extract filename from URl and create appropriate local path
$path = parse_url($url, PHP_URL_PATH);
$filename = pathinfo($path, PATHINFO_FILENAME).'-'.$key; // Or whatever you want
$filepath = $folder.$filename;
// Open a filestream for each file and assign it to corresponding cURL object
$fstreams[$key] = fopen($filepath, 'w');
curl_setopt($requests[$key], CURLOPT_FILE, $fstreams[$key]);
// Add cURL object to multi-handle
curl_multi_add_handle($mh, $requests[$key]);
}
// Do while all request have been completed
do {
curl_multi_exec($mh, $active);
} while ($active > 0);
// Collect all data here and clean up
foreach ($requests as $key => $request) {
//$returned[$key] = curl_multi_getcontent($request); // Use this if you're not downloading into file, also remove CURLOPT_FILE option and fstreams array
curl_multi_remove_handle($mh, $request); //assuming we're being responsible about our resource management
curl_close($request); //being responsible again. THIS MUST GO AFTER curl_multi_getcontent();
fclose($fstreams[$key]);
}
curl_multi_close($mh);
这篇关于来自多个 url 的 file_get_contents的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:来自多个 url 的 file_get_contents
基础教程推荐
猜你喜欢
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01