<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://localhost/mytest/phpinfo.php"); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //若是把這行註釋掉的話,就會直接輸出 $result=curl_exec($ch); curl_close($ch); ?>
爲何要使用代理進行抓取呢?以google爲例吧,若是去抓google的數據,短期內抓的很頻繁的話,你就抓取不到了。google對你的ip地址作限制這個時候,你能夠換代理從新抓。php
<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://blog.51yip.com"); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE); curl_setopt($ch, CURLOPT_PROXY, 125.21.23.6:8080); //url_setopt($ch, CURLOPT_PROXYUSERPWD, 'user:password');若是要密碼的話,加上這個 $result=curl_exec($ch); curl_close($ch); ?>
<?php $ch = curl_init(); /*在這裏須要注意的是,要提交的數據不能是二維數組或者更高 *例如array('name'=>serialize(array('tank','zhang')),'sex'=>1,'birth'=>'20101010') *例如array('name'=>array('tank','zhang'),'sex'=>1,'birth'=>'20101010')這樣會報錯的*/ $data = array('name' => 'test', 'sex'=>1,'birth'=>'20101010'); curl_setopt($ch, CURLOPT_URL, 'http://localhost/mytest/curl/upload.php'); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_exec($ch); ?>
在 upload.php文件中,print_r($_POST);利用curl就能抓取出upload.php輸出的內容Array ( [name] => test [sex] => 1 [birth] => 20101010 )html
<?php function checklogin( $user, $password ) { if ( emptyempty( $user ) || emptyempty( $password ) ) { return 0; } $ch = curl_init( ); curl_setopt( $ch, CURLOPT_REFERER, "http://mail.sina.com.cn/index.html" ); curl_setopt( $ch, CURLOPT_HEADER, true ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $ch, CURLOPT_USERAGENT, USERAGENT ); curl_setopt( $ch, CURLOPT_COOKIEJAR, COOKIEJAR ); curl_setopt( $ch, CURLOPT_TIMEOUT, TIMEOUT ); curl_setopt( $ch, CURLOPT_URL, "http://mail.sina.com.cn/cgi-bin/login.cgi" ); curl_setopt( $ch, CURLOPT_POST, true ); curl_setopt( $ch, CURLOPT_POSTFIELDS, "&logintype=uid&u=".urlencode( $user )."&psw=".$password ); $contents = curl_exec( $ch ); curl_close( $ch ); if ( !preg_match( "/Location: (.*)\\/cgi\\/index\\.php\\?check_time=(.*)\n/", $contents, $matches ) ) { return 0; }else{ return 1; } } define( "USERAGENT", $_SERVER['HTTP_USER_AGENT'] ); define( "COOKIEJAR", tempnam( "/tmp", "cookie" ) ); define( "TIMEOUT", 500 ); echo checklogin("zhangying215","xtaj227"); ?>