Curl Learning and application (with multithreaded implementations) _php tutorial

Source: Internet
Author: User
Tags urlencode ssl certificate
Curl Installation:

installation under Windows
: Modify the settings of the php.ini file to find the Php_curl.dll
Cancel the comment under the Extension=php_curl.dll
install under Linux:
Copy CodeThe code is as follows:
# wget http://curl.haxx.se/download/curl-7.17.1.tar.gz
# tar ZXVF curl-7.17.1.tar.gz//Unzip
#cd curl-7.17.1
#./configure–prefix=/usr/local/curl
# make
# make Install


This is the method that was installed before installing PHP.
Phpinf to see if the load was successful!
Post data fetion interface using Curl
Using curl to write the Fetion interface, there are many online, here just to do a test
Copy CodeThe code is as follows:
$username = 13800138000;
$password = 123456;
$sendto = 13912345678;
$message = "Test a try! ";
$curlPost = ' username= '. UrlEncode ($username). ' &password= '. UrlEncode ($password). ' &sendto= '. UrlEncode ($sendto). ' &message= '. UrlEncode ($message). ";
$ch = Curl_init ();//Initialize Curl
curl_setopt ($ch, Curlopt_url, ' http://sms.api.bz/fetion.php ');//crawl specified Web page
curl_setopt ($ch, Curlopt_header, 0);//Set HEADER
curl_setopt ($ch, Curlopt_returntransfer, 1);//requires the result to be a string and output to the screen
curl_setopt ($ch, Curlopt_post, 1);//post Submission method
curl_setopt ($ch, Curlopt_postfields, $curlPost);
$data = curl_exec ($ch);//Run Curl
Curl_close ($ch);
Print_r ($data);//output result


The result is: The SMS has been submitted to the Send queue!
The address of the Fetion interface is http://sms.api.bz/
Fetion Interface Mode:
Http://sms.api.bz/fetion.php?username= your mobile fetion sign-in phone number
&password= your mobile fetion login password
&sendto= receive SMS Fetion friend's phone number
&message= SMS Content
Format: http://sms.api.bz/fetion.php?username=13800138000&password=123456&sendto=13912345678&message= SMS Content
Note to keep the utf-8 format, I made a mistake.

Summarize the use of the Curl method:

Initialize Curl

Use curl_setopt to set the destination URL, and other options, which are detailed reference to the options method: http://cn2.php.net/manual/zh/ref.curl.php

Curl_exec, perform Curl

After execution, turn off Curl
The final step is to output
One of the most wanted curl functions: Curl_getinfo
Curl_getinfo (Resource $ch [, int $opt = 0])

Copy CodeThe code is as follows:
/*curl instances
*/
$curl = Curl_init ();
Set the URL you need to crawl
curl_setopt ($curl, Curlopt_url, ' http://www.baidu.com ');
Set Header
curl_setopt ($curl, Curlopt_header, 0);
Sets the curl parameter, which requires the result to be saved to a string or output to the screen.
curl_setopt ($curl, Curlopt_returntransfer, 1);
Run Curl, request a Web page
$data = curl_exec ($curl);
if ($data = = = False) {
echo Curl_error ($curl); exit;
}
$info = Curl_getinfo ($curl);
Close URL Request
Curl_close ($curl);

Show the data obtained
Var_dump ($info);
Var_dump ($data);


can return:

urlinfo_effective_url– last valid URL address
curlinfo_http_code– the last HTTP code received
curlinfo_filetime– the time of the remote acquisition of the document, if not, the return value is "1"
curlinfo_total_time– time spent on the last transmission
curlinfo_namelookup_time– time spent in name resolution
curlinfo_connect_time– time spent establishing a connection
curlinfo_pretransfer_time– the time it takes to establish a connection to prepare the transfer
curlinfo_starttransfer_time– the time it takes to start a connection to a transfer
curlinfo_redirect_time– the time that the redirect was used before the transaction transfer started
curlinfo_size_upload– The total amount of uploaded data
curlinfo_size_download– the total amount of downloaded data
curlinfo_speed_download– Average Download speed
curlinfo_speed_upload– Average upload speed
The size of the Curlinfo_header_size–header section
curlinfo_header_out– sending the requested string
curlinfo_request_size– the size of the request in the HTTP request that has the problem
curlinfo_ssl_verifyresult– Verify the result of the request by setting the SSL certificate returned by Curlopt_ssl_verifypeer
curlinfo_content_length_download– length of downloaded content read from Content-length:field
curlinfo_content_length_upload– Description of upload content size
curlinfo_content_type– Download content content-type: value, NULL indicates that the server is not sending a valid Content-type:header

Using Curl for multithreading

