search

简介:这是PHP CURL库解释的详细页面,介绍了和php,有关的知识、技巧、经验,和一些php源码等。

class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=333673' scrolling='no'>

使用PHP的cURL库可以简单和有效地去抓网页。你只需要运行一个脚本,然后分析一下你所抓取的网页,然后就可以以程序的方式得到你想要的数据 了。无论是你想从从一个链接上取部分数据,或是取一个XML文件并把其导入数据库,那怕就是简单的获取网页内容,cURL 是一个功能强大的PHP库。本文主要讲述如果使用这个PHP库。

启用 cURL 设置
首先,我们得先要确定我们的PHP是否开启了这个库,你可以通过使用php_info()函数来得到这一信息。

phpinfo();

如果你可以在网页上看到下面的输出,那么表示cURL库已被开启。


如果你看到的话,那么你需要设置你的PHP并开启这个库。如果你是在Windows平台下,那么非常简单,你需要改一改你的php.ini文件的设置,找到php_curl.dll,并取消前面的分号注释就行了。如下所示:

//取消下在的注释

extension=php_curl.dll

如果你是在Linux下面,那么,你需要重新编译你的PHP了,编辑时,你需要打开编译参数――在configure命令上加上“?with-curl” 参数。

一个小示例
如果一切就绪,下面是一个小例程:

// 初始化一个 cURL 对象

$curl = curl_init();

// 设置你需要抓取的URL

curl_setopt( $curl , CURLOPT_URL, 'http://coolshell.cn ' );

// 设置header

curl_setopt( $curl , CURLOPT_HEADER, 1);

// 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。

curl_setopt( $curl , CURLOPT_RETURNTRANSFER, 1);

// 运行cURL,请求网页

$data = curl_exec( $curl );

// 关闭URL请求

curl_close( $curl );

// 显示获得的数据

var_dump( $data );

如何POST数据

上面是抓取网页的代码,下面则是向某个网页POST数据。假设我们有一个处理表单的网址http://www.example.com/sendSMS.php ,其可以接受两个表单域,一个是电话号码,一个是短信内容。

$phoneNumber = '13912345678' ;

$message = 'This message was generated by curl and php' ;

$curlPost = 'pNUMBER=' . urlencode( $phoneNumber ) . '&MESSAGE=' . urlencode( $message ) . '&SUBMIT=Send' ;

$ch = curl_init();

curl_setopt( $ch , CURLOPT_URL, 'http://www.example.com/sendSMS.php ' );

curl_setopt( $ch , CURLOPT_HEADER, 1);

curl_setopt( $ch , CURLOPT_RETURNTRANSFER, 1);

curl_setopt( $ch , CURLOPT_POST, 1);

curl_setopt( $ch , CURLOPT_POSTFIELDS, $curlPost );

$data = curl_exec();

curl_close( $ch );

从上面的程序我们可以看到,使用CURLOPT_POST设置HTTP协议的POST方法,而不是GET方法,然后以CURLOPT_POSTFIELDS设置POST的数据。

关于代理服务器

下面是一个如何使用代理服务器的示例。请注意其中高亮的代码,代码很简单,我就不用多说了。

$ch = curl_init();

curl_setopt( $ch , CURLOPT_URL, 'http://www.example.com ' );

curl_setopt( $ch , CURLOPT_HEADER, 1);

curl_setopt( $ch , CURLOPT_RETURNTRANSFER, 1);

curl_setopt( $ch , CURLOPT_HTTPPROXYTUNNEL, 1);

curl_setopt( $ch , CURLOPT_PROXY, 'fakeproxy.com:1080' );

curl_setopt( $ch , CURLOPT_PROXYUSERPWD, 'user:password' );

$data = curl_exec();

curl_close( $ch );

关于SSL和Cookie

关于SSL也就是HTTPS协议,你只需要把CURLOPT_URL连接中的http://变成https://就可以了。当然,还有一个参数叫CURLOPT_SSL_VERIFYHOST可以设置为验证站点。

关于Cookie,你需要了解下面三个参数:

CURLOPT_COOKIE,在当面的会话中设置一个cookie CURLOPT_COOKIEJAR,当会话结束的时候保存一个Cookie CURLOPT_COOKIEFILE,Cookie的文件。

HTTP服务器认证

最后,我们来看一看HTTP服务器认证的情况。

$ch = curl_init();

curl_setopt( $ch , CURLOPT_URL, 'http://www.example.com ' );

curl_setopt( $ch , CURLOPT_RETURNTRANSFER, 1);

curl_setopt( $ch , CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

curl_setopt(CURLOPT_USERPWD, '[username]:[password]' )

$data = curl_exec();

curl_close( $ch );

关于其它更多的内容,请大家参看相关的cURL手册吧。

ZT : http://coolshell.cn/articles/664.html

“PHP CURL库解释”的更多相关文章 》

爱J2EE关注Java迈克尔杰克逊视频站JSON在线工具

http://biancheng.dnbcw.info/php/333673.html pageNo:11
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the difference between unset() and session_destroy()?What is the difference between unset() and session_destroy()?May 04, 2025 am 12:19 AM

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

What is sticky sessions (session affinity) in the context of load balancing?What is sticky sessions (session affinity) in the context of load balancing?May 04, 2025 am 12:16 AM

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

What are the different session save handlers available in PHP?What are the different session save handlers available in PHP?May 04, 2025 am 12:14 AM

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

What is a session in PHP, and why are they used?What is a session in PHP, and why are they used?May 04, 2025 am 12:12 AM

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

Explain the lifecycle of a PHP session.Explain the lifecycle of a PHP session.May 04, 2025 am 12:04 AM

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

What is the difference between absolute and idle session timeouts?What is the difference between absolute and idle session timeouts?May 03, 2025 am 12:21 AM

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

What steps would you take if sessions aren't working on your server?What steps would you take if sessions aren't working on your server?May 03, 2025 am 12:19 AM

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

What is the significance of the session_start() function?What is the significance of the session_start() function?May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Atom editor mac version download

Atom editor mac version download

The most popular open source editor