douiwn6941 2019-07-15 06:15
浏览 173

如何跳过http状态404或401的图像?

I am trying to save images from dropbox to my server. I have already done the code for that. I am also checking the HTTP status of the url before downloading the images with code. I am getting proper status but the issue is it always throws an error 401 even when i have put a condition to download images with status code 200 or 302. I am having the links of images in an excel file that i am reading with a laravel package. Please help me.

Image Download Function

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,
        ]);
    }
}

Image Status Check Function

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;
}
  • 写回答

2条回答 默认 最新

  • douzheng1853 2019-07-15 06:44
    关注

    Try this:

    $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. */
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等