国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊 PHP的CURL簡單介紹

PHP的CURL簡單介紹

Jun 13, 2016 am 10:42 AM
curl php url one and What introduce use Command Line exist yes of Simple grammar

什么是CURL,PHP與CURL

(PHP CURL)

一 什么是CURL

curl是一個利用URL語法在命令行方式下工作的文件傳輸工具。curl是一個利用URL語法在命令行方式下工作的文件傳輸工具。

它支持很多協(xié)議:FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以及 LDAP。curl同樣支持HTTPS認(rèn)證,HTTP POST方法, HTTP PUT方法, FTP上傳, kerberos認(rèn)證, HTTP上傳, 代理服務(wù)器, cookies, 用戶名/密碼認(rèn)證, 下載文件斷點(diǎn)續(xù)傳,

上載文件斷點(diǎn)續(xù)傳, http代理服務(wù)器管道( proxy tunneling), 甚至它還支持IPv6, socks5代理服務(wù)器, 通過http代理服務(wù)器上傳文件到FTP服務(wù)器等等,功能十分強(qiáng)大。

原來php默認(rèn)并不進(jìn)行此項功能的擴(kuò)展,但還是有的,只是沒有讓它生效罷了。打開PHP安裝目錄,搜索以下三個文件 ssleay32.dll、libeay32.dll和 php_curl.dll,

一一拷貝到系統(tǒng)目錄下的system32文件夾下,修改php.ini文件,找到;extension= php_curl.dll行,去掉前面的;號,保存,重啟服務(wù)器

網(wǎng)站采集,登陸,COOKIE等

二 PHP中的CURL庫

curl_close - 關(guān)閉一個curl會話

curl_copy_handle - 拷貝一個curl連接資源的所有內(nèi)容和參數(shù)

curl_errno - 返回一個包含當(dāng)前會話錯誤信息的數(shù)字編號

curl_error - 返回一個包含當(dāng)前會話錯誤信息的字符串

curl_exec - 執(zhí)行一個curl會話

curl_getinfo - 獲取一個curl連接資源句柄的信息

curl_init - 初始化一個curl會話

curl_multi_add_handle - 向curl批處理會話中添加單獨(dú)的curl句柄資源

curl_multi_close - 關(guān)閉一個批處理句柄資源

curl_multi_exec - 解析一個curl批處理句柄

curl_multi_getcontent - 返回獲取的輸出的文本流

curl_multi_info_read - 獲取當(dāng)前解析的curl的相關(guān)傳輸信息

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 - 以數(shù)組的形式為一個curl設(shè)置會話參數(shù)

curl_setopt - 為一個curl設(shè)置會話參數(shù)

curl_version - 獲取curl相關(guān)的版本信息

curl_init()函數(shù)的作用初始化一個curl會話,curl_init()函數(shù)唯一的一個參數(shù)是可選的,表示一個url地址。

curl_exec()函數(shù)的作用是執(zhí)行一個curl會話,唯一的參數(shù)是curl_init()函數(shù)返回的句柄。

curl_close()函數(shù)的作用是關(guān)閉一個curl會話,唯一的參數(shù)是curl_init()函數(shù)返回的句柄。

三關(guān)于CURL_SETOPT()函數(shù)的設(shè)置

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

curl_setopt()函數(shù)將為一個CURL會話設(shè)置選項。option參數(shù)是你想要的設(shè)置,value是這個選項給定的值。

下列選項的值將被作為長整形使用(在option參數(shù)中指定):

* CURLOPT_INFILESIZE: 當(dāng)你上傳一個文件到遠(yuǎn)程站點(diǎn),這個選項告訴PHP你上傳文件的大小。

* CURLOPT_VERBOSE: 如果你想CURL報告每一件意外的事情,設(shè)置這個選項為一個非零值。

* CURLOPT_HEADER: 如果你想把一個頭包含在輸出中,設(shè)置這個選項為一個非零值。

