分享【php imgur国外图床】imgur.com限制国内IP上传图片
本帖最后由 芊雨千寻 于 2017-7-6 14:39 编辑php 上传图片到imgur.com 代码分享imgur.com 网站限制中国IP上传图片
图床就是将图片上传到别人的网站上,自己网站调用图片,来减小服务器压力,比较适合漫画,图片,还有一些违规网站之类的
$cookie是存储cookis文件自动创建
$pic_file_url 是上传的图片路径
<?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 />';
咚次哒次 发表于 2017-7-6 14:42
重要的资料放别人网站,感觉没什么保障啊
当然本地也要有备份了,图片网站存在别人网站 还有CDN有很大用途的呢 芊雨千寻 发表于 2017-7-6 16:10
当然本地也要有备份了,图片网站存在别人网站 还有CDN有很大用途的呢
感觉有点明白了,愿闻其详 专业 就是借调存储 重要的资料放别人网站,感觉没什么保障啊 看了一下 感觉自己PHP白学了 protea_ban 发表于 2017-7-6 15:43
看了一下 感觉自己PHP白学了
这个真的没啥难度 ,我也是一个小白学了就很少用 偶尔写 这个思路确实可以啊。。
页:
[1]
2