CURL is a tool that uses URL syntax to transfer files and data. It supports many protocols, such as HTTP, FTP, TELNET, etc. The best part is that PHP also supports the CURL library. Using PHP's CURL library can easily and effectively scrape web pages. You only need to run a script and analyze the web pages you crawled, and then you can get the data you want programmatically. Whether you want to retrieve partial data from a link, take an XML file and import it into a database, or even simply retrieve the content of a web page, CURL is a powerful PHP library.
①: Initialization
1 |
|
②: Setting attributes
1 |
|
③: Execute and get the result
1 |
|
④: Release the handle
1 |
|
①: GET method to achieve
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
②: POST method to implement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
③: If The obtained data is in json format and is interpreted into an array using the json_decode function.
$output_array = json_decode($data,true); //If the second parameter is true, it will be converted to array form. If not filled in, it will be in the form of an object
If you use json_decode($data) to parse, you will get object type data.
An encapsulated function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
Although these two functions are not difficult, they are still worth learning. Because these two functions must be used when making interfaces or calling interfaces.
Recommended related articles:
How to crawl Tmall and Taobao product data with php
The above is the detailed content of PHP uses CURL to implement GET and POST request step examples. For more information, please follow other related articles on the PHP Chinese website!