* CURLOPT_NOPROGRESS: 如果你不會PHP為CURL傳輸顯示一個進(jìn)程條,設(shè)置這個選項為一個非零值。注意:PHP自動設(shè)置這個選項為非零值,你應(yīng)該僅僅為了調(diào)試的目的來改變這個選項。

* CURLOPT_NOBODY: 如果你不想在輸出中包含body部分,設(shè)置這個選項為一個非零值。

* CURLOPT_FAILONERROR: 如果你想讓PHP在發(fā)生錯誤(HTTP代碼返回大于等于300)時,不顯示,設(shè)置這個選項為一人非零值。默認(rèn)行為是返回一個正常頁,忽略代碼。

* CURLOPT_UPLOAD: 如果你想讓PHP為上傳做準(zhǔn)備,設(shè)置這個選項為一個非零值。

* CURLOPT_POST: 如果你想PHP去做一個正規(guī)的HTTP POST,設(shè)置這個選項為一個非零值。這個POST是普通的 application/x-www-from-urlencoded 類型,多數(shù)被HTML表單使用。

* CURLOPT_FTPLISTONLY: 設(shè)置這個選項為非零值,PHP將列出FTP的目錄名列表。

* CURLOPT_FTPAPPEND: 設(shè)置這個選項為一個非零值,PHP將應(yīng)用遠(yuǎn)程文件代替覆蓋它。

* CURLOPT_NETRC: 設(shè)置這個選項為一個非零值,PHP將在你的 ~./netrc 文件中查找你要建立連接的遠(yuǎn)程站點(diǎn)的用戶名及密碼。

* CURLOPT_FOLLOWLOCATION: 設(shè)置這個選項為一個非零值(象 “Location: “)的頭,服務(wù)器會把它當(dāng)做HTTP頭的一部分發(fā)送(注意這是遞歸的,PHP將發(fā)送形如 “Location: “的頭)。

* CURLOPT_PUT: 設(shè)置這個選項為一個非零值去用HTTP上傳一個文件。要上傳這個文件必須設(shè)置CURLOPT_INFILE和CURLOPT_INFILESIZE選項.

* CURLOPT_MUTE: 設(shè)置這個選項為一個非零值,PHP對于CURL函數(shù)將完全沉默。

* CURLOPT_TIMEOUT: 設(shè)置一個長整形數(shù),作為最大延續(xù)多少秒。

* CURLOPT_LOW_SPEED_LIMIT: 設(shè)置一個長整形數(shù),控制傳送多少字節(jié)。

* CURLOPT_LOW_SPEED_TIME: 設(shè)置一個長整形數(shù),控制多少秒傳送CURLOPT_LOW_SPEED_LIMIT規(guī)定的字節(jié)數(shù)。

* CURLOPT_RESUME_FROM: 傳遞一個包含字節(jié)偏移地址的長整形參數(shù),(你想轉(zhuǎn)移到的開始表單)。

* CURLOPT_SSLVERSION: 傳遞一個包含SSL版本的長參數(shù)。默認(rèn)PHP將被它自己努力的確定,在更多的安全中你必須手工設(shè)置。

* CURLOPT_TIMECONDITION: 傳遞一個長參數(shù),指定怎么處理CURLOPT_TIMEVALUE參數(shù)。你可以設(shè)置這個參數(shù)為TIMECOND_IFMODSINCE 或 TIMECOND_ISUNMODSINCE。這僅用于HTTP。

* CURLOPT_TIMEVALUE: 傳遞一個從1970-1-1開始到現(xiàn)在的秒數(shù)。這個時間將被CURLOPT_TIMEVALUE選項作為指定值使用,或被默認(rèn)TIMECOND_IFMODSINCE使用。

下列選項的值將被作為字符串:

* CURLOPT_URL: 這是你想用PHP取回的URL地址。你也可以在用curl_init()函數(shù)初始化時設(shè)置這個選項。

