Heim > php教程 > php手册 > Hauptteil

总结php中 curl 常见的5个实例(附代码)

PHPz
Freigeben: 2018-10-18 14:54:46
nach vorne
1121 Leute haben es durchsucht

我用php ,curl主要是抓取数据,当然我们可以用其他的方法来抓取,比如fsockopen,file_get_contents等。但是只能抓那些能直接访问的页面,如果要抓取有页面访问控制的页面,或者是登录以后的页面就比较困难了。

1,抓取无访问控制文件

<span style="font-family: 微软雅黑,Microsoft YaHei;"><?php  <br/> $ch = curl_init();  <br/> curl_setopt($ch, CURLOPT_URL, "http://localhost/mytest/phpinfo.php");  <br/> curl_setopt($ch, CURLOPT_HEADER, false);  <br/> curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果把这行注释掉的话,就会直接输出  <br/> $result=curl_exec($ch);  <br/> curl_close($ch);  <br/> ?>  <br></span>
Nach dem Login kopieren

2,使用代理进行抓取

为什么要使用代理进行抓取呢?以google为例吧,如果去抓google的数据,短时间内抓的很频繁的话,你就抓取不到了。google对你的ip地址做限制这个时候,你可以换代理重新抓。

<span style="font-family: 微软雅黑,Microsoft YaHei;"><pre name="code" class="php"><?php  <br/> $ch = curl_init();  <br/> curl_setopt($ch, CURLOPT_URL, "http://blog.51yip.com");  <br/> curl_setopt($ch, CURLOPT_HEADER, false);  <br/> curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  <br/> curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);  <br/> curl_setopt($ch, CURLOPT_PROXY, 125.21.23.6:8080);  <br/> //url_setopt($ch, CURLOPT_PROXYUSERPWD, &#39;user:password&#39;);如果要密码的话,加上这个  <br/> $result=curl_exec($ch);  <br/> curl_close($ch);  <br/> ?> <br></span>
Nach dem Login kopieren

3,post数据后,抓取数据

单独说一下数据提交数据,因为用 curl的时候,很多时候会有数据交互的,所以比较重要的。

<span style="font-family: 微软雅黑,Microsoft YaHei;"><?php  <br/> $ch = curl_init();  <br/> /*在这里需要注意的是,要提交的数据不能是二维数组或者更高 <br/> *例如array(&#39;name&#39;=>serialize(array('tank','zhang')),'sex'=>1,'birth'=>'20101010') <br> *例如array('name'=>array('tank','zhang'),'sex'=>1,'birth'=>'20101010')这样会报错的*/  <br> $data = array('name' => 'test', 'sex'=>1,'birth'=>'20101010');  <br> curl_setopt($ch, CURLOPT_URL, 'http://localhost/mytest/curl/upload.php');  <br> curl_setopt($ch, CURLOPT_POST, 1);  <br> curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  <br> curl_exec($ch);  <br> ?>  <br></span>
Nach dem Login kopieren

在 upload.php文件中,print_r($_POST);利用curl就能抓取出upload.php输出的内容Array ( [name] => test [sex] => 1 [birth] => 20101010 )

4,抓取一些有页面访问控制的页面

mmm.jpg


以前写过一篇,页面访问控制的3种方法有兴趣的可以看一下。

如果用上面提到的方法抓的话,会报以下错误

You are not authorized to view this page
You do not have permission to view this directory or page using the credentials that you supplied because your Web browser is sending a WWW-Authenticate header field that the Web server is not configured to accept.

这个时候,我们就要用CURLOPT_USERPWD来进行验证了

<span style="font-family: 微软雅黑,Microsoft YaHei;"><?php  <br/> $ch = curl_init();  <br/> curl_setopt($ch, CURLOPT_URL, "http://club-china");  <br/> /*CURLOPT_USERPWD主要用来破解页面访问控制的 <br/> *例如平时我们所以htpasswd产生页面控制等。*/  <br/> //curl_setopt($ch, CURLOPT_USERPWD, &#39;user:password&#39;);  <br/> curl_setopt($ch, CURLOPT_HTTPGET, 1);  <br/> curl_setopt($ch, CURLOPT_REFERER, "http://club-china");  <br/> curl_setopt($ch, CURLOPT_HEADER, 0);  <br/> $result=curl_exec($ch);  <br/> curl_close($ch);  <br/> ?>  <br></span>
Nach dem Login kopieren

【相关教程推荐】

1. php编程从入门到精通全套视频教程
2. php从入门到精通 
3. bootstrap教程

Verwandte Etiketten:
Quelle:csdn.net
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!