search
HomeBackend DevelopmentPHP TutorialA brief introduction to PHP's CURL_PHP tutorial

A brief introduction to PHP's CURL_PHP tutorial

Jul 13, 2016 pm 05:44 PM
curlphpurloneandWhatintroduceuseCommand LineexistyesofSimplegrammar

什么是CURL,PHP与CURL

(PHP CURL)

一 什么是CURL

curl是一个利用URL语法在命令行方式下工作的文件传输工具。curl是一个利用URL语法在命令行方式下工作的文件传输工具。

它支持很多协议:FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以及 LDAP。curl同样支持HTTPS认证,HTTP POST方法, HTTP PUT方法, FTP上传, kerberos认证, HTTP上传, 代理服务器, cookies, 用户名/密码认证, 下载文件断点续传,

上载文件断点续传, http代理服务器管道( proxy tunneling), 甚至它还支持IPv6, socks5代理服务器, 通过http代理服务器上传文件到FTP服务器等等,功能十分强大。

原来php默认并不进行此项功能的扩展,但还是有的,只是没有让它生效罢了。打开PHP安装目录,搜索以下三个文件 ssleay32.dll、libeay32.dll和 php_curl.dll,

一一拷贝到系统目录下的system32文件夹下,修改php.ini文件,找到;extension= php_curl.dll行,去掉前面的;号,保存,重启服务器

网站采集,登陆,COOKIE等

二 PHP中的CURL库

curl_close - 关闭一个curl会话

curl_copy_handle - 拷贝一个curl连接资源的所有内容和参数

curl_errno - 返回一个包含当前会话错误信息的数字编号

curl_error - 返回一个包含当前会话错误信息的字符串

curl_exec - 执行一个curl会话

curl_getinfo - 获取一个curl连接资源句柄的信息

curl_init - 初始化一个curl会话

curl_multi_add_handle - 向curl批处理会话中添加单独的curl句柄资源

curl_multi_close - 关闭一个批处理句柄资源

curl_multi_exec - 解析一个curl批处理句柄

curl_multi_getcontent - 返回获取的输出的文本流

curl_multi_info_read - 获取当前解析的curl的相关传输信息

curl_multi_init - 初始化一个curl批处理句柄资源

curl_multi_remove_handle - 移除curl批处理句柄资源中的某个句柄资源

curl_multi_select - Get all the sockets associated with the cURL extension, which can then be "selected"

curl_setopt_array - 以数组的形式为一个curl设置会话参数

curl_setopt - 为一个curl设置会话参数

curl_version - 获取curl相关的版本信息

curl_init()函数的作用初始化一个curl会话,curl_init()函数唯一的一个参数是可选的,表示一个url地址。

curl_exec()函数的作用是执行一个curl会话,唯一的参数是curl_init()函数返回的句柄。

curl_close()函数的作用是关闭一个curl会话,唯一的参数是curl_init()函数返回的句柄。

三关于CURL_SETOPT()函数的设置

bool curl_setopt (int ch, string option, mixed value)

curl_setopt()函数将为一个CURL会话设置选项。option参数是你想要的设置,value是这个选项给定的值。

下列选项的值将被作为长整形使用(在option参数中指定):

* CURLOPT_INFILESIZE: 当你上传一个文件到远程站点,这个选项告诉PHP你上传文件的大小。

* CURLOPT_VERBOSE: 如果你想CURL报告每一件意外的事情,设置这个选项为一个非零值。

* CURLOPT_HEADER: 如果你想把一个头包含在输出中,设置这个选项为一个非零值。

* CURLOPT_NOPROGRESS: 如果你不会PHP为CURL传输显示一个进程条,设置这个选项为一个非零值。注意:PHP自动设置这个选项为非零值,你应该仅仅为了调试的目的来改变这个选项。

* CURLOPT_NOBODY: 如果你不想在输出中包含body部分,设置这个选项为一个非零值。

* CURLOPT_FAILONERROR: 如果你想让PHP在发生错误(HTTP代码返回大于等于300)时,不显示,设置这个选项为一人非零值。默认行为是返回一个正常页,忽略代码。

* CURLOPT_UPLOAD: 如果你想让PHP为上传做准备,设置这个选项为一个非零值。

* CURLOPT_POST: 如果你想PHP去做一个正规的HTTP POST,设置这个选项为一个非零值。这个POST是普通的 application/x-www-from-urlencoded 类型,多数被HTML表单使用。

* CURLOPT_FTPLISTONLY: 设置这个选项为非零值,PHP将列出FTP的目录名列表。

* CURLOPT_FTPAPPEND: 设置这个选项为一个非零值,PHP将应用远程文件代替覆盖它。

* CURLOPT_NETRC: 设置这个选项为一个非零值,PHP将在你的 ~./netrc 文件中查找你要建立连接的远程站点的用户名及密码。

