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

Introduction to the functions of PHP's cURL library: crawling web pages, POST data and others

Dec 23, 2016 pm 04:22 PM

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(); 
?>
Copy after login

 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);
Copy after login

 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); 
?>
Copy after login

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); 
?>
Copy after login

  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); 
?>
Copy after login

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 of this Website
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)