我正在尝试将图像从保管箱保存到我的服务器。我已经完成了代码。在下载带有代码的图像之前,我还检查了URL的HTTP状态。我正在获取正确的状态,但问题是,即使我已经设置了条件来下载状态码为200或302的图像,它也始终会引发错误401。我在使用laravel读取的excel文件中拥有图像的链接包。请帮助我。
图片下载功能
public function ImportImagesProcess(Request $request)
{
$product_type = $request->get('product_type');
$category_id = $request->get('category_id');
$category_level = $request->get('category_level');
$filepath = $request->get('file_path');
$total_rows = $request->get('total_rows');
$start_index = $request->get('start_index');
$row_counter_break = $request->get('row_counter_break');
$data = Excel::load($filepath)->limit(false, ($start_index))->get();
$dataArr = $data->toArray();
$array = array_map('array_filter', $dataArr);
$array = array_filter($array);
if ($start_index > $total_rows) {
return response()->json([
'status' => 'complete',
'product_type' => $product_type,
'category_id' => $category_id,
'category_level' => $category_level,
'file_path' => $filepath,
'total_rows' => $total_rows,
'start_index' => 0,
'row_counter_break' => $row_counter_break,
]);
} else {
$rowCounter = 0;
foreach ($array as $value) {
$image_list = [];
if (!empty($value['image1'])) {
array_push($image_list, $value['image1']);
}
if (!empty($value['image2'])) {
array_push($image_list, $value['image2']);
}
if (!empty($value['image3'])) {
array_push($image_list, $value['image3']);
}
if (!empty($value['image4'])) {
array_push($image_list, $value['image4']);
}
if (!empty($value['image5'])) {
array_push($image_list, $value['image5']);
}
foreach ($image_list as $il) {
if ($rowCounter >= $row_counter_break)
break;
$status = self::CheckLinkStatus($il);
if ($status == 200) {
if (strpos($il, "?dl=0") !== false) {
$image_url = str_replace("?dl=0", "", $il);
$image_url = str_replace("www.dropbox.com", "content.dropboxapi.com", $il);
$info = pathinfo($image_url);
$contents = file_get_contents($image_url, true);
$file = $info['basename'];
file_put_contents(public_path('datauploads') . "/" . $file, $contents);
} else {
$img_status = self::CheckLinkStatus($il);
if ($img_status !== 404 && $img_status !== 401) {
$image_url = str_replace("www.dropbox.com", "content.dropboxapi.com", $il);
$info = pathinfo($image_url);
$contents = file_get_contents($image_url, true);
$file = $info['basename'];
file_put_contents(public_path('datauploads') . "/" . $file, $contents);
}
}
}
}
$rowCounter++;
}
return response()->json([
'status' => 'success',
'product_type' => $product_type,
'category_id' => $category_id,
'category_level' => $category_level,
'file_path' => $filepath,
'total_rows' => $total_rows,
'start_index' => $start_index,
'row_counter_break' => $row_counter_break,
]);
}
}
图像状态检查功能
public static function CheckLinkStatus($image)
{
$ch = curl_init($image);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $retcode;
}
答案 0 :(得分:1)
尝试一下:
$ch = curl_init($image);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($httpCode != 404) //# if error code is not "404"
{
/* Your Function here. */
}
else
{
/* Handle 404 here. */
}
答案 1 :(得分:0)
尝试设置HTTP代码数组,例如:
$httpCodes = [401,404];
不仅仅是检查:
if(!in_array(CheckLinkStatus($image),$httpCodes){
// YOur logic
} else {
// Handle 401 && 404
}