PHP的CURL簡(jiǎn)單介紹
Jun 13, 2016 am 10:42 AM
什么是CURL,PHP與CURL
(PHP CURL)
一 什么是CURL
curl是一個(gè)利用URL語(yǔ)法在命令行方式下工作的文件傳輸工具。curl是一個(gè)利用URL語(yǔ)法在命令行方式下工作的文件傳輸工具。
它支持很多協(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, 用戶(hù)名/密碼認(rèn)證, 下載文件斷點(diǎn)續(xù)傳,
上載文件斷點(diǎn)續(xù)傳, http代理服務(wù)器管道( proxy tunneling), 甚至它還支持IPv6, socks5代理服務(wù)器, 通過(guò)http代理服務(wù)器上傳文件到FTP服務(wù)器等等,功能十分強(qiáng)大。
原來(lái)php默認(rèn)并不進(jìn)行此項(xiàng)功能的擴(kuò)展,但還是有的,只是沒(méi)有讓它生效罷了。打開(kāi)PHP安裝目錄,搜索以下三個(gè)文件 ssleay32.dll、libeay32.dll和 php_curl.dll,
一一拷貝到系統(tǒng)目錄下的system32文件夾下,修改php.ini文件,找到;extension= php_curl.dll行,去掉前面的;號(hào),保存,重啟服務(wù)器
網(wǎng)站采集,登陸,COOKIE等
二 PHP中的CURL庫(kù)
curl_close - 關(guān)閉一個(gè)curl會(huì)話
curl_copy_handle - 拷貝一個(gè)curl連接資源的所有內(nèi)容和參數(shù)
curl_errno - 返回一個(gè)包含當(dāng)前會(huì)話錯(cuò)誤信息的數(shù)字編號(hào)
curl_error - 返回一個(gè)包含當(dāng)前會(huì)話錯(cuò)誤信息的字符串
curl_exec - 執(zhí)行一個(gè)curl會(huì)話
curl_getinfo - 獲取一個(gè)curl連接資源句柄的信息
curl_init - 初始化一個(gè)curl會(huì)話
curl_multi_add_handle - 向curl批處理會(huì)話中添加單獨(dú)的curl句柄資源
curl_multi_close - 關(guān)閉一個(gè)批處理句柄資源
curl_multi_exec - 解析一個(gè)curl批處理句柄
curl_multi_getcontent - 返回獲取的輸出的文本流
curl_multi_info_read - 獲取當(dāng)前解析的curl的相關(guān)傳輸信息
curl_multi_init - 初始化一個(gè)curl批處理句柄資源
curl_multi_remove_handle - 移除curl批處理句柄資源中的某個(gè)句柄資源
curl_multi_select - Get all the sockets associated with the cURL extension, which can then be "selected"
curl_setopt_array - 以數(shù)組的形式為一個(gè)curl設(shè)置會(huì)話參數(shù)
curl_setopt - 為一個(gè)curl設(shè)置會(huì)話參數(shù)
curl_version - 獲取curl相關(guān)的版本信息
curl_init()函數(shù)的作用初始化一個(gè)curl會(huì)話,curl_init()函數(shù)唯一的一個(gè)參數(shù)是可選的,表示一個(gè)url地址。
curl_exec()函數(shù)的作用是執(zhí)行一個(gè)curl會(huì)話,唯一的參數(shù)是curl_init()函數(shù)返回的句柄。
curl_close()函數(shù)的作用是關(guān)閉一個(gè)curl會(huì)話,唯一的參數(shù)是curl_init()函數(shù)返回的句柄。
三關(guān)于CURL_SETOPT()函數(shù)的設(shè)置
bool curl_setopt (int ch, string option, mixed value)
curl_setopt()函數(shù)將為一個(gè)CURL會(huì)話設(shè)置選項(xiàng)。option參數(shù)是你想要的設(shè)置,value是這個(gè)選項(xiàng)給定的值。
下列選項(xiàng)的值將被作為長(zhǎng)整形使用(在option參數(shù)中指定):
* CURLOPT_INFILESIZE: 當(dāng)你上傳一個(gè)文件到遠(yuǎn)程站點(diǎn),這個(gè)選項(xiàng)告訴PHP你上傳文件的大小。
* CURLOPT_VERBOSE: 如果你想CURL報(bào)告每一件意外的事情,設(shè)置這個(gè)選項(xiàng)為一個(gè)非零值。
* CURLOPT_HEADER: 如果你想把一個(gè)頭包含在輸出中,設(shè)置這個(gè)選項(xiàng)為一個(gè)非零值。
* CURLOPT_NOPROGRESS: 如果你不會(huì)PHP為CURL傳輸顯示一個(gè)進(jìn)程條,設(shè)置這個(gè)選項(xiàng)為一個(gè)非零值。注意:PHP自動(dòng)設(shè)置這個(gè)選項(xiàng)為非零值,你應(yīng)該僅僅為了調(diào)試的目的來(lái)改變這個(gè)選項(xiàng)。
* CURLOPT_NOBODY: 如果你不想在輸出中包含body部分,設(shè)置這個(gè)選項(xiàng)為一個(gè)非零值。
* CURLOPT_FAILONERROR: 如果你想讓PHP在發(fā)生錯(cuò)誤(HTTP代碼返回大于等于300)時(shí),不顯示,設(shè)置這個(gè)選項(xiàng)為一人非零值。默認(rèn)行為是返回一個(gè)正常頁(yè),忽略代碼。
* CURLOPT_UPLOAD: 如果你想讓PHP為上傳做準(zhǔn)備,設(shè)置這個(gè)選項(xiàng)為一個(gè)非零值。
* CURLOPT_POST: 如果你想PHP去做一個(gè)正規(guī)的HTTP POST,設(shè)置這個(gè)選項(xiàng)為一個(gè)非零值。這個(gè)POST是普通的 application/x-www-from-urlencoded 類(lèi)型,多數(shù)被HTML表單使用。
* CURLOPT_FTPLISTONLY: 設(shè)置這個(gè)選項(xiàng)為非零值,PHP將列出FTP的目錄名列表。
* CURLOPT_FTPAPPEND: 設(shè)置這個(gè)選項(xiàng)為一個(gè)非零值,PHP將應(yīng)用遠(yuǎn)程文件代替覆蓋它。
* CURLOPT_NETRC: 設(shè)置這個(gè)選項(xiàng)為一個(gè)非零值,PHP將在你的 ~./netrc 文件中查找你要建立連接的遠(yuǎn)程站點(diǎn)的用戶(hù)名及密碼。
* CURLOPT_FOLLOWLOCATION: 設(shè)置這個(gè)選項(xiàng)為一個(gè)非零值(象 “Location: “)的頭,服務(wù)器會(huì)把它當(dāng)做HTTP頭的一部分發(fā)送(注意這是遞歸的,PHP將發(fā)送形如 “Location: “的頭)。
* CURLOPT_PUT: 設(shè)置這個(gè)選項(xiàng)為一個(gè)非零值去用HTTP上傳一個(gè)文件。要上傳這個(gè)文件必須設(shè)置CURLOPT_INFILE和CURLOPT_INFILESIZE選項(xiàng).
* CURLOPT_MUTE: 設(shè)置這個(gè)選項(xiàng)為一個(gè)非零值,PHP對(duì)于CURL函數(shù)將完全沉默。
* CURLOPT_TIMEOUT: 設(shè)置一個(gè)長(zhǎng)整形數(shù),作為最大延續(xù)多少秒。
* CURLOPT_LOW_SPEED_LIMIT: 設(shè)置一個(gè)長(zhǎng)整形數(shù),控制傳送多少字節(jié)。
* CURLOPT_LOW_SPEED_TIME: 設(shè)置一個(gè)長(zhǎng)整形數(shù),控制多少秒傳送CURLOPT_LOW_SPEED_LIMIT規(guī)定的字節(jié)數(shù)。
* CURLOPT_RESUME_FROM: 傳遞一個(gè)包含字節(jié)偏移地址的長(zhǎng)整形參數(shù),(你想轉(zhuǎn)移到的開(kāi)始表單)。
* CURLOPT_SSLVERSION: 傳遞一個(gè)包含SSL版本的長(zhǎng)參數(shù)。默認(rèn)PHP將被它自己努力的確定,在更多的安全中你必須手工設(shè)置。
* CURLOPT_TIMECONDITION: 傳遞一個(gè)長(zhǎng)參數(shù),指定怎么處理CURLOPT_TIMEVALUE參數(shù)。你可以設(shè)置這個(gè)參數(shù)為T(mén)IMECOND_IFMODSINCE 或 TIMECOND_ISUNMODSINCE。這僅用于HTTP。
* CURLOPT_TIMEVALUE: 傳遞一個(gè)從1970-1-1開(kāi)始到現(xiàn)在的秒數(shù)。這個(gè)時(shí)間將被CURLOPT_TIMEVALUE選項(xiàng)作為指定值使用,或被默認(rèn)TIMECOND_IFMODSINCE使用。
下列選項(xiàng)的值將被作為字符串:
* CURLOPT_URL: 這是你想用PHP取回的URL地址。你也可以在用curl_init()函數(shù)初始化時(shí)設(shè)置這個(gè)選項(xiàng)。
* CURLOPT_USERPWD: 傳遞一個(gè)形如[username]:[password]風(fēng)格的字符串,作用PHP去連接。
* CURLOPT_PROXYUSERPWD: 傳遞一個(gè)形如[username]:[password] 格式的字符串去連接HTTP代理。
* CURLOPT_RANGE: 傳遞一個(gè)你想指定的范圍。它應(yīng)該是”X-Y”格式,X或Y是被除外的。HTTP傳送同樣支持幾個(gè)間隔,用逗句來(lái)分隔(X-Y,N-M)。
* CURLOPT_POSTFIELDS: 傳遞一個(gè)作為HTTP “POST”操作的所有數(shù)據(jù)的字符串。
* CURLOPT_REFERER: 在HTTP請(qǐng)求中包含一個(gè)”referer”頭的字符串。
* CURLOPT_USERAGENT: 在HTTP請(qǐng)求中包含一個(gè)”user-agent”頭的字符串。
* CURLOPT_FTPPORT: 傳遞一個(gè)包含被ftp “POST”指令使用的IP地址。這個(gè)POST指令告訴遠(yuǎn)程服務(wù)器去連接我們指定的IP地址。這個(gè)字符串可以是一個(gè)IP地址,一個(gè)主機(jī)名,一個(gè)網(wǎng)絡(luò)界面名 (在UNIX下),或是‘-’(使用系統(tǒng)默認(rèn)IP地址)。
* CURLOPT_COOKIE: 傳遞一個(gè)包含HTTP cookie的頭連接。
* CURLOPT_SSLCERT: 傳遞一個(gè)包含PEM格式證書(shū)的字符串。
* CURLOPT_SSLCERTPASSWD: 傳遞一個(gè)包含使用CURLOPT_SSLCERT證書(shū)必需的密碼。
* CURLOPT_COOKIEFILE: 傳遞一個(gè)包含cookie數(shù)據(jù)的文件的名字的字符串。這個(gè)cookie文件可以是Netscape格式,或是堆存在文件中的HTTP風(fēng)格的頭。
* CURLOPT_CUSTOMREQUEST: 當(dāng)進(jìn)行HTTP請(qǐng)求時(shí),傳遞一個(gè)字符被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ù)器支持命令先不要去這樣做。下列的選項(xiàng)要求一個(gè)文件描述(通過(guò)使用fopen()函數(shù)獲得):
* CURLOPT_FILE: 這個(gè)文件將是你放置傳送的輸出文件,默認(rèn)是STDOUT.
* CURLOPT_INFILE: 這個(gè)文件是你傳送過(guò)來(lái)的輸入文件。
* CURLOPT_WRITEHEADER: 這個(gè)文件寫(xiě)有你輸出的頭部分。
* CURLOPT_STDERR: 這個(gè)文件寫(xiě)有錯(cuò)誤而不是stderr。用來(lái)獲取需要登錄的頁(yè)面的例子,當(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);??
//把返回來(lái)的cookie信息保存在$cookie_jar文件中??
?
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);??
?
//設(shè)定返回的數(shù)據(jù)是否自動(dòng)顯示??
?
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);??
?
//設(shè)定是否顯示頭信息??
?
curl_setopt($ch, CURLOPT_HEADER, false);??
?
//設(shè)定是否輸出頁(yè)面內(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

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

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

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

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

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.

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.

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

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.
