基于PHP的cURL快速入門(mén)二
Jun 13, 2016 pm 01:03 PM
基于PHP的cURL快速入門(mén)2
?
用POST方法發(fā)送數(shù)據(jù)
當(dāng)發(fā)起GET請(qǐng)求時(shí),數(shù)據(jù)可以通過(guò)“查詢(xún)字串”(query string)傳遞給一個(gè)URL。例如,在google中搜索時(shí),搜索關(guān)鍵即為URL的查詢(xún)字串的一部分:
http://www.google.com/search?q=nettuts
這種情況下你可能并不需要cURL來(lái)模擬。把這個(gè)URL丟給“file_get_contents()”就能得到相同結(jié)果。
不過(guò)有一些HTML表單是用POST方法提交的。這種表單提交時(shí),數(shù)據(jù)是通過(guò) HTTP請(qǐng)求體(request body) 發(fā)送,而不是查詢(xún)字串。例如,當(dāng)使用CodeIgniter論壇的表單,無(wú)論你輸入什么關(guān)鍵字,總是被POST到如下頁(yè)面:
http://codeigniter.com/forums/do_search/
你可以用PHP腳本來(lái)模擬這種URL請(qǐng)求。首先,新建一個(gè)可以接受并顯示POST數(shù)據(jù)的文件,我們給它命名為post_output.php:
print_r($_POST);?
接下來(lái),寫(xiě)一段PHP腳本來(lái)執(zhí)行cURL請(qǐng)求:
以下為引用的內(nèi)容:
$url = "http://localhost/post_output.php"; $post_data = array ( "foo" => "bar", "query" => "Nettuts", "action" => "Submit" ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 我們?cè)赑OST數(shù)據(jù)哦! curl_setopt($ch, CURLOPT_POST, 1); // 把post的變量加上 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); $output = curl_exec($ch); curl_close($ch); echo $output;?
執(zhí)行代碼后應(yīng)該會(huì)得到以下結(jié)果:

