curl 抓取图片
Posted 一个人的孤独自白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 抓取图片的主要内容,如果未能解决你的问题,请参考以下文章