PHP-based Curl Quick Start three

Source: Internet
Author: User
Tags curl options http authentication
PHP-based Curl QuickStart 3

The following code is a bit complicated, so I'll explain in detail in small steps:

The following is the referenced content:

1. Batch processor $MH = Curl_multi_init ();//2. Add the urlfor ($i = 0; $i < $max _connections; $i + +) {Add_url_to_multi_handle ($MH, $url _list) required for batch processing;} 3. Initial processing does {$MRC = Curl_multi_exec ($MH, $active);} while ($MRC = = Curlm_call_multi_perform);//4. Main Loop while ($active && $MRC = = CURLM_OK) {//5. Active connection if (Curl_multi_select ($MH)! = 1) {//6. Work do {$MRC = Curl_mult I_exec ($MH, $active);} while ($MRC = = Curlm_call_multi_perform);//7. Any information? if ($mhinfo = Curl_multi_info_read ($MH)) {//means that the connection ends normally//8. Get information from curl handle $chinfo = Curl_getinfo ($mhinfo [' handle ']);//9. Dead Chain? if (! $chinfo [' Http_code ']) {$dead _urls []= $chinfo [' url '];//10.404?} else if ($chinfo [' http_code '] = = 404) {$not _found_ URLs []= $chinfo [' url '];//11. You can also use} else {$working _urls []= $chinfo [' url '];} 12. Remove handle Curl_multi_remove_handle ($MH, $mhinfo [' Handle ']), Curl_close ($mhinfo [' handle ']);//13. Add a new URL, work if (Add_url_to_multi_handle ($MH, $url _list)) {do {$MRC = Curl_multi_exec ($MH, $active);} while ($MRC = = Curlm_ca Ll_multi_perform);}}} //14. Finished Curl_multi_close ($MH), echo "==dead urls==\n", echo implode ("\ n", $dead _urls). "\ n"; echo "==404 urls==\n"; Echo implode ("\ n", $not _found_urls). "\ n"; echo "==working urls==\n"; Echo implode ("\ n", $working _urls);//15. Add Urlfunction add_url_to_multi_handle ($MH, $url _list) to the batch processor {static $index = 0;//If there is no URL to use if ($url _list[$index]) {// New Curl Handle $ch = Curl_init ();//Configuration Urlcurl_setopt ($ch, Curlopt_url, $url _list[$index]);//do not want to output the returned content curl_setopt ($ch, Curlopt_returntransfer, 1);//redirect to where we go curl_setopt ($ch, curlopt_followlocation, 1);//Do not need content body, can save bandwidth and time Curl_ Setopt ($ch, curlopt_nobody, 1);//Join the Batch processor Curl_multi_add_handle ($MH, $ch);//Dial the counter, the next time you call the function can add the next URL $index++; return true;} else {//no new URL required to process return false;}}

?


The above code is explained below. The ordinal number of the list corresponds to the sequential numbers in the code comment.
Create a new batch processor. Created a multi handle.
Later we will create a function add_url_to_multi_handle () that adds the URL to the batch processor. Whenever this function is called, a new URL is added to the batch processor. At first, we added 10 URLs to the batch processor (this figure was determined by $max _connections).
It is necessary to run curl_multi_exec () for initialization, as long as it returns curlm_call_multi_perform there is still work to be done. This is done primarily to create a connection, which does not wait for the full URL response.
As long as there are active connections in the batch, the main loop will persist.
Curl_multi_select () waits until a URL query generates an active connection.
Curl's work came again, mostly to get the response data.
Check various information. When a URL request is complete, an array is returned.
There is a CURL handle in the returned array. We use it to get the appropriate information for a single curl request.
If this is a dead chain or the request times out, the HTTP status code is not returned.
If this page is not found, a 404 status code will be returned.
In other cases we think this link is available (of course, you can also check the 500 error and so on ...) )。
Remove this curl handle from the batch because it's no longer using value, turn it off!
Good, now you can add another URL to come in. Once again, the initialization work begins ...
Well, it's all dry. Close the batch processor and generate a report.
Look back at the function that adds a new URL to the batch processor. Each time this function is called, the static variable $index incremented once, so that we know how many URLs are left in the process.

I ran this script over my blog (test needs, some error links were intentionally added), the results are as follows:

The following is the referenced content:

?



A total of about 40 URLs are inspected, which takes less than two seconds. When you need to check a larger number of URLs, its worry-saving effect can be imagined! If you open 10 connections at the same time, you can get up to 10 times times faster! In addition, you can use the no-partition feature of the Curl batch to handle a large number of URL requests without blocking your web script.

?

A few other useful curl options

HTTP Authentication

If a URL request requires HTTP-based authentication, you can use the following code:
Copy content to Clipboard code:

The following is the referenced content:

$url = "http://www.somesite.com/members/"; $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_returntransfer, 1);//Send user name and password curl_setopt ($ch, Curlopt_userpwd, "Myusername:mypassword");//You can allow it to redirect Curl_ Setopt ($ch, curlopt_followlocation, 1);//The following option allows CURL to send user name and password curl_setopt ($ch, Curlopt_unrestricted_auth, 1) after redirection// ; $output = curl_exec ($ch); Curl_close ($ch);

?


FTP Upload

PHP comes with an FTP class library, but you can also use CURL:

The following is the referenced content:

Open a file pointer $file = fopen ("/path/to/file", "R");//URL contains most of the required information, "Ftp://username:[email protected]:21/path/to/new /file "; $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_returntransfer, 1);// Upload related options curl_setopt ($ch, curlopt_upload, 1); curl_setopt ($ch, Curlopt_infile, $fp); curl_setopt ($ch, Curlopt_ Infilesize, FileSize ("/path/to/file"));//whether to turn on ASCII mode (useful when uploading text files) curl_setopt ($ch, CURLOPT_FTPASCII, 1); $output = Curl _exec ($ch); Curl_close ($ch);

?


Fan Wall Surgery

You can use a proxy to initiate a curl request:


The following is the referenced content:

$ch = Curl_init (); curl_setopt ($ch, Curlopt_url, ' http://www.example.com '); curl_setopt ($ch, Curlopt_returntransfer, 1 );//Specify Proxy address curl_setopt ($ch, Curlopt_proxy, ' 11.11.11.11:8080 ');//if required, provide a username and password curl_setopt ($ch, Curlopt_ Proxyuserpwd, ' User:pass '); $output = Curl_exec ($ch); Curl_close ($ch);

?



callback function

You can have curl invoke a specified callback function during a URL request. For example, start using the data as soon as the content or response is downloaded, instead of waiting for the full download to complete.


The following is the referenced content:

$ch = Curl_init (); curl_setopt ($ch, Curlopt_url, ' http://net.tutsplus.com '); curl_setopt ($ch, Curlopt_writefunction, " Progress_function "), Curl_exec ($ch), Curl_close ($ch), function progress_function ($ch, $str) {echo $str; return strlen ($ STR);}
?



This callback function must return the length of the string, otherwise this function will not work properly.

In the process of receiving a URL response, the function is called whenever a packet is received.

Summary

Today we learned about the power of the Curl Library and the flexibility of extensibility. I hope you like it. The next time you want to launch a URL request, consider curl!

Text: PHP-based Curl Quick start

English Original: http://net.tutsplus.com/tutorial%20...%20for-mastering-curl/

Original Author: Burak Guzel

This article link: http://www.blueidea.com/tech/program/2010/7348.asp
  • 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.