Curl is generally used to crawl Web pages, the second is GET or post data, the third application is to implement the multi-threaded task of PHP
The following to implement multi-threaded

Copy CodeThe code is as follows:
/*
Curl Multi-threaded crawl
*/
/**
* Curl Multithreading
*
* @param array $array parallel URLs
* @param int $timeout time-out
* @return Array
*/
function Curl_http ($array, $timeout) {
$res = Array ();
$MH = Curl_multi_init ();//Create Multiple Curl handles
$startime = Getmicrotime ();
foreach ($array as $k = = $url) {
$conn [$k]=curl_init ($url);

curl_setopt ($conn [$k], curlopt_timeout, $timeout);//Set time-out
curl_setopt ($conn [$k], Curlopt_useragent, ' mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0) ');
curl_setopt ($conn [$k], Curlopt_maxredirs, 7);//http Orientation level
curl_setopt ($conn [$k], Curlopt_header, 0);//Do not HEADER, add block efficiency
curl_setopt ($conn [$k], curlopt_followlocation, 1); 302 redirect
curl_setopt ($conn [$k],curlopt_returntransfer,1);
Curl_multi_add_handle ($MH, $conn [$k]);
}
To prevent the death cycle from killing the CPU. This is based on the online wording
do {
$MRC = Curl_multi_exec ($MH, $active);//When no data, active=true
} while ($MRC = = Curlm_call_multi_perform);//When data is being accepted
while ($active and $MRC = = CURLM_OK) {//when there is no data or when a request is paused, active=true
if (Curl_multi_select ($MH)! =-1) {
do {
$MRC = Curl_multi_exec ($MH, $active);
} while ($MRC = = Curlm_call_multi_perform);
}
}

foreach ($array as $k = = $url) {
Curl_error ($conn [$k]);
$res [$k]=curl_multi_getcontent ($conn [$k]);//Get return information
$header [$k]=curl_getinfo ($conn [$k]);//Return header information
Curl_close ($conn [$k]);//Closing the handle
Curl_multi_remove_handle ($MH, $conn [$k]); Freeing resources
}

Curl_multi_close ($MH);
$endtime = Getmicrotime ();
$diff _time = $endtime-$startime;

Return Array (' diff_time ' = $diff _time,
' Return ' = $res,
' Header ' = $header
);

}
Calculate Current time
function Getmicrotime () {
List ($usec, $sec) = Explode ("", Microtime ());
return (float) $usec + (float) $sec);
}

Test it, curl. Three URLs
$array = Array (
"http://www.weibo.com/",
"http://www.renren.com/",
"http://www.qq.com/"
);
$data = Curl_http ($array, ' 10 ');//Call
Var_dump ($data);//Output

?>


The explanation of the does while:

Because $active to wait for all the URL data to be completed before it becomes false, so here to use the return value of curl_multi_exec to determine whether there is data,
When there is data on the call curl_multi_exec, temporarily no data to enter the Select phase, the new data can be awakened to continue execution.
The advantage here is that the CPU is useless. More detailed Description: http://hi.baidu.com/%D4%C2%D2%B9%C4%FD%ED%F8/blog/item/9dfcf4fbe6b84374024f563d.html

This multi-threaded writing step:
First step: Call Curl_multi_init
Step two: Loop call Curl_multi_add_handle
It is important to note that the second parameter of Curl_multi_add_handle is a sub-handle from Curl_init.
Step three: Continue calling Curl_multi_exec
Fourth step: Loop call Curl_multi_getcontent as needed to get results
Fifth step: Call Curl_multi_remove_handle, and call curl_close for each word handle
Sixth step: Call Curl_multi_close
Multi-threaded testing:

Summary: 36 HTTP requests, from the execution of the time sequence, three sites of IP crossover, the description is concurrent!
—————————————————————————
Curl under the Linux command
Several common ways to use:
Download function:
Direct download equivalent to wget
Curl-o 1.jpg Http://cgi2.tky.3web.ne.jp/~zzh/screen1.JPG
Download Screen1 in bulk. Jpg–screen10. Jpg
Curl-o http://cgi2.tky.3web.ne.jp/~zzh/screen[1-10]. Jpg
Breakpoint Download
Curl-c-O Http://cgi2.tky.3wb.ne.jp/~zzh/screen1.JPG


Reverse proxy function
Curl-x 123.45.67.89:1080-o page.html http://www.yahoo.com
Show header File
Curl-i www.sina.com

http://www.bkjia.com/PHPjc/327381.html www.bkjia.com true http://www.bkjia.com/PHPjc/327381.html techarticle Curl Installation: Install under Windows: Modify the settings of the php.ini file, locate Php_curl.dll//Cancel under the comment Extension=php_curl.dll Linux below installation: Copy code code as follows ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.