* CURLOPT_FOLLOWLOCATION: 设置这个选项为一个非零值(象 “Location: “)的头,服务器会把它当做HTTP头的一部分发送(注意这是递归的,PHP将发送形如 “Location: “的头)。

* CURLOPT_PUT: Set this option to a non-zero value to upload a file using HTTP. To upload this file the CURLOPT_INFILE and CURLOPT_INFILESIZE options must be set.

* CURLOPT_MUTE: Set this option to a non-zero value and PHP will be completely silent for the CURL function.

* CURLOPT_TIMEOUT: Set a long integer as the maximum number of seconds.

* CURLOPT_LOW_SPEED_LIMIT: Set a long integer to control how many bytes are sent.

* CURLOPT_LOW_SPEED_TIME: Set a long integer to control how many seconds to transmit the number of bytes specified by CURLOPT_LOW_SPEED_LIMIT.

* CURLOPT_RESUME_FROM: Pass a long parameter containing the byte offset address, (the start form you want to transfer to).

* CURLOPT_SSLVERSION: Pass a long parameter containing the SSL version. By default PHP will do its own hard work, in more security you have to set it manually.

* CURLOPT_TIMECONDITION: Pass a long parameter to specify how to handle the CURLOPT_TIMEVALUE parameter. You can set this parameter to TIMECOND_IFMODSINCE or TIMECOND_ISUNMODSINCE. This is only for HTTP.

* CURLOPT_TIMEVALUE: Pass a number of seconds from 1970-1-1 to now. This time will be used as the specified value by the CURLOPT_TIMEVALUE option, or by the default TIMECOND_IFMODSINCE.

The values ​​of the following options will be treated as strings:

* CURLOPT_URL: This is the URL address you want to retrieve using PHP. You can also set this option during initialization with the curl_init() function.

* CURLOPT_USERPWD: Pass a string in the form of [username]:[password] and use PHP to connect.

* CURLOPT_PROXYUSERPWD: Pass a string in the format [username]:[password] to connect to the HTTP proxy.

* CURLOPT_RANGE: Pass a range you want to specify. It should be in "X-Y" format, with X or Y being excluded. HTTP transfers also support several intervals, separated by commas (X-Y,N-M).

* CURLOPT_POSTFIELDS: Pass a string containing all the data as an HTTP "POST" operation.

* CURLOPT_REFERER: A string containing a "referer" header in the HTTP request.

* CURLOPT_USERAGENT: A string containing a "user-agent" header in the HTTP request.

* CURLOPT_FTPPORT: Pass an IP address containing the IP address used by the ftp "POST" command. This POST command tells the remote server to connect to the IP address we specified. This string can be an IP address, a host name, a network interface name (under UNIX), or '-' (use the system default IP address).

* CURLOPT_COOKIE: Pass a header connection containing an HTTP cookie.

* CURLOPT_SSLCERT: Pass a string containing the certificate in PEM format.

* CURLOPT_SSLCERTPASSWD: Pass a password containing the necessary password to use the CURLOPT_SSLCERT certificate.

* CURLOPT_COOKIEFILE: Pass a string containing the name of the file containing cookie data. This cookie file can be in Netscape format, or it can be a stack of HTTP-style headers stored in the file.

* CURLOPT_CUSTOMREQUEST: When making an HTTP request, pass a character to be used by GET or HEAD. Pass a string to be used instead of GET or HEAD when doing an HTTP request. This is useful for doing or another, more obscure, HTTP request. Note: Make sure your server supports Order not to do it yet. The following options require a file description (obtained using the fopen() function):

* CURLOPT_FILE: This file will be the output file you place and transmit. The default is STDOUT.

* CURLOPT_INFILE: This file is the input file you sent.

* CURLOPT_WRITEHEADER: This file contains the header part of your output.

* CURLOPT_STDERR: This file has errors written instead of stderr. An example of obtaining a page that requires login. The current method is to log in once every time. People who need it will make improvements.

Four related examples

1 Example
view plaincopy to clipboardprint?

$cookie_jar = tempnam(./tmp,cookie);

$ch = curl_init(); curl_setopt($ch,CURLOPT_URL , http://******);

curl_setopt($ch, CURLOPT_POST, 1);

$request = email_address=&password=&action=;

curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
//Save the returned cookie information in the $cookie_jar file

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);

//Set whether the returned data is automatically displayed

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//Set whether to display header information

curl_setopt( $ch, CURLOPT_HEADER, false);

//Set whether to output page content

curl_setopt($ch, CURLOPT_NOBODY, false);

curl_exec($ch);

curl_close($ch); //get data after login

$ch2 = curl_init();

curl_setopt($ch2, CURLOPT_URL, http://** ***);

curl_setopt($ch2

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478764.htmlTechArticleWhat is CURL, PHP and CURL (PHP CURL) 1. What is CURL curl is a tool that uses URL syntax on the command line File transfer tool that works in . curl is a command line method that uses URL syntax...
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

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

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)