我想在php中保存来自实时网址的图像。事实上,这些图像是通过调用gmap streetview image api
收集的。在循环中它被调用。那里有近500次迭代。但问题是需要花费太多时间来保存。根据要求,我必须改变它。我也使用过file_get_contents()
和file_put_contents()
以及CURL,但没有任何效果。
那么有什么解决方案吗?任何帮助将不胜感激。
答案 0 :(得分:0)
尝试使用github上提供的Rolling curl类,它将帮助您节省最长时间,因为这是处理卷曲请求的最快方法。 Get the rolling curl git
答案 1 :(得分:0)
<?php
require 'RollingCurl/RollingCurl.php';
require 'RollingCurl/Request.php';
$urls = array('http://img20.360buyimg.com/da/jfs/t3034/220/2054716322/90888/70d760a9/57d1379dN44824334.jpg','http://img12.360buyimg.com/da/jfs/t3103/216/1194453878/182763/7a8b03d5/57c7d7d4N76ea0c7c.jpg','http://img12.360buyimg.com/da/jfs/t3181/286/1455973725/195103/6ee41941/57ce3452Nc20c284f.jpg');
$rollingCurl = new \RollingCurl\RollingCurl();
foreach ($urls as $key => $value) {
# code...
$rollingCurl->get($urls[$key]);
}
$rollingCurl
->setCallback(function(\RollingCurl\Request $request, \RollingCurl\RollingCurl $rollingCurl) {
$saveto = __DIR__.'/images/'.basename($request->getUrl());
if(file_exists($saveto)){
unlink($saveto);
}
$fp = fopen($saveto,'x');
fwrite($fp, $request->getResponseText());
fclose($fp);
})
->execute();
;
?>
答案 2 :(得分:0)
嗨,Partha这里是工作代码使用此
<?php
require 'RollingCurl/RollingCurl.php';
require 'RollingCurl/Request.php';
$urls = array('https://maps.googleapis.com/maps/api/streetview?size=400x400&location=taj%20Mahal&key=AIzaSyDTn9FYuxm3h3jKbEjwViHb7TKaCsXhUxI');
$cnt = 1;
$rollingCurl = new \RollingCurl\RollingCurl();
foreach ($urls as $key => $value) {
# code...
$rollingCurl->get($urls[$key]);
}
$rollingCurl
->setCallback(function(\RollingCurl\Request $request, \RollingCurl\RollingCurl $rollingCurl) {
$saveto = __DIR__."/images/".$GLOBALS['cnt'].".jpeg";
file_put_contents($saveto,$request->getResponseText(),FILE_APPEND|LOCK_EX);
$GLOBALS['cnt']++;
})->execute();
?>