### 配置方法: 1、将文件 - php_curl.dll - libeay32.dll - ssleay32.dll - php5ts.dll 复制到 `%windir%/system32` 以及php 目录的 `ext` 目录下,并且找到`php.ini`(phpinfo里显示的那个php.ini文件),修改了 `extension=php_curl.dll` 并把前面的分号去掉,然后重起APACHE / IIS 这些DLL 5.2.4PHP版本可以在这里找下载:[http://www.blueidea.com/tech/program/2010/7348.asp](http://www.blueidea.com/tech/program/2010/7348.asp "http://www.blueidea.com/tech/program/2010/7348.asp") ### 实用方法: 1、简单使用 // 1. 初始化 $ch = curl_init(); // 2. 设置选项,包括URL curl_setopt($ch, CURLOPT_URL, "http://www.163.com");//http://localhost:7020/del.php?menu=test curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); // 3. 执行并获取HTML文档内容 $output = curl_exec($ch); echo $output; if ($output === FALSE) { echo "cURL Error: " . curl_error($ch);//检测错误 } //打印抓取信息数组 $info = curl_getinfo($ch); foreach($info as $a=>$b) { echo $a."--".$b.""; } // 4. 释放curl句柄 curl_close($ch); 2、修改heade头 // 测试用的URL $urls = array( "http://www.cnn.com", "http://www.mozilla.com" ); //,"http://www.facebook.com" // 测试用的浏览器信息 $browsers = array( "standard" => array ( "user_agent" => "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 (.NET CLR 3.5.30729)", "language" => "en-us,en;q=0.5" ), "iphone" => array ( "user_agent" => "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A537a Safari/419.3", "language" => "en" ), "french" => array ( "user_agent" => "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB6; .NET CLR 2.0.50727)", "language" => "fr,fr-FR;q=0.5" ) ); foreach ($urls as $url) { echo "URL: $url"; foreach ($browsers as $test_name => $browser) { $ch = curl_init(); // 设置 url curl_setopt($ch, CURLOPT_URL, $url); // 设置浏览器的特定header curl_setopt($ch, CURLOPT_HTTPHEADER, array( "User-Agent: {$browser['user_agent']}", "Accept-Language: {$browser['language']}" ) ); // 页面内容我们并不需要 curl_setopt($ch, CURLOPT_NOBODY, 1); // 只需返回HTTP header curl_setopt($ch, CURLOPT_HEADER, 1); // 返回结果,而不是输出它 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); // 有重定向的HTTP头信息吗? if (preg_match("!Location: (.*)!", $output, $matches)) { echo "$test_name: redirects to $matches[1]"; } else { echo "$test_name: no redirection"; } } echo "\n\n"; } 3、POST数据 $url = "http://localhost:7020/del.php?menu=getpost"; $post_data = array ( "foo" => "bar", "query" => "Nettuts", "action" => "Submit" ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 我们在POST数据哦! curl_setopt($ch, CURLOPT_POST, 1); // 把post的变量加上 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); $output = curl_exec($ch); curl_close($ch); echo $output; 4、文件上传 $url = "http://localhost:7020/del.php?menu=getfile"; $post_data = array ( "foo" => "bar", // 要上传的本地文件地址 "upload" => "@C:/1.txt" ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); $output = curl_exec($ch); curl_close($ch); echo $output; 5、同时处理多个curl // 创建两个cURL资源 $ch1 = curl_init(); $ch2 = curl_init(); // 指定URL和适当的参数 curl_setopt($ch1, CURLOPT_URL, "http://www.baidu.com"); curl_setopt($ch1, CURLOPT_HEADER, 0); curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/"); curl_setopt($ch2, CURLOPT_HEADER, 0); // 创建cURL批处理句柄 $mh = curl_multi_init(); // 加上前面两个资源句柄 curl_multi_add_handle($mh,$ch1); curl_multi_add_handle($mh,$ch2); // 预定义一个状态变量 $active = null; // 执行批处理 do { $mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); while ($active && $mrc == CURLM_OK) { if (curl_multi_select($mh) != -1) { do { $mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); } } // 关闭各个句柄 curl_multi_remove_handle($mh, $ch1); curl_multi_remove_handle($mh, $ch2); curl_multi_close($mh); 6、模拟登录 function vlogin($url,$request){ $cookie_jar = tempnam('./tmp','cookie');//在当前目录下生成一个随机文件名的临时文件 $ch = curl_init($url); //初始化curl模块 curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1);//post方式提交 curl_setopt($ch, CURLOPT_POSTFIELDS, $request);//要提交的内容 //把返回$cookie_jar来的cookie信息保存在$cookie_jar文件中 curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar); $xianshi=curl_exec ($ch); curl_close($ch); //get data after login $curl="http://jwc.ecjtu.jx.cn/mis_o/query.php"; $ch=curl_init($curl); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "StuID=20102110130309&Term=2012.1"); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar); $xianshi=curl_exec($ch); curl_close($ch); return $xianshi; } $url="http://jwc.ecjtu.jx.cn/mis_o/login.php";//这个是我们学校教务处的网站,也就是我要爬的网站地址文件。 $request="user=用户名&pass=密码&Submit=提交"; echo vlogin($url, $request)