本文目录一览:
PHP CURL 获取远程数据下载
这样做肯定是用的你的带宽,是把文件下载到你的服务器上,然后再下载给客户端。
有两条路你可以去试试看,我没做过:一是setcookie指定域名是那个网站,然后转向:
setcookie ($cname ,$cvalue ,$expire ,$path , $host);
header('location: $url");
另外一个方法类似,好像有个P3P可以传递COOKIE,需要你自己查资料:
setcookie ($cname ,$cvalue);
header('P3P: ....');
header('location: $url");
第二个办法应该是可以的,陶宝和开心网都在用这样的技术,陶宝有许多域名,一次登录后都可以使用,就是利用P3P实现的COOKIE传递。
curl获取远程图片时,如何设置本地保存路径?
设置保存路径
define('IMAGE_DIR', 'c:\\xampp\\htdocs\\scraper\\image\\');
保存图片函数。
$imageUrl = 你要的图片的url
$imageType = 你要的图片保存的格式
saveImage($imageUrl, $imageType = 'IMAGETYPE_GIF') {
if (!file_exists(IMAGE_DIR)) {
mkdir(IMAGE_DIR, 0777, true);
}
if( $imageType === IMAGETYPE_JPEG ) {
$fileExt = 'jpg';
} elseif ( $imageType === IMAGETYPE_GIF ) {
$fileExt = 'gif';
} elseif ( $imageType === IMAGETYPE_PNG ) {
$fileExt = 'png';
}
$newImageName = md5($imageUrl). '.' . $fileExt;
$image = new Image();
$image-load($imageUrl);
$image-resizeToWidth(100);
$image-save( IMAGE_DIR . $newImageName, $imageType );
return $newImageName;
}
这是我的图片类,保存前可转换格式,图片大小。
?php
class Image {
private $_image;
private $_imageFormat;
public function load($imageFile) {
$imageInfo = getImageSize($imageFile);
$this-_imageFormat = $imageInfo[2];
if( $this-_imageFormat === IMAGETYPE_JPEG ) {
$this-_image = imagecreatefromjpeg($imageFile);
} elseif( $this-_imageFormat === IMAGETYPE_GIF ) {
$this-_image = imagecreatefromgif($imageFile);
} elseif( $this-_imageFormat === IMAGETYPE_PNG ) {
$this-_image = imagecreatefrompng($imageFile);
}
}
public function save($imageFile, $_imageFormat=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $_imageFormat == IMAGETYPE_JPEG ) {
imagejpeg($this-_image,$imageFile,$compression);
} elseif ( $_imageFormat == IMAGETYPE_GIF ) {
imagegif($this-_image,$imageFile);
} elseif ( $_imageFormat == IMAGETYPE_PNG ) {
imagepng($this-_image,$imageFile);
}
if( $permissions != null) {
chmod($imageFile,$permissions);
}
}
public function getWidth() {
return imagesx($this-_image);
}
public function getHeight() {
return imagesy($this-_image);
}
public function resizeToHeight($height) {
$ratio = $height / $this-getHeight();
$width = $this-getWidth() * $ratio;
$this-resize($width,$height);
}
public function resizeToWidth($width) {
$ratio = $width / $this-getWidth();
$height = $this-getheight() * $ratio;
$this-resize($width,$height);
}
public function scale($scale) {
$width = $this-getWidth() * $scale/100;
$height = $this-getheight() * $scale/100;
$this-resize($width,$height);
}
private function resize($width, $height) {
$newImage = imagecreatetruecolor($width, $height);
imagecopyresampled($newImage, $this-_image, 0, 0, 0, 0, $width, $height, $this-getWidth(), $this-getHeight());
$this-_image = $newImage;
}
}
?
php 用CURL 抓取图片
preg_match('@p\.php\?p=(.*)@Ui', $url, $url);//获取图片地址
if(isset($url[1]))
$url=$url[1];
else
$url='';
if($url):
//curl抓取图片过程
$ch = curl_init();
if (defined('CURLOPT_IPRESOLVE') defined('CURL_IPRESOLVE_V4')) {
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$content = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($info['http_code'] != 200)
$content = NULL;
if($content)//保存图片到本地
@file_put_contents ('存放地址', $content);
endif;