PHP curl library explanation

Source: Internet
Author: User

Introduction: This is a detailed page for PHP curl library explanation. It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 333673 'rolling = 'no'>

Using the PHP curl library, you can easily and effectively capture webpages. You only need to run a script, analyze the web page you crawled, and then you canProgramTo obtain the data you want.
. Whether you want to get part of the data from a link or an XML file and import it to the database, you may simply get the webpage content, curl
Is a powerful php library. This article describes how to use this PHP library.

Enable curl settings

First, we must first determine whether PHP has enabled this library. You can use the php_info () function to obtain this information.

123 <? PHP
Phpinfo ();?>

If you can see the following output on the webpage, it indicates that the curl library has been enabled.

If you see this, you need to set your PHP and enable this library. If you are on Windows, it is very simple. You need to modify the settings of your php. ini file, find php_curl.dll, and cancel the semicolon comment. As follows:

12 // Cancel the commentExtension = php_curl.dll

If you are in Linux, You need to recompile your php. During editing, you need to open the compilation parameter-add the "with-curl" parameter to the configure command.

A small example

If everything is ready, the following is a small routine:

123456789101112131415161718192021 // initialize a curl object $ curl
= curl_init (); // set the URL to be crawled curl_setopt (
$ curl
, curlopt_url,
'HTTP: // coolshell.cn
'

); // set the header curl_setopt (
$ curl
, curlopt_header, 1); // you can specify the curl parameter to save the result to the string or Output to the screen. curl_setopt (
$ curl
, curlopt_returntransfer, 1 ); // run curl, request webpage $ data
= curl_exec (
$ curl
); // closes a URL request curl_close (
$ curl
); // display the obtained data var_dump (
$ data
);

How to post data

The above is the Web page captureCode, The following is to post data to a webpage. Suppose we have a URL http://www.example.com/sendSMS.php for processing forms
It can accept two form fields: one is the phone number and the other is the text message content.

12345678910111213 <? PHP
$ Phonenumber
=
'123'
;
$ Message
=
'This message was generated by curl and php'
;
$ Curlpost
=
'Pnumber ='
. Urlencode (
$ Phonenumber
).
'& Message ='
. Urlencode (
$ Message
).
'& Submit = send'
;
$ Ch
= Curl_init ();
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 procedure, we can see that using curlopt_post to set the POST method of the HTTP protocol, instead of the get method, and then setting the post data with curlopt_postfields.

About proxy servers

 
The following is an example of how to use the proxy server. Pay attention to the highlighted Code. The Code is very simple and I don't need to talk about it.
1234567891011 <? 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 cookie

For SSL, that is, the HTTPS protocol, you only need to change the http: // In the curlopt_url connection to https. Of course, the parameter curlopt_ssl_verifyhost can be set as a verification site.

For cookie, you need to understand the following three parameters:

    • Curlopt_cookie: Set a cookie in the face-to-face session.
    • Curlopt_cookiejar saves a cookie when the session ends.
    • Curlopt_cookiefile: the cookie file.

HTTP Server Authentication

Finally, let's take a look at the situation of HTTP Server Authentication.

12345678910 <? 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, see the curl manual.

ZT: http://coolshell.cn/articles/664.html

More articles on "php curl library explanation"

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/333673.html pageno: 11.

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.