php curl简单采集图片生成base64编码(并附curl函数参数说明)

许多年前的代码突然拿来一用,特做此笔记(此处的code用来定位内部错误位置,非http code)
<?php 
$url="http://c.hiphotos.baidu.com/image/w%3D210/sign=ed30880babec8a13141a50e1c7029157/d52a2834349b033be1a9503e17ce36d3d539bd35.gif";
function curl_url($url,$type=0,$timeout=30){
	
	$msg = ['code'=>2100,'status'=>'error','msg'=>'未知错误!'];
	$imgs= ['image/jpeg'=>'jpeg',
               'image/jpg'=>'jpg',
               'image/gif'=>'gif',
               'image/png'=>'png',
               'text/html'=>'html',
               'text/plain'=>'txt',
               'image/pjpeg'=>'jpg',
               'image/x-png'=>'png',
               'image/x-icon'=>'ico'
		 ];
	if(!stristr($url,'http')){
		$msg['code']= 2101;
		$msg['msg'] = 'url地址不正确!';	
		return $msg;
	}	
	$dir= pathinfo($url);
	//var_dump($dir);
	$host = $dir['dirname'];
	$refer= $host.'/';
	$ch = curl_init($url);
	curl_setopt ($ch, CURLOPT_REFERER, $refer); //伪造来源地址
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回变量内容还是直接输出字符串,0输出,1返回内容
	curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);//在启用CURLOPT_RETURNTRANSFER的时候,返回原生的(Raw)输出
	curl_setopt($ch, CURLOPT_HEADER, 0); //是否输出HEADER头信息 0否1是
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); //超时时间
	$data = curl_exec($ch);
	//$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE); 
	//$httpContentType = curl_getinfo($ch,CURLINFO_CONTENT_TYPE);
	$info = curl_getinfo($ch);
	curl_close($ch);
	$httpCode = intval($info['http_code']);
	$httpContentType = $info['content_type'];
	$httpSizeDownload= intval($info['size_download']);
	
	if($
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值