码迷,mamicode.com
首页 > Web开发 > 详细

curl 抓取图片

时间:2017-05-15 19:41:56      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:iss   ssl   utf-8   logs   error   dir   class   nload   highlight   

/**
     * 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 抓取图片

标签:iss   ssl   utf-8   logs   error   dir   class   nload   highlight   

原文地址:http://www.cnblogs.com/-mrl/p/6857610.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!