這段腳本發(fā)送一個(gè)POST請(qǐng)求給 post_output.php ,這個(gè)頁(yè)面 $_POST 變量并返回,我們利用cURL捕捉了這個(gè)輸出。
文件上傳
上傳文件和前面的POST十分相似。因?yàn)樗械奈募蟼鞅韱味际峭ㄟ^(guò)POST方法提交的。
首先新建一個(gè)接收文件的頁(yè)面,命名為 upload_output.php:
print_r($_FILES);
以下是真正執(zhí)行文件上傳任務(wù)的腳本:
以下為引用的內(nèi)容:
$url = "http://localhost/upload_output.php"; $post_data = array ( "foo" => "bar", // 要上傳的本地文件地址 "upload" => "@C:/wamp/www/test.zip" ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); $output = curl_exec($ch); curl_close($ch); echo $output;?
如果你需要上傳一個(gè)文件,只需要把文件路徑像一個(gè)post變量一樣傳過(guò)去,不過(guò)記得在前面加上@符號(hào)。執(zhí)行這段腳本應(yīng)該會(huì)得到如下輸出:

cURL批處理(multi cURL)
cURL還有一個(gè)高級(jí)特性――批處理句柄(handle)。這一特性允許你同時(shí)或異步地打開(kāi)多個(gè)URL連接。
下面是來(lái)自來(lái)自php.net的示例代碼:
以下為引用的內(nèi)容:
// 創(chuàng)建兩個(gè)cURL資源 $ch1 = curl_init(); $ch2 = curl_init(); // 指定URL和適當(dāng)?shù)膮?shù) curl_setopt($ch1, CURLOPT_URL, "http://lxr.php.net/"); curl_setopt($ch1, CURLOPT_HEADER, 0); curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/"); curl_setopt($ch2, CURLOPT_HEADER, 0); // 創(chuàng)建cURL批處理句柄 $mh = curl_multi_init(); // 加上前面兩個(gè)資源句柄 curl_multi_add_handle($mh,$ch1); curl_multi_add_handle($mh,$ch2); // 預(yù)定義一個(gè)狀態(tài)變量 $active = null; // 執(zhí)行批處理 do { $mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); while ($active && $mrc == CURLM_OK) { if (curl_multi_select($mh) != -1) { do { $mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); } } // 關(guān)閉各個(gè)句柄 curl_multi_remove_handle($mh, $ch1); curl_multi_remove_handle($mh, $ch2); curl_multi_close($mh);
?
這里要做的就是打開(kāi)多個(gè)cURL句柄并指派給一個(gè)批處理句柄。然后你就只需在一個(gè)while循環(huán)里等它執(zhí)行完畢。
這個(gè)示例中有兩個(gè)主要循環(huán)。第一個(gè) do-while 循環(huán)重復(fù)調(diào)用 curl_multi_exec() 。這個(gè)函數(shù)是無(wú)隔斷(non-blocking)的,但會(huì)盡可能少地執(zhí)行。它返回一個(gè)狀態(tài)值,只要這個(gè)值等于常量 CURLM_CALL_MULTI_PERFORM ,就代表還有一些刻不容緩的工作要做(例如,把對(duì)應(yīng)URL的http頭信息發(fā)送出去)。也就是說(shuō),我們需要不斷調(diào)用該函數(shù),直到返回值發(fā)生改變。
而接下來(lái)的 while 循環(huán),只在 $active 變量為 true 時(shí)繼續(xù)。這一變量之前作為第二個(gè)參數(shù)傳給了 curl_multi_exec() ,代表只要批處理句柄中是否還有活動(dòng)連接。接著,我們調(diào)用 curl_multi_select() ,在活動(dòng)連接(例如接受服務(wù)器響應(yīng))出現(xiàn)之前,它都是被“屏蔽”的。這個(gè)函數(shù)成功執(zhí)行后,我們又會(huì)進(jìn)入另一個(gè) do-while 循環(huán),繼續(xù)下一條URL。
還是來(lái)看一看怎么把這一功能用到實(shí)處吧:
WordPress 連接檢查器
想象一下你有一個(gè)文章數(shù)目龐大的博客,這些文章中包含了大量外部網(wǎng)站鏈接。一段時(shí)間之后,因?yàn)檫@樣那樣的原因,這些鏈接中相當(dāng)數(shù)量都失效了。要么是被和諧了,要么是整個(gè)站點(diǎn)都被功夫網(wǎng)了...
我們下面建立一個(gè)腳本,分析所有這些鏈接,找出打不開(kāi)或者404的網(wǎng)站/網(wǎng)頁(yè),并生成一個(gè)報(bào)告。
請(qǐng)注意,以下并不是一個(gè)真正可用的WordPress插件,僅僅是一段獨(dú)立功能的腳本而已,僅供演示,謝謝。
好,開(kāi)始吧。首先,從數(shù)據(jù)庫(kù)中讀取所有這些鏈接:
以下為引用的內(nèi)容:
// CONFIG $db_host = 'localhost'; $db_user = 'root'; $db_pass = ''; $db_name = 'wordpress'; $excluded_domains = array( 'localhost', 'www.mydomain.com'); $max_connections = 10; // 初始化一些變量 $url_list = array(); $working_urls = array(); $dead_urls = array(); $not_found_urls = array(); $active = null; // 連到 MySQL if (!mysql_connect($db_host, $db_user, $db_pass)) { die('Could not connect: ' . mysql_error()); } if (!mysql_select_db($db_name)) { die('Could not select db: ' . mysql_error()); } // 找出所有含有鏈接的文章 $q = "SELECT post_content FROM wp_posts WHERE post_content LIKE '%href=%' AND post_status = 'publish' AND post_type = 'post'"; $r = mysql_query($q) or die(mysql_error()); while ($d = mysql_fetch_assoc($r)) { // 用正則匹配鏈接 if (preg_match_all("!href=\"(.*?)\"!", $d['post_content'], $matches)) { foreach ($matches[1] as $url) { // exclude some domains $tmp = parse_url($url); if (in_array($tmp['host'], $excluded_domains)) { continue; } // store the url $url_list []= $url; } } } // 移除重復(fù)鏈接 $url_list = array_values(array_unique($url_list)); if (!$url_list) { die('No URL to check'); }
?
我們首先配置好數(shù)據(jù)庫(kù),一系列要排除的域名($excluded_domains),以及最大并發(fā)連接數(shù)($max_connections)。然后,連接數(shù)據(jù)庫(kù),獲取文章和包含的鏈接,把它們收集到一個(gè)數(shù)組中($url_list)。

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)

