如何在使用php cURL时阻止网址的某些部分?

时间:2011-02-07 02:29:37

标签: php curl

由于带宽问题,我想在远程网址上使用cURL时阻止所有图像。让我举一个简单的例子:一个页面有200个jpg图像,我想得到那个页面没有cURL的200个图像。

2 个答案:

答案 0 :(得分:7)

当cURLing一个URL时,你只收到该URL的内容,这可能只是一个HTML文档。
cURL 会自动下载HTML文档中引用的所有200张图片,因为cURL不关心HTML。恰恰相反;如果您想要下载所有200张图片,您必须手动解析HTML并为每张图片做出进一步的cURL请求。

命令行示例:

$ curl -i www.w3.org
HTTP/1.1 200 OK
Date: Mon, 07 Feb 2011 02:46:36 GMT
Server: Apache/2
Content-Location: Home.html
Vary: negotiate,accept,Accept-Encoding
TCN: choice
Last-Modified: Tue, 01 Feb 2011 20:42:28 GMT
ETag: "74f2-49b3e92157500;89-3f26bd17a2f00"
Accept-Ranges: bytes
Content-Length: 29938
Cache-Control: max-age=600
Expires: Mon, 07 Feb 2011 02:56:36 GMT
P3P: policyref="http://www.w3.org/2001/05/P3P/p3p.xml"
Connection: close
Content-Type: text/html; charset=utf-8

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- Generated from data/head-home.php, ../../smarty/{head.tpl} -->
<head>
<title>World Wide Web Consortium (W3C)</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="Help" href="/Help/" />
<link rel="stylesheet" href="/2008/site/css/minimum" type="text/css" media="handheld, all" />
<style type="text/css" media="print, screen and (min-width: 481px)">
/*<![CDATA[*/
@import url("/2008/site/css/advanced");
/*]]>*/
</style>
<link href="/2008/site/css/minimum" rel="stylesheet" type="text/css" media="handheld, only screen and (max-device-width: 480px)" />
<meta name="viewport" content="width=device-width" />
<link rel="stylesheet" href="/2008/site/css/print" type="text/css" media="print" />
<link rel="shortcut icon" href="/2008/site/images/favicon.ico" type="image/x-icon" />
<meta name="description" content="The World Wide Web Consortium (W3C) is an international community where Member organizations, a full-time staff, and the public work together to develop Web standards." />
<link rel="alternate" type="application/atom+xml" title="W3C News" href="/News/atom.xml" />
</head>
<body id="www-w3-org" class="w3c_public w3c_home">
<div id="w3c_container">
<!-- Generated from data/mast-home.php, ../../smarty/{mast.tpl} -->
<div id="w3c_mast"><!-- #w3c_mast / Page top header -->
<h1 class="logo"><a tabindex="2" accesskey="1" href="/"><img src="/2008/site/images/logo-w3c-mobile-lg" width="90" height="53" alt="W3C" /></a> <span class="alt-logo">W3C</span></h1>
<div id="w3c_nav">

...

这是一个cURL请求。那里有一张图片:<img src="/2008/site/images/logo-w3c-mobile-lg" width="90" height="53" alt="W3C" />。这就是你所得到的一切,你没有得到这个图像。

答案 1 :(得分:0)

如果没有图像你就无法得到它但你可以用正则表达式或dom解析器从结果中轻松地删除它们...但是使用curl,你实际上并没有对图像做出请求,只是html在页面上(所以你要剥离标签)