* CURLOPT_USERPWD: 傳遞一個形如[username]:[password]風(fēng)格的字符串,作用PHP去連接。

* CURLOPT_PROXYUSERPWD: 傳遞一個形如[username]:[password] 格式的字符串去連接HTTP代理。

* CURLOPT_RANGE: 傳遞一個你想指定的范圍。它應(yīng)該是”X-Y”格式,X或Y是被除外的。HTTP傳送同樣支持幾個間隔,用逗句來分隔(X-Y,N-M)。

* CURLOPT_POSTFIELDS: 傳遞一個作為HTTP “POST”操作的所有數(shù)據(jù)的字符串。

* CURLOPT_REFERER: 在HTTP請求中包含一個”referer”頭的字符串。

* CURLOPT_USERAGENT: 在HTTP請求中包含一個”user-agent”頭的字符串。

* CURLOPT_FTPPORT: 傳遞一個包含被ftp “POST”指令使用的IP地址。這個POST指令告訴遠(yuǎn)程服務(wù)器去連接我們指定的IP地址。這個字符串可以是一個IP地址,一個主機(jī)名,一個網(wǎng)絡(luò)界面名 (在UNIX下),或是‘-’(使用系統(tǒng)默認(rèn)IP地址)。

* CURLOPT_COOKIE: 傳遞一個包含HTTP cookie的頭連接。

* CURLOPT_SSLCERT: 傳遞一個包含PEM格式證書的字符串。

* CURLOPT_SSLCERTPASSWD: 傳遞一個包含使用CURLOPT_SSLCERT證書必需的密碼。

* CURLOPT_COOKIEFILE: 傳遞一個包含cookie數(shù)據(jù)的文件的名字的字符串。這個cookie文件可以是Netscape格式,或是堆存在文件中的HTTP風(fēng)格的頭。

* CURLOPT_CUSTOMREQUEST: 當(dāng)進(jìn)行HTTP請求時,傳遞一個字符被GET或HEAD使用。為進(jìn)行DELETE或其它操作是有益的,更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. 注意: 在確認(rèn)你的服務(wù)器支持命令先不要去這樣做。下列的選項要求一個文件描述(通過使用fopen()函數(shù)獲得):

* CURLOPT_FILE: 這個文件將是你放置傳送的輸出文件,默認(rèn)是STDOUT.

* CURLOPT_INFILE: 這個文件是你傳送過來的輸入文件。

* CURLOPT_WRITEHEADER: 這個文件寫有你輸出的頭部分。

* CURLOPT_STDERR: 這個文件寫有錯誤而不是stderr。用來獲取需要登錄的頁面的例子,當(dāng)前做法是每次或許都登錄一次,有需要的人再做改進(jìn)了.

四 相關(guān)例子

1 例子
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);??
//把返回來的cookie信息保存在$cookie_jar文件中??
?
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);??
?
//設(shè)定返回的數(shù)據(jù)是否自動顯示??
?
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);??
?
//設(shè)定是否顯示頭信息??
?
curl_setopt($ch, CURLOPT_HEADER, false);??
?
//設(shè)定是否輸出頁面內(nèi)容??
?
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

Statement of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How Do Generators Work in PHP? How Do Generators Work in PHP? Jul 11, 2025 am 03:12 AM

AgeneratorinPHPisamemory-efficientwaytoiterateoverlargedatasetsbyyieldingvaluesoneatatimeinsteadofreturningthemallatonce.1.Generatorsusetheyieldkeywordtoproducevaluesondemand,reducingmemoryusage.2.Theyareusefulforhandlingbigloops,readinglargefiles,or

How to access a character in a string by index in PHP How to access a character in a string by index in PHP Jul 12, 2025 am 03:15 AM

In PHP, you can use square brackets or curly braces to obtain string specific index characters, but square brackets are recommended; the index starts from 0, and the access outside the range returns a null value and cannot be assigned a value; mb_substr is required to handle multi-byte characters. For example: $str="hello";echo$str[0]; output h; and Chinese characters such as mb_substr($str,1,1) need to obtain the correct result; in actual applications, the length of the string should be checked before looping, dynamic strings need to be verified for validity, and multilingual projects recommend using multi-byte security functions uniformly.

