<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >CURL, Client URL Library Functions</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="函数参考" HREF="funcref.html"><LINK REL="PREVIOUS" TITLE="ctype_xdigit" HREF="function.ctype-xdigit.html"><LINK REL="NEXT" TITLE="Constants" HREF="curl.constants.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="reference" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.ctype-xdigit.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="curl.constants.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="reference" ><A NAME="ref.curl" ></A ><DIV CLASS="TITLEPAGE" ><H1 CLASS="title" >XVIII. CURL, Client URL Library Functions</H1 ><DIV CLASS="PARTINTRO" ><A NAME="AEN24255" ></A ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="curl.intro" >简介</A ></H1 ><P > PHP supports libcurl, a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP's ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication. </P ><P > These functions have been added in PHP 4.0.2. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="curl.requirements" >需求</A ></H1 ><P > In order to use PHP's cURL functions you need to install the <A HREF="http://curl.haxx.se/" TARGET="_top" >libcurl</A > package. PHP requires that you use libcurl 7.0.2-beta or higher. In PHP 4.2.3, you will need libcurl version 7.9.0 or higher. From PHP 4.3.0, you will need a libcurl version that's 7.9.8 or higher. PHP 5.0.0 requires a libcurl version 7.10.5 or greater. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="curl.installation" >安装</A ></H1 ><P > To use PHP's cURL support you must also compile PHP <CODE CLASS="option" >--with-curl[=DIR]</CODE > where DIR is the location of the directory containing the lib and include directories. In the "include" directory there should be a folder named "curl" which should contain the <TT CLASS="filename" >easy.h</TT > and <TT CLASS="filename" >curl.h</TT > files. There should be a file named <TT CLASS="filename" >libcurl.a</TT > located in the "lib" directory. Beginning with PHP 4.3.0 you can configure PHP to use cURL for URL streams <CODE CLASS="option" >--with-curlwrappers</CODE >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note to Win32 Users: </B > In order to enable this module on a Windows environment, <TT CLASS="filename" >libeay32.dll</TT > and <TT CLASS="filename" >ssleay32.dll</TT > must be present in your PATH. </P ><P > You don't need <TT CLASS="filename" >libcurl.dll</TT > from the cURL site. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="curl.resources" >资源类型</A ></H1 ><P > This extension defines two resource types: a cURL handle and a cURL multi handle. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="AEN24282" >预定义常量</A ></H1 ><P > See also the cURL <A HREF="curl.constants.html" >Predefined Constants</A > </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="curl.examples" >范例</A ></H1 ><P > Once you've compiled PHP with cURL support, you can begin using the cURL functions. The basic idea behind the cURL functions is that you initialize a cURL session using the <A HREF="function.curl-init.html" ><B CLASS="function" >curl_init()</B ></A >, then you can set all your options for the transfer via the <A HREF="function.curl-setopt.html" ><B CLASS="function" >curl_setopt()</B ></A >, then you can execute the session with the <A HREF="function.curl-exec.html" ><B CLASS="function" >curl_exec()</B ></A > and then you finish off your session using the <A HREF="function.curl-close.html" ><B CLASS="function" >curl_close()</B ></A >. Here is an example that uses the cURL functions to fetch the example.com homepage into a file: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN24293" ></A ><P ><B >例 1. Using PHP's cURL module to fetch the example.com homepage</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /><br />$ch </font><font color="#007700">= </font><font color="#0000BB">curl_init</font><font color="#007700">(</font><font color="#DD0000">"http://www.example.com/"</font><font color="#007700">);<br /></font><font color="#0000BB">$fp </font><font color="#007700">= </font><font color="#0000BB">fopen</font><font color="#007700">(</font><font color="#DD0000">"example_homepage.txt"</font><font color="#007700">, </font><font color="#DD0000">"w"</font><font color="#007700">);<br /><br /></font><font color="#0000BB">curl_setopt</font><font color="#007700">(</font><font color="#0000BB">$ch</font><font color="#007700">, </font><font color="#0000BB">CURLOPT_FILE</font><font color="#007700">, </font><font color="#0000BB">$fp</font><font color="#007700">);<br /></font><font color="#0000BB">curl_setopt</font><font color="#007700">(</font><font color="#0000BB">$ch</font><font color="#007700">, </font><font color="#0000BB">CURLOPT_HEADER</font><font color="#007700">, </font><font color="#0000BB">0</font><font color="#007700">);<br /><br /></font><font color="#0000BB">curl_exec</font><font color="#007700">(</font><font color="#0000BB">$ch</font><font color="#007700">);<br /></font><font color="#0000BB">curl_close</font><font color="#007700">(</font><font color="#0000BB">$ch</font><font color="#007700">);<br /></font><font color="#0000BB">fclose</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ></DIV ><DIV CLASS="TOC" ><DL ><DT ><B >目录</B ></DT ><DT ><A HREF="curl.constants.html" >Constants</A > -- Curl Predefined Constants</DT ><DT ><A HREF="function.curl-close.html" >curl_close</A > -- Close a cURL session</DT ><DT ><A HREF="function.curl-copy-handle.html" >curl_copy_handle</A > -- Copy a cURL handle along with all of its preferences</DT ><DT ><A HREF="function.curl-errno.html" >curl_errno</A > -- Return the last error number</DT ><DT ><A HREF="function.curl-error.html" >curl_error</A > -- Return a string containing the last error for the current session</DT ><DT ><A HREF="function.curl-exec.html" >curl_exec</A > -- Perform a cURL session</DT ><DT ><A HREF="function.curl-getinfo.html" >curl_getinfo</A > -- Get information regarding a specific transfer</DT ><DT ><A HREF="function.curl-init.html" >curl_init</A > -- Initialize a cURL session</DT ><DT ><A HREF="function.curl-multi-add-handle.html" >curl_multi_add_handle</A > -- Add a normal cURL handle to a cURL multi handle</DT ><DT ><A HREF="function.curl-multi-close.html" >curl_multi_close</A > -- Close a set of cURL handles</DT ><DT ><A HREF="function.curl-multi-exec.html" >curl_multi_exec</A > -- Run the sub-connections of the current cURL handle</DT ><DT ><A HREF="function.curl-multi-getcontent.html" >curl_multi_getcontent</A > -- Return the content of a cURL handle if <TT CLASS="constant" ><B >CURLOPT_RETURNTRANSFER</B ></TT > is set</DT ><DT ><A HREF="function.curl-multi-info-read.html" >curl_multi_info_read</A > -- Get information about the current transfers</DT ><DT ><A HREF="function.curl-multi-init.html" >curl_multi_init</A > -- Returns a new cURL multi handle</DT ><DT ><A HREF="function.curl-multi-remove-handle.html" >curl_multi_remove_handle</A > -- Remove a multi handle from a set of cURL handles</DT ><DT ><A HREF="function.curl-multi-select.html" >curl_multi_select</A > -- Get all the sockets associated with the cURL extension, which can then be "selected"</DT ><DT ><A HREF="function.curl-setopt-array.html" >curl_setopt_array</A > -- Set multiple options for a cURL transfer</DT ><DT ><A HREF="function.curl-setopt.html" >curl_setopt</A > -- Set an option for a cURL transfer</DT ><DT ><A HREF="function.curl-version.html" >curl_version</A > -- Gets cURL version information</DT ></DL ></DIV ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.ctype-xdigit.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="curl.constants.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >ctype_xdigit</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="funcref.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Constants</TD ></TR ></TABLE ></DIV ></BODY ></HTML >