PHP-based Curl QuickStart 2
?
Send data using the Post method
When a GET request is initiated, the data can be passed to a URL through the query string. For example, when searching in Google, the search key is part of the query string for the URL:
Http://www.google.com/search?q=nettuts
In this case you may not need curl to simulate. Throw this URL to "file_get_contents ()" To get the same result.
However, some HTML forms are submitted using the Post method. When this form is submitted, the data is sent over the HTTP request body, not the query string. For example, when using the CodeIgniter forum form, no matter what keyword you enter, always post to the following page:
http://codeigniter.com/forums/do_search/
You can use PHP scripts to simulate this URL request. First, create a new file that can accept and display the post data, and we'll name it post_output.php:
Print_r ($_post);
?
Next, write a PHP script to perform the Curl request:
The following is the referenced content:
$url = "http://localhost/post_output.php"; $post _data = Array ("foo" = "bar", "query" = "Nettuts", "action" = "S Ubmit "); $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_returntransfer, 1);// We're in post data Oh! curl_setopt ($ch, Curlopt_post, 1);//Add the POST variables curl_setopt ($ch, Curlopt_postfields, $post _data); $output = Curl_exec ($ CH); Curl_close ($ch); Echo $output;
?
After executing the code, you should get the following results:
This script sends a POST request to post_output.php, this page $_post the variable and returns, and we use curl to capture the output.
File Upload
The upload file is very similar to the previous post. Because all file upload forms are submitted via the Post method.
First create a new page to receive the file, named upload_output.php:
Print_r ($_files);
Here's a script that really performs the file upload task:
The following is the referenced content:
$url = "http://localhost/upload_output.php"; $post _data = Array ("foo" = "bar",//local file address to be uploaded "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;
?
If you need to upload a file, just pass the file path like a post variable, [email protected]?? To the following output:
Curl Batch processing (Multi Curl)
Curl also has an advanced feature-the batch handle (handle). This feature allows you to open multiple URL connections simultaneously or asynchronously.
The following is a sample code from Php.net:
The following is the referenced content:
Create two curl Resources $ch1 = Curl_init (); $ch 2 = Curl_init ();//specify URL and appropriate parameters curl_setopt ($ch 1, Curlopt_url, "http://lxr.php.net/") ; curl_setopt ($ch 1, curlopt_header, 0), curl_setopt ($ch 2, Curlopt_url, "http://www.php.net/"), curl_setopt ($ch 2, Curlopt_header, 0);//Create curl Batch Handle $MH = Curl_multi_init ();//Add the preceding two resource handle Curl_multi_add_handle ($MH, $ch 1); curl_multi_ Add_handle ($MH, $ch 2);//predefined a state variable $active = null;//Execute batch do {$MRC = Curl_multi_exec ($MH, $active);} while ($MRC = = Curlm_ca Ll_multi_perform) while ($active && $MRC = = CURLM_OK) {if (Curl_multi_select ($MH)! =-1) {do {$MRC = CURL_MULTI_EX EC ($MH, $active);} while ($MRC = = Curlm_call_multi_perform);}} Close each handle curl_multi_remove_handle ($MH, $ch 1); Curl_multi_remove_handle ($MH, $ch 2); Curl_multi_close ($MH);
?
To do this is to open multiple curl handles and assign them to a batch handle. Then you just wait for it to execute in a while loop.
There are two main loops in this example. The first do-while loop repeats the invocation of Curl_multi_exec (). This function is no partition (non-blocking), but will be executed as little as possible. It returns a status value, as long as the value equals the constant Curlm_call_multi_perform, there is still some urgent work to do (for example, sending the HTTP header information for the corresponding URL). That is, we need to constantly call this function until the return value changes.
and the next while loop, only continues when the $active variable is true. This variable is passed as the second argument to Curl_multi_exec (), which represents whether there is an active connection in the batch handle as well. Next, we call Curl_multi_select (), which is "masked" until the active connection (for example, the Accept server response) appears. Once this function executes successfully, we go to another do-while loop and continue to the next URL.
Let's take a look at how to put this into practice:
WordPress Connection Checker
Imagine that you have a blog with a large number of articles that contain links to external websites. After a while, the amount of these links is invalidated for such reasons. Either the harmony, or the entire site has been the Kung Fu network ...
We create a script below, analyze all these links, find out which sites/pages are open or 404, and generate a report.
Note that the following is not a really usable WordPress plugin, just a standalone script, just for demonstration, thank you.
OK, let's get started. First, read all these links from the database:
The following is the referenced content:
Config$db_host = ' localhost '; $db _user = ' root '; $db _pass = '; $db _name = ' WordPress '; $excluded _domains = Array (' Localho St ', ' www.mydomain.com '); $max _connections = 10;//initializes some variables $url_list = array (), $working _urls = Array (), $dead _urls = Array ( ), $not _found_urls = Array (), $active = null;//to Mysqlif (!mysql_connect ($db _host, $db _user, $db _pass)) {die (' Could not Co Nnect: '. Mysql_error ());} if (!mysql_select_db ($db _name)) {die (' Could not select DB: '. mysql_error ());} Find all articles containing links $q = "Select Post_content from Wp_postswhere 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)) {//Use regular match link 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;}}} Remove duplicate link $url_list = Array_values (Array_unique ($url _list)); (! $url _list) {die (' No URL to check ');}
?
We first configure the database, a series of domain names to exclude ($excluded _domains), and the maximum number of concurrent connections ($max _connections). Then, connect to the database, get the articles and the included links, and collect them into an array ($url _list).