search
Homephp教程PHP开发Introduction to the functions of PHP's cURL library: crawling web pages, POST data and others

Whether you want to get some data from a link, take an XML file and import it into a database, or even simply get the content of a web page, cURL is a powerful PHP library. This article mainly describes how to use this PHP library.
  Enable cURL settings
  First, we must first determine whether our PHP has this library enabled. You can get this information by using the php_info() function.

<?php 
phpinfo(); 
?>

 If you can see the following output on the web page, it means that the cURL library has been enabled.
 If you see this, then you need to set up your PHP and enable this library. If you are on the Windows platform, it is very simple. You need to change the settings of your php.ini file, find php_curl.dll, and cancel the previous semicolon comment. As shown below:
//Cancel the comment below
extension=php_curl.dll

If you are under Linux, then you need to recompile your PHP. When editing, you need to turn on the compilation parameters—— Add the "--with-curl" parameter to the configure command.
 A small example
 If everything is ready, here is a small routine:

<?php 
// 初始化一个 cURL 对象 
$curl = curl_init(); 
// 设置你需要抓取的URL 
curl_setopt($curl, CURLOPT_URL, &#39;http://jb51.net&#39;); 
// 设置header 
curl_setopt($curl, CURLOPT_HEADER, 1); 
// 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
// 运行cURL,请求网页 
$data = curl_exec($curl); 
// 关闭URL请求 
curl_close($curl); 
// 显示获得的数据 
var_dump($data);

 How to POST data
 The above is the code to crawl the web page, and the following is the code to POST data to a certain web page. Suppose we have a form processing URL http://www.example.com/sendSMS.php, which can accept two form fields, one is a phone number, and the other is text message content.

<?php 
$phoneNumber = &#39;13912345678&#39;; 
$message = &#39;This message was generated by curl and php&#39;; 
$curlPost = &#39;pNUMBER=&#39; . urlencode($phoneNumber) . &#39;&MESSAGE=&#39; . urlencode($message) . &#39;&SUBMIT=Send&#39;; 
$ch = curl_init();chain link fencing 
curl_setopt($ch, CURLOPT_URL, &#39;http://www.example.com/sendSMS.php&#39;); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); 
$data = curl_exec(); 
curl_close($ch); 
?>

From the above program, we can see that use CURLOPT_POST to set the POST method of the HTTP protocol instead of the GET method, and then set the POST data with CURLOPT_POSTFIELDS.
 About proxy server
 The following is an example of how to use a proxy server. Please pay attention to the highlighted code. The code is very simple, so I don’t need to say more.

<?php 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, &#39;http://www.example.com&#39;); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1); 
curl_setopt($ch, CURLOPT_PROXY, &#39;fakeproxy.com:1080&#39;); 
curl_setopt($ch, CURLOPT_PROXYUSERPWD, &#39;user:password&#39;); 
$data = curl_exec(); 
curl_close($ch); 
?>

  About SSL and Cookies
  About SSL, which is the HTTPS protocol. For gas generators, you only need to change the http:// in the CURLOPT_URL connection to https://. Of course, there is also a parameter called CURLOPT_SSL_VERIFYHOST that can be set to verify the site.
 About Cookie, you need to understand the following three parameters:
 CURLOPT_COOKIE, set a cookie in the face-to-face session
 CURLOPT_COOKIEJAR, save a Cookie when the session ends
 CURLOPT_COOKIEFILE, Cookie file.
 HTTP server authentication
Finally, let’s take a look at HTTP server authentication.

<?php 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, &#39;http://www.example.com&#39;); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
curl_setopt(CURLOPT_USERPWD, &#39;[username]:[password]&#39;) 
$data = curl_exec(); 
curl_close($ch); 
?>

For more information, please refer to the relevant cURL manual.

For more introduction to the functions of PHP’s cURL library, please pay attention to the PHP Chinese website for crawling web pages, POST data and other related articles!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),