好友
阅读权限10
听众
最后登录1970-1-1
|
芊雨千寻
发表于 2017-7-6 14:36
本帖最后由 芊雨千寻 于 2017-7-6 14:39 编辑
php 上传图片到imgur.com 代码分享imgur.com 网站限制中国IP上传图片
图床就是将图片上传到别人的网站上,自己网站调用图片,来减小服务器压力,比较适合漫画,图片,还有一些违规网站之类的
$cookie 是存储cookis文件自动创建
$pic_file_url 是上传的图片路径
imgur.com.rar
(1.09 KB, 下载次数: 62)
<?php
$cookie = dirname(__FILE__)."/imgur.com.cookie";
$url='http://imgur.com/upload/checkcaptcha';
$pic_file_url=dirname(__FILE__).'\psb.jpg';
$pic_data = file_get_contents($pic_file_url);
$data=array(
'total_uploads' => '1',
'create_album' => 'true',
);
$header=array(
'Accept:*/*',
'Accept-Encoding:gzip, deflate',
'Accept-Language:zh-CN,zh;q=0.8',
'Connection:keep-alive',
'Content-Length:'.strlen(http_build_query($data)),
'Content-Type:application/x-www-form-urlencoded; charset=UTF-8',
'Host:imgur.com',
'Origin:http://imgur.com',
'Referer:http://imgur.com/upload',
'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
'X-Requested-With:XMLHttpRequest'
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
$tmpInfo = curl_exec($curl);
curl_close($curl);
$tmpInfo = json_decode($tmpInfo,TRUE);
if($tmpInfo['status']!=200){
echo 'err';
exit();
}
echo $tmpInfo['data']['deletehash'].'<br />';
$boundary = '-------------'.uniqid();
$fields = '--'.$boundary."\r\n";
$fields .= 'Content-Disposition: form-data; name="new_album_id"'."\r\n\r\n";
$fields .= $tmpInfo['data']['new_album_id']."\r\n";
$fields .= '--'.$boundary."\r\n";
$fields .= 'Content-Disposition: form-data; name="Filedata"; filename="psb.jpg"'."\r\n";
$fields .= 'Content-Type: image/jpeg'."\r\n\r\n";
$fields .= $pic_data."\r\n";
$fields .= '--'.$boundary.'--'."\r\n";
$header=array(
'Accept:*/*',
'Accept-Encoding:gzip, deflate',
'Accept-Language:zh-CN,zh;q=0.8',
'Connection:keep-alive',
'Content-Length:'.strlen($fields),
'Content-Type:multipart/form-data; boundary='.$boundary,
'Host:imgur.com',
'Origin:http://imgur.com',
'Referer:http://imgur.com/upload',
'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://imgur.com/upload');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$pic_json = curl_exec($ch);
curl_close($ch);
$pic_arr = json_decode($pic_json,TRUE);
//print_r($pic_arr);
if($pic_arr['status']!=200){
echo 'err';
exit();
}
echo 'http://i.imgur.com/'.$pic_arr['data']['hash'].'b'.$pic_arr['data']['ext'].'<br />';
echo 'http://i.imgur.com/'.$pic_arr['data']['hash'].'g'.$pic_arr['data']['ext'].'<br />';
echo 'http://i.imgur.com/'.$pic_arr['data']['hash'].$pic_arr['data']['ext'].'<br />';
|
|