The reason for the error is NameResolutionError(self.host,self,e)frome, which is an exception type in the urllib3 library. The reason for this error is that DNS resolution failed, that is, the host name or IP address attempted to be resolved cannot be found. This may be caused by the entered URL address being incorrect or the DNS server being temporarily unavailable. How to solve this error There may be several ways to solve this error: Check whether the entered URL address is correct and make sure it is accessible Make sure the DNS server is available, you can try using the "ping" command on the command line to test whether the DNS server is available Try accessing the website using the IP address instead of the hostname if behind a proxy

To update the curl version under Linux, you can follow the steps below: Check the current curl version: First, you need to determine the curl version installed in the current system. Open a terminal and execute the following command: curl --version This command will display the current curl version information. Confirm available curl version: Before updating curl, you need to confirm the latest version available. You can visit curl's official website (curl.haxx.se) or related software sources to find the latest version of curl. Download the curl source code: Using curl or a browser, download the source code file for the curl version of your choice (usually .tar.gz or .tar.bz2

PHP function introduction—get_headers(): Overview of obtaining the response header information of the URL: In PHP development, we often need to obtain the response header information of the web page or remote resource. The PHP function get_headers() can easily obtain the response header information of the target URL and return it in the form of an array. This article will introduce the usage of get_headers() function and provide some related code examples. Usage of get_headers() function: get_header

PHP8.1 released: Introducing curl for concurrent processing of multiple requests. Recently, PHP officially released the latest version of PHP8.1, which introduced an important feature: curl for concurrent processing of multiple requests. This new feature provides developers with a more efficient and flexible way to handle multiple HTTP requests, greatly improving performance and user experience. In previous versions, handling multiple requests often required creating multiple curl resources and using loops to send and receive data respectively. Although this method can achieve the purpose

Differences: 1. Different definitions, url is a uniform resource locator, and html is a hypertext markup language; 2. There can be many urls in an html, but only one html page can exist in a url; 3. html refers to is a web page, and url refers to the website address.

From start to finish: How to use php extension cURL for HTTP requests Introduction: In web development, it is often necessary to communicate with third-party APIs or other remote servers. Using cURL to make HTTP requests is a common and powerful way. This article will introduce how to use PHP to extend cURL to perform HTTP requests, and provide some practical code examples. 1. Preparation First, make sure that php has the cURL extension installed. You can execute php-m|grepcurl on the command line to check

How to handle 301 redirection of web pages in PHPCurl? When using PHPCurl to send network requests, you will often encounter a 301 status code returned by the web page, indicating that the page has been permanently redirected. In order to handle this situation correctly, we need to add some specific options and processing logic to the Curl request. The following will introduce in detail how to handle 301 redirection of web pages in PHPCurl, and provide specific code examples. 301 redirect processing principle 301 redirect means that the server returns a 30

1. Java calls post interface 1. Use URLConnection or HttpURLConnection that comes with java. There is no need to download other jar packages. Call URLConnection. If the interface response code is modified by the server, the return message cannot be received. It can only be received when the response code is correct. to return publicstaticStringsendPost(Stringurl,Stringparam){OutputStreamWriterout=null;BufferedReaderin=null;StringBuilderresult=newSt
