Example of PHP + Ajax remote image capture and download

Source: Internet
Author: User
Send request: Send the entered target URL and save path name to image.info asynchronously. php file. This file contains an ImageCatch class. Note that either of them specifies the target image capture and the other one only specifies... Send request:Send the entered target URL and save path name to image.info asynchronously using AJAX. php file, which contains an ImageCatch class, note that because one is to specify the target image capture, one is to specify a URL, such as http://www.phprm.com form, therefore, there is also a parameter used to determine whether to specify the target crawling or website crawling.

Receive request: Receives Two sent parameters, the target URL and storage path, instantiates the ImageCatch class, and transmits the address and storage path, use the file_get_contents function to assign a value to the variable $ content.

First, specify the implementation of image capturing: it is relatively simple to specify the method of image capturing. you can directly use the file_get_contents function to read the image, and then write file_put_contents to a file to save it.

The following describes how to capture images from a specified website:

The method is a little different from the method for capturing the specified image address. because jquery + ajax is used to capture images in the refreshing mode, a request is sent once. First, let's talk about what request is sent for the first time. Obviously, the first request is to get all the image addresses and total number of images on the target website. how can I get all the image addresses on the target website? The idea is the same as above, but the method is different.

Step 1:Use the file_get_contents function to read the target URL and assign a value to the content variable.

Step 2:Use regular expressions to match the src attributes in all img tags and save them in an array. This way, the image address of the webpage has been obtained.

Step 3:Use regular expressions to match the href attributes of link labels of all style sheet files and save them in an array $ arr1.

Step 4:You can also use the file_get_contents function to read the obtained style table file, use the regular expression to match the url background image address in the css style, and save it in an array $ arr2, in this way, the css image is retrieved.

Step 5:Combine $ arr1 and $ arr2 with the array_merge function into $ arr and use an array $ arr3 ("total" => count ($ arr )) obtain the total number of images and append them to the array $ arr so that the image address and total number are obtained.

Step 6:Use json_encode to compile a json data returned

Step 7:After receiving the returned json data, you can store the data into an array to determine whether the array is empty or not. then, you can call a function in a recursive way, after the returned results, the first element of the array is removed, and then the system checks whether the array is empty or not. The system continues sending the capture request until the array is empty, all images have been captured.

Now let's look at the example. the index. php code is as follows:

   
  PHP remote image capturing  

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.