PHP cURL proxy

Source: Internet
Author: User
Tags php example
PHP uses the cURL proxy to use PHP's cURL scheme to capture the website in a simple and effective manner. You only need to run a notebook, and then analyze the web page you crawled, and then you can get the desired data in the way of a program. No matter whether you want to connect a part of the data from a region, or take an XML file and import it into the data volume, the fear is that a simple volume is used to retrieve the internal content of the website. the cURL is used by PHP to use the cURL proxy.
You can use PHP cURL flood to capture the website in a simple and effective manner. You only need to run a notebook, and then analyze the web page you crawled, and then you can get the desired data in the way of a program. No matter whether you want to connect a part of the data from a region, or take an XML file and import it into the data volume, the fear is that the website content can be retrieved from a simple volume. cURL is a powerful PHP program. This document describes how to use this PHP example.

Reset with cURL

First, we have to determine whether our PHP has enabled this feature. you can obtain this information by using the php_info () function.
Php proxy

<? Php
Phpinfo ();
? >

If you can see the following output on the webpage, it indicates that cURL has been opened.

If you see it, you need to set your PHP and enable it. If you are in Windows, it is very simple and you need to change your php. set the ini file, find php_curl.dll, and cancel the preceding Shard allocation. As follows:
Php proxy

// Cancel the specified region
Extension = php_curl.dll



If you are in Linux, you need to rewrite your PHP, you need to open the parameter numbers-add "-with-curl" to the configure command.

A small example

If everything goes wrong, the following is a small routine:
Php proxy

<? Php
// Initialize a cURL object
$ Curl = curl_init ();
// Set the URL you want to capture
Curl_setopt ($ curl, CURLOPT_URL, 'http: // cocre.com ');
// Set the header
Curl_setopt ($ curl, CURLOPT_HEADER, 1 );
// Set the number of cURL records. The result must be saved to the string or output to the screen.
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 );
// Run cURL. please find the webpage
$ Data = curl_exec ($ curl );
// Request URL
Curl_close ($ curl );
// Displays the data obtained.
Var_dump ($ data );





POST data

The above is the generation of the crawling website, and the following is the POST data to a certain website. Assume that we have an address http://www.example.com/sendSMS.php for processing orders, which can accept two single table domains, one is telephone, and the other is text message content.
Php proxy

<? Php
$ PhoneNumber = '000000 ';
$ 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 );
? >



We can see from the above program that using CURLOPT_POST to set the POST method of the HTTP protocol instead of the GET method, and then using CURLOPT_POSTFIELDS to set the POST data.

About the proxy server

The following is an example of how to use the proxy server. I don't need to talk about the highlighted substitution.
Php proxy

<? 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 );
? >





SSL and Cookie

For SSL, that is, HTTPS protocol, you only need to change the http: // in the CURLOPT_URL scheme to https. You can also set CURLOPT_SSL_VERIFYHOST as a verification site.

For Cookie, you need to know the following three data records:

CURLOPT_COOKIE. set a cookie in the session.

CURLOPT_COOKIEJAR saves a Cookie when the session ends.

CURLOPT_COOKIEFILE: the Cookie file.

HTTP server certificate

Finally, let's take a look at the situation of the HTTP server authentication.
Php proxy

<? 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 );
? >


To sum up, cURL can do three things about http:
1. directly capture the webpage of an address.
2. post data to an address.
3. access a webpage through a proxy. Or post.
Others, including https and cookie.


For more content, refer to the cURL manual.
Related Article

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.