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, 'http://jb51.net'); // 设置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 = '13912345678'; $message = 'This message was generated by curl and php'; $curlPost = 'pNUMBER=' . urlencode($phoneNumber) . '&MESSAGE=' . urlencode($message) . '&SUBMIT=Send'; $ch = curl_init();chain link fencing curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/sendSMS.php'); 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, 'http://www.example.com'); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1); curl_setopt($ch, CURLOPT_PROXY, 'fakeproxy.com:1080'); curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'user:password'); $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, 'http://www.example.com'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt(CURLOPT_USERPWD, '[username]:[password]') $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!