


Performance comparison of using file_get_content series functions and using curl series functions to collect images
Since the car content in the backend of a company’s car website mainly comes from Auto Home, the editor colleagues have to manually add cars to Auto Home every day, which is really a pain in the ass. Ever since, in order to change this situation, as a development coder, my task has come. . . That is to prepare a function. As long as you paste the corresponding car home URL, the data can be automatically filled into the form in our backend. At present, the basic filling has been implemented, but the corresponding car photo album is still not collected. Come in.
I have done the function of collecting pictures before, but most of the cars in Autohome have a lot of pictures. At the beginning, I planned to use the previous method of collecting pictures, that is, use file_get_content to get the content corresponding to the URL. Then match the address of the image, then use file_get_content to obtain the content of these image URLs, and then load it locally. The code is as follows:
<?<span>php </span><span>header</span>('Content-type:text/html;charset=utf-8'<span>); </span><span>set_time_limit</span>(0<span>); </span><span>class</span><span> runtime { </span><span>var</span> <span>$StartTime</span> = 0<span>; </span><span>var</span> <span>$StopTime</span> = 0<span>; </span><span>function</span><span> get_microtime() { </span><span>list</span>(<span>$usec</span>, <span>$sec</span>) = <span>explode</span>(' ', <span>microtime</span><span>()); </span><span>return</span> ((<span>float</span>)<span>$usec</span> + (<span>float</span>)<span>$sec</span><span>); } </span><span>function</span><span> start() { </span><span>$this</span>->StartTime = <span>$this</span>-><span>get_microtime(); } </span><span>function</span><span> stop() { </span><span>$this</span>->StopTime = <span>$this</span>-><span>get_microtime(); } </span><span>function</span><span> spent() { </span><span>return</span> <span>round</span>((<span>$this</span>->StopTime - <span>$this</span>->StartTime) * 1000, 1<span>); } } </span><span>$runtime</span>= <span>new</span><span> runtime(); </span><span>$runtime</span>-><span>start(); </span><span>$url</span> = 'http://car.autohome.com.cn/pic/series-s15306/289.html#pvareaid=102177'<span>; </span><span>$rs</span> = <span>file_get_contents</span>(<span>$url</span><span>); </span><span>//</span><span> echo $rs;exit;</span> <span>preg_match_all</span>('/(\/pic\/series-s15306\/289-\d+\.html)/', <span>$rs</span>, <span>$urlArr</span><span>); </span><span>$avalie</span> = <span>array_unique</span>(<span>$urlArr</span>[0<span>]); </span><span>$count</span> = <span>array</span><span>(); </span><span>foreach</span> (<span>$avalie</span> <span>as</span> <span>$key</span> => <span>$ul</span><span>) { </span><span>$pattern</span> = '/<img src="(http:\/\/car1\.autoimg\.cn\/upload\/\d+\/\d+\/\d+\/.*?\.jpg)"/'<span>; </span><span>preg_match_all</span>(<span>$pattern</span>, <span>file_get_contents</span>('http://car.autohome.com.cn'.<span>$ul</span>), <span>$imgSrc</span><span>); </span><span>$count</span> = <span>array_merge</span>(<span>$count</span>, <span>$imgSrc</span>[1<span>]); } </span><span>foreach</span>(<span>$count</span> <span>as</span> <span>$k</span>=><span>$v</span><span>) { </span><span>$data</span>[<span>$k</span>] = <span>file_get_contents</span>(<span>$v</span><span>); } </span><span>foreach</span>(<span>$data</span> <span>as</span> <span>$k</span>=><span>$v</span><span>) { </span><span>file_put_contents</span>('./pic2/'.<span>time</span>().'_'.<span>rand</span>(1, 10000).'.jpg', <span>$v</span><span>); } </span><span>$runtime</span>-><span>stop(); </span><span>echo</span> "页面执行时间: ".<span>$runtime</span>->spent()." 毫秒";
It turns out that this method is fine with fewer pictures, but it is quite laggy if there are too many pictures. . It is also difficult to run local tests, let alone go online by then. After Baidu, I used the curl method to download images. After testing, it did improve, but it still felt a bit slow. It would be great if PHP had multiple threads. . .
After some tossing and looking for information, I found that the curl library of PHP can actually simulate multi-threading, that is, using the curl_multi_* series of functions. After rewriting, the code became like this:
<?<span>php </span><span>header</span>('Content-type:text/html;charset=utf-8'<span>); </span><span>set_time_limit</span>(0<span>); </span><span>class</span><span> runtime { </span><span>var</span> <span>$StartTime</span> = 0<span>; </span><span>var</span> <span>$StopTime</span> = 0<span>; </span><span>function</span><span> get_microtime() { </span><span>list</span>(<span>$usec</span>, <span>$sec</span>) = <span>explode</span>(' ', <span>microtime</span><span>()); </span><span>return</span> ((<span>float</span>)<span>$usec</span> + (<span>float</span>)<span>$sec</span><span>); } </span><span>function</span><span> start() { </span><span>$this</span>->StartTime = <span>$this</span>-><span>get_microtime(); } </span><span>function</span><span> stop() { </span><span>$this</span>->StopTime = <span>$this</span>-><span>get_microtime(); } </span><span>function</span><span> spent() { </span><span>return</span> <span>round</span>((<span>$this</span>->StopTime - <span>$this</span>->StartTime) * 1000, 1<span>); } } </span><span>$runtime</span>= <span>new</span><span> runtime(); </span><span>$runtime</span>-><span>start(); </span><span>$url</span> = 'http://car.autohome.com.cn/pic/series-s15306/289.html#pvareaid=102177'<span>; </span><span>$rs</span> = <span>file_get_contents</span>(<span>$url</span><span>); </span><span>preg_match_all</span>('/(\/pic\/series-s15306\/289-\d+\.html)/', <span>$rs</span>, <span>$urlArr</span><span>); </span><span>$avalie</span> = <span>array_unique</span>(<span>$urlArr</span>[0<span>]); </span><span>$count</span> = <span>array</span><span>(); </span><span>foreach</span> (<span>$avalie</span> <span>as</span> <span>$key</span> => <span>$ul</span><span>) { </span><span>$pattern</span> = '/<img src="(http:\/\/car1\.autoimg\.cn\/upload\/\d+\/\d+\/\d+\/.*?\.jpg)"/'<span>; </span><span>preg_match_all</span>(<span>$pattern</span>, <span>file_get_contents</span>('http://car.autohome.com.cn'.<span>$ul</span>), <span>$imgSrc</span><span>); </span><span>$count</span> = <span>array_merge</span>(<span>$count</span>, <span>$imgSrc</span>[1<span>]); } </span><span>$handle</span> =<span> curl_multi_init(); </span><span>foreach</span>(<span>$count</span> <span>as</span> <span>$k</span> => <span>$v</span><span>) { </span><span>$curl</span>[<span>$k</span>] = curl_init(<span>$v</span><span>); curl_setopt(</span><span>$curl</span>[<span>$k</span>], CURLOPT_RETURNTRANSFER, 1<span>); curl_setopt(</span><span>$curl</span>[<span>$k</span>], CURLOPT_HEADER, 0<span>); curl_setopt(</span><span>$curl</span>[<span>$k</span>], CURLOPT_TIMEOUT, 30<span>); curl_multi_add_handle (</span><span>$handle</span>, <span>$curl</span>[<span>$k</span><span>]); } </span><span>$active</span> = <span>null</span><span>; </span><span>do</span><span> { </span><span>$mrc</span> = curl_multi_exec(<span>$handle</span>, <span>$active</span><span>); } </span><span>while</span> (<span>$mrc</span> ==<span> CURLM_CALL_MULTI_PERFORM); </span><span>while</span> (<span>$active</span> && <span>$mrc</span> ==<span> CURLM_OK) { // 这句在php5.3以后的版本很关键,因为没有这句,可能curl_multi_select可能会永远返回-1,这样就永远死在循环里了 </span><span>while</span> (curl_multi_exec(<span>$handle</span>, <span>$active</span>) ===<span> CURLM_CALL_MULTI_PERFORM); </span><span>if</span> (curl_multi_select(<span>$handle</span>) != -1<span>) { </span><span>do</span><span> { </span><span>$mrc</span> = curl_multi_exec(<span>$handle</span>, <span>$active</span><span>); } </span><span>while</span> (<span>$mrc</span> ==<span> CURLM_CALL_MULTI_PERFORM); } } </span><span>foreach</span> (<span>$curl</span> <span>as</span> <span>$k</span> => <span>$v</span><span>) { </span><span>if</span> (curl_error(<span>$curl</span>[<span>$k</span>]) == ""<span>) { </span><span>$data</span>[<span>$k</span>] = curl_multi_getcontent(<span>$curl</span>[<span>$k</span><span>]); } curl_multi_remove_handle(</span><span>$handle</span>, <span>$curl</span>[<span>$k</span><span>]); curl_close(</span><span>$curl</span>[<span>$k</span><span>]); } </span><span>foreach</span>(<span>$data</span> <span>as</span> <span>$k</span>=><span>$v</span><span>) { </span><span>$file</span> = <span>time</span>().'_'.<span>rand</span>(1000, 9999).'.jpg'<span>; </span><span>file_put_contents</span>('./pic3/'.<span>$file</span>, <span>$v</span><span>); } curl_multi_close(</span><span>$handle</span><span>); </span><span>$runtime</span>-><span>stop(); </span><span>echo</span> "页面执行时间: ".<span>$runtime</span>->spent()." 毫秒";
Okay, multi-threaded collection is really refreshing. After a series of tests and comparisons, 5 tests showed that curl multi-threading was faster than file_get_content 4 times, and the time was 3 to 5 times that of file_get_content. To sum up, , try to use this method in future collections to improve efficiency.
The above introduces the performance comparison of using the file_get_content series of functions and using the curl series of functions to collect images, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

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

Both curl and Pythonrequests are powerful tools for sending HTTP requests. While curl is a command-line tool that allows you to send requests directly from the terminal, Python's requests library provides a more programmatic way to send requests from Python code. The basic syntax for converting curl to Pythonrequestscurl command is as follows: curl[OPTIONS]URL When converting curl command to Python request, we need to convert the options and URL into Python code. Here is an example curlPOST command: curl-XPOST https://example.com/api

Use Java's File.length() function to get the size of a file. File size is a very common requirement when dealing with file operations. Java provides a very convenient way to get the size of a file, that is, using the length() method of the File class. . This article will introduce how to use this method to get the size of a file and give corresponding code examples. First, we need to create a File object to represent the file we want to get the size of. Here is how to create a File object: Filef

How to convert php blob to file: 1. Create a php sample file; 2. Through "function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })} ” method can be used to convert Blob to File.

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

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

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

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

Use Java's File.renameTo() function to rename files. In Java programming, we often need to rename files. Java provides the File class to handle file operations, and its renameTo() function can easily rename files. This article will introduce how to use Java's File.renameTo() function to rename files and provide corresponding code examples. The File.renameTo() function is a method of the File class.