How to prevent session hijacking in PHP? How to prevent session hijacking in PHP? Jul 11, 2025 am 03:15 AM

To prevent session hijacking in PHP, the following measures need to be taken: 1. Use HTTPS to encrypt the transmission and set session.cookie_secure=1 in php.ini; 2. Set the security cookie attributes, including httponly, secure and samesite; 3. Call session_regenerate_id(true) when the user logs in or permissions change to change to change the SessionID; 4. Limit the Session life cycle, reasonably configure gc_maxlifetime and record the user's activity time; 5. Prohibit exposing the SessionID to the URL, and set session.use_only

How to URL encode a string in PHP with urlencode How to URL encode a string in PHP with urlencode Jul 11, 2025 am 03:22 AM

The urlencode() function is used to encode strings into URL-safe formats, where non-alphanumeric characters (except -, _, and .) are replaced with a percent sign followed by a two-digit hexadecimal number. For example, spaces are converted to signs, exclamation marks are converted to!, and Chinese characters are converted to their UTF-8 encoding form. When using, only the parameter values ??should be encoded, not the entire URL, to avoid damaging the URL structure. For other parts of the URL, such as path segments, the rawurlencode() function should be used, which converts the space to . When processing array parameters, you can use http_build_query() to automatically encode, or manually call urlencode() on each value to ensure safe transfer of data. just

PHP get the first N characters of a string PHP get the first N characters of a string Jul 11, 2025 am 03:17 AM

You can use substr() or mb_substr() to get the first N characters in PHP. The specific steps are as follows: 1. Use substr($string,0,N) to intercept the first N characters, which is suitable for ASCII characters and is simple and efficient; 2. When processing multi-byte characters (such as Chinese), mb_substr($string,0,N,'UTF-8'), and ensure that mbstring extension is enabled; 3. If the string contains HTML or whitespace characters, you should first use strip_tags() to remove the tags and trim() to clean the spaces, and then intercept them to ensure the results are clean.

PHP get the last N characters of a string PHP get the last N characters of a string Jul 11, 2025 am 03:17 AM

There are two main ways to get the last N characters of a string in PHP: 1. Use the substr() function to intercept through the negative starting position, which is suitable for single-byte characters; 2. Use the mb_substr() function to support multilingual and UTF-8 encoding to avoid truncating non-English characters; 3. Optionally determine whether the string length is sufficient to handle boundary situations; 4. It is not recommended to use strrev() substr() combination method because it is not safe and inefficient for multi-byte characters.

How to set and get session variables in PHP? How to set and get session variables in PHP? Jul 12, 2025 am 03:10 AM

To set and get session variables in PHP, you must first always call session_start() at the top of the script to start the session. 1. When setting session variables, use $_SESSION hyperglobal array to assign values ??to specific keys, such as $_SESSION['username']='john_doe'; it can store strings, numbers, arrays and even objects, but avoid storing too much data to avoid affecting performance. 2. When obtaining session variables, you need to call session_start() first, and then access the $_SESSION array through the key, such as echo$_SESSION['username']; it is recommended to use isset() to check whether the variable exists to avoid errors

How to prevent SQL injection in PHP How to prevent SQL injection in PHP Jul 12, 2025 am 03:02 AM

Key methods to prevent SQL injection in PHP include: 1. Use preprocessing statements (such as PDO or MySQLi) to separate SQL code and data; 2. Turn off simulated preprocessing mode to ensure true preprocessing; 3. Filter and verify user input, such as using is_numeric() and filter_var(); 4. Avoid directly splicing SQL strings and use parameter binding instead; 5. Turn off error display in the production environment and record error logs. These measures comprehensively prevent the risk of SQL injection from mechanisms and details.

See all articles