首页 > 代码库 > curl 抓取图片

curl 抓取图片

/**
     * curl 抓取图片
     * @param $url
     * @return mixed
     */
    public static function downLoadImage($url)
    {
        $header = array(‘Expect:‘);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, ‘GET‘);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        $img = curl_exec($ch);
        curl_close ($ch);

        //$return_code = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
        return $img;
    }


 

foreach ($productData as $val) {
            $serviceParams[‘model_id‘] = $val[‘id‘];
            // 调用逻辑层
            $qrcodeService->getQrcode($serviceParams);
            // 接收逻辑层处理结果
            if (!is_null($qrcodeService->getError())) {
                return $this->setError($qrcodeService->getError());
            }
            $qrcode = CommonLib::downLoadImage($qrcodeService->_data);
            //重试一次
            if (!$qrcode) {
                $qrcode = CommonLib::downLoadImage($qrcodeService->_data);
            }
            //重名重写
            if (isset($name[$val[‘name‘]])) {
                $name[$val[‘name‘]]++;
                $val[‘name‘] = $val[‘name‘] . ‘(‘ . $name[$val[‘name‘]] . ‘)‘;
            }
            $name[$val[‘name‘]] = 0;
            file_put_contents(iconv(‘utf-8‘, ‘gbk‘, $dir . DIRECTORY_SEPARATOR . $val[‘name‘] . ‘.png‘), $qrcode);
        }

 

curl 抓取图片