Use curl in PHP for Get and POST requests

Source: Internet
Author: User
Tags ftp commands http authentication terminates ftp transfer ssl certificate sub domain
One, what is curl?

CURL is a tool that uses URL syntax to transfer files and data, and supports many protocols, such as HTTP, FTP, Telnet, and so on. Best of all, PHP also supports the CURL library. Using the PHP Curl Library, you can easily and effectively grab pages. You just need to run a script, then analyze the page you crawled, and then you can get the data you want in a program. Whether you want to take part of the data from a link, or take an XML file and import it into the database, the fear is simply to get the Web content, CURL is a powerful PHP library.


Second, the Curl function library.

curl_close-closing a Curl session

curl_copy_handle-Copy all the contents and parameters of a Curl connection resource

curl_errno-returns a numeric number that contains the current session error information

curl_error-returns a string containing the current session error message

Curl_exec-performing a Curl session

Curl_getinfo-gets the information for a Curl connection resource handle

curl_init-initialization of a curl session

curl_multi_add_handle-Adding a separate curl handle resource to a curl batch session

curl_multi_close-closing a batch handle resource

curl_multi_exec-parsing a Curl batch handle

curl_multi_getcontent-returns the text stream of the obtained output

Curl_multi_info_read-Gets the related transfer information for the currently resolved curl

curl_multi_init-initializing a curl batch handle resource

curl_multi_remove_handle-removing a handle resource from the Curl batch handle resource

Curl_multi_select-get all the sockets associated with the CURL extension, which can and be "selected"

curl_setopt_array-set session parameters as an array for a curl

curl_setopt-setting session parameters for a curl

curl_version-getting the version information about Curl

The Curl_init () function Initializes a curl session, and the only parameter to the Curl_init () function is optional, representing a URL address.

The function of the curl_exec () function is to perform a curl session, and the only argument is the handle returned by the Curl_init () function.

The function of the Curl_close () function is to close a curl session, and the only argument is the handle returned by the Curl_init () function.

Iii. basic steps for PHP to build a curl request

①: Initialization

Curl_init ()

②: Setting properties

curl_setopt (). There is a long string of curl parameters that can be set to specify the details of the URL request.

③: Execute and get results

Curl_exec ()

④: Release handle

Curl_close ()

Four, curl implementation get and post

①:get Method implementation

 
  

②:post Method implementation

 
 
  "Coder",        "password" = "12345"        );    curl_setopt ($curl, Curlopt_postfields, $post _data);    Execute command    $data = curl_exec ($curl);    Close URL request    curl_close ($curl);    Displays the data obtained    Print_r ($data);? >

③: If the data is obtained in JSON format, use the Json_decode function to interpret the array.

$output _array = Json_decode ($output, true);

If you use Json_decode ($output) parsing, you will get data of type object.

V. A function of my own encapsulation

Parameter 1: URL to access, parameter 2:post data (not filled in as get), Parameter 3: $cookies submitted, Parameter 4: return $cookies function Curl_request ($url, $post = ", $cookie ="        , $returnCookie =0) {$curl = Curl_init ();        curl_setopt ($curl, Curlopt_url, $url); curl_setopt ($curl, Curlopt_useragent, ' mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1;        trident/6.0) ');        curl_setopt ($curl, curlopt_followlocation, 1);        curl_setopt ($curl, Curlopt_autoreferer, 1);        curl_setopt ($curl, Curlopt_referer, "Http://XXX");            if ($post) {curl_setopt ($curl, Curlopt_post, 1);        curl_setopt ($curl, Curlopt_postfields, Http_build_query ($post));        } if ($cookie) {curl_setopt ($curl, Curlopt_cookie, $cookie);        } curl_setopt ($curl, Curlopt_header, $returnCookie);        curl_setopt ($curl, Curlopt_timeout, 10);        curl_setopt ($curl, Curlopt_returntransfer, 1);        $data = curl_exec ($curl);        if (Curl_errno ($curl)) {return curl_error ($curl); } CUrl_close ($curl);            if ($returnCookie) {list ($header, $body) = Explode ("\r\n\r\n", $data, 2); Preg_match_all ("/set\-cookie: ([^;]            *);/", $header, $matches);            $info [' cookie '] = substr ($matches [1][0], 1);            $info [' content '] = $body;        return $info;        }else{return $data; }}


Optional parameter Description:

First Class:

For the optional parameters of these options below, value should be set to a bool type:

Options

Optional value values

Note

Curlopt_autoreferer

Referer: Information is automatically set in the header when based on location: redirection.

Curlopt_binarytransfer

Returns the native (raw) output when Curlopt_returntransfer is enabled.

Curlopt_cookiesession

When enabled, Curl simply passes a session cookie, ignoring other cookies, and by default, curl returns all cookies to the server. Session cookies are cookies that are used to determine if the session on the server is valid.

Curlopt_crlf

Converts a Unix newline character to a carriage return line break when enabled.

Curlopt_dns_use_global_cache

When enabled, a global DNS cache is enabled, which is thread-safe and enabled by default.

Curlopt_failonerror

The HTTP status code is displayed, and the default behavior is to ignore HTTP messages with numbers less than or equal to 400.

Curlopt_filetime

When enabled, attempts to modify the information in the remote document. The resulting information is returned through the curlinfo_filetime option of the Curl_getinfo () function. Curl_getinfo ().

Curlopt_followlocation

When enabled, the "location:" returned by the server server is returned to the server recursively in the header, using Curlopt_maxredirs to limit the number of recursive returns.

Curlopt_forbid_reuse

Forced disconnection after completion of the interaction, cannot be reused.

Curlopt_fresh_connect

Forces a new connection to be taken in place of the connection in the cache.

Curlopt_ftp_use_eprt

When enabled, when FTP is downloaded, use the EPRT (or lprt) command. When set to False, disable EPRT and LPRT, using the port command only.

Curlopt_ftp_use_epsv

When enabled, the EPSV command is first attempted before replying to PASV mode during FTP transfer. When set to False, the EPSV command is disabled.

Curlopt_ftpappend

Append writes to the file instead of overwriting it when enabled.

Curlopt_ftpascii

The alias of the Curlopt_transfertext.

Curlopt_ftplistonly

Only the name of the FTP directory is listed when enabled.

Curlopt_header

When enabled, the information for the header file is output as a data stream.

Curlinfo_header_out

The request string to trace the handle when enabled.

Available starting from PHP 5.1.3. The curlinfo_ prefix is intentional (intentional).

Curlopt_httpget

When enabled, the method for HTTP is set to get, because get is the default, so it is used only if it is modified.

Curlopt_httpproxytunnel

When enabled, it is transmitted over an HTTP proxy.

Curlopt_mute

Restores the default values for all modified parameters in the Curl function when enabled.

Curlopt_netrc

After the connection is established, access the ~/.NETRC file to get the user name and password information to connect to the remote site.

Curlopt_nobody

When enabled, the body portion of the HTML is not output.

Curlopt_noprogress

Turns off the progress bar of the curl transfer when enabled, and the default setting for this entry is enabled.

Note:

PHP automatically sets this option to true, and this option should only be changed for debugging purposes.

Curlopt_nosignal

When enabled, ignores all the signals that curl passes to PHP. This entry is enabled by default when SAPI multi-threaded transmissions.

When CURL 7.10 is added.

Curlopt_post

When enabled, a regular post request is sent with the type: application/x-www-form-urlencoded, just as the form was submitted.

Curlopt_put

Allow HTTP to send files when enabled, you must set both Curlopt_infile and Curlopt_infilesize.

Curlopt_returntransfer

The information obtained by CURL_EXEC () is returned as a file stream, rather than as a direct output.

Curlopt_ssl_verifypeer

When disabled, curl terminates the validation from the server. Use the Curlopt_cainfo option to set the certificate using the Curlopt_capath option to set the certificate directory if Curlopt_ssl_verifypeer (the default value is 2) is enabled, Curlopt_ssl_ Verifyhost needs to be set to true otherwise set to False.

The default is true from Curl 7.10. The default binding installation starts with Curl 7.10.

Curlopt_transfertext

When enabled, use ASCII mode for FTP transmissions. For LDAP, it retrieves plain text information rather than HTML. On Windows systems, the system does not set stdout to binary mode.

Curlopt_unrestricted_auth

The user name and password information is continuously appended to multiple locations in the header generated by using curlopt_followlocation, even if the domain name has changed.

Curlopt_upload

Allow file uploads when enabled.

Curlopt_verbose

When enabled, all information is reported and stored in the stderr or specified curlopt_stderr.


Second Category:

For the optional parameters of these options below, value should be set to an integer type:

Options

Optional value values

Note

Curlopt_buffersize

The size of the cache is read in each fetch, but there is no guarantee that the value will be filled every time.

was added in Curl 7.10.

Curlopt_closepolicy

Not curlclosepolicy_least_recently_used is curlclosepolicy_oldest, there are three other curlclosepolicy_, but Curl is not supported for the time being.

Curlopt_connecttimeout

The time to wait before initiating the connection, or infinite wait if set to 0.

Curlopt_connecttimeout_ms

The time, in milliseconds, to try to connect the wait. If set to 0, the wait is infinite.

Added in Curl 7.16.2. Available starting from PHP 5.2.3.

Curlopt_dns_cache_timeout

Sets the time to save DNS information in memory by default of 120 seconds.

Curlopt_ftpsslauth

FTP authentication method: Curlftpauth_ssl (first try SSL), Curlftpauth_tls (try TLS first) or curlftpauth_default (let curl decide automatically).

Added in Curl 7.12.2.

Curlopt_http_version

Curl_http_version_none (default, let CURL determine which version to use), CURL_HTTP_VERSION_1_0 (enforces http/1.0) or curl_http_version_1_1 (enforces HTTP /1.1).

Curlopt_httpauth

The HTTP authentication methods used, the optional values are: Curlauth_basic, Curlauth_digest, Curlauth_gssnegotiate, CURLAUTH_NTLM, Curlauth_any, and Curlauth_ Anysafe.

You can use the | bit field (OR) operator to separate multiple values, and curl lets the server select one of the best supported values.

Curlauth_any equivalent to Curlauth_basic | Curlauth_digest | Curlauth_gssnegotiate | CURLAUTH_NTLM.

Curlauth_anysafe equivalent to Curlauth_digest | Curlauth_gssnegotiate | CURLAUTH_NTLM.

Curlopt_infilesize

Sets the size limit for the upload file, in bytes (byte).

Curlopt_low_speed_limit

When the transfer speed is less than Curlopt_low_speed_limit (bytes/sec), PHP will determine if it is too slow to cancel the transfer, depending on the curlopt_low_speed_time.

Curlopt_low_speed_time

When the transfer speed is less than Curlopt_low_speed_limit (bytes/sec), PHP will determine if it is too slow to cancel the transfer, depending on the curlopt_low_speed_time.

Curlopt_maxconnects

The maximum number of connections allowed, which is over Curlopt_closepolicy, determines which connections should be stopped.

Curlopt_maxredirs

Specify the maximum number of HTTP redirects that are used with curlopt_followlocation.

Curlopt_port

Used to specify the connection port. (option available)

Curlopt_protocols

The bit field of the curlproto_* refers to. If enabled, the bit-domain value qualifies libcurl for the protocols that are available for use during transmission. This will allow you to support a number of protocols when compiling libcurl, but the limit is just a subset of what is allowed in them. The default libcurl will use all of the protocols it supports. See Curlopt_redir_protocols.

The available protocol options are: Curlproto_http, Curlproto_https, Curlproto_ftp, Curlproto_ftps, CURLPROTO_SCP, Curlproto_sftp, CURLPROTO_ TELNET, Curlproto_ldap, Curlproto_ldaps, Curlproto_dict, Curlproto_file, Curlproto_tftp, Curlproto_all

Added in Curl 7.19.4.

Curlopt_proxyauth

How HTTP proxy connections are validated. Use the bit field flags in Curlopt_httpauth to set the appropriate options. For proxy authentication only Curlauth_basic and CURLAUTH_NTLM are currently supported.

Added in Curl 7.10.7.

Curlopt_proxyport

The port of the proxy server. The port can also be set in Curlopt_proxy.

Curlopt_proxytype

Not curlproxy_http (the default) is CURLPROXY_SOCKS5.

was added in Curl 7.10.

Curlopt_redir_protocols

The bit field value in the curlproto_*. If enabled, the bit-domain value restricts the protocol that the transport thread can use when Curlopt_followlocation is turned on to follow a redirect. This will enable you to restrict the transport thread to use a subset of the allowed protocols when redirecting the default Libcurl will allow all protocols except file and SCP. This and 7.19.4 pre-release versions are unconditionally followed by a number of different supported protocols. For agreement constants, refer to Curlopt_protocols.

Added in Curl 7.19.4.

Curlopt_resume_from

Passes a byte offset (used to continue the breakpoint) when the transfer is resumed.

Curlopt_ssl_verifyhost

1 Check that there is a common name (common name) in the server SSL certificate. Note: The common name (Common name) is generally filled in with the domain name (domain) or subdomain (sub domain) where you will be applying for an SSL certificate. 2 checks if the common name exists and matches the host name provided.

Curlopt_sslversion

The SSL version used (2 or 3). By default, PHP detects this value by itself, although in some cases it needs to be set manually.

Curlopt_timecondition

If edited after a time specified by Curlopt_timevalue, returns a page using Curl_timecond_ifmodsince, if it has not been modified, and Curlopt_header is true, returns a 304 not Modified "header, Curlopt_header is false, the curl_timecond_ifunmodsince is used, and the default value is Curl_timecond_ifunmodsince.

Curlopt_timeout

Sets the maximum number of seconds that curl is allowed to execute.

Curlopt_timeout_ms

Sets the maximum number of milliseconds that curl allows to execute.

Added in Curl 7.16.2. can be used from PHP 5.2.3.

Curlopt_timevalue

Sets the timestamp used by a curlopt_timecondition, using curl_timecond_ifmodsince in the default state.

Third Category:

For the optional parameters of these options below, value should be set to a string type:

Options

Optional value values

Note

Curlopt_cainfo

A file name that holds 1 or more certificates used to authenticate the server. This parameter is only meaningful when used with Curlopt_ssl_verifypeer.

Curlopt_capath

A directory that holds multiple CA certificates. This option is used in conjunction with Curlopt_ssl_verifypeer.

Curlopt_cookie

Set the contents of the "Cookie:" section of the HTTP request. Multiple cookies are separated by semicolons, with a semicolon followed by a space (for example, "fruit=apple; Colour=red ").

Curlopt_cookiefile

The file name that contains the cookie data, the format of the cookie file can be in Netscape format, or just plain HTTP header information is stored in the file.

Curlopt_cookiejar

The file where the cookie information is saved after the connection ends.

Curlopt_customrequest

Use a custom request message instead of "GET" or "HEAD" as an HTTP request. This is for executing "DELETE" or other more covert HTTP requests. Valid values such as "GET", "POST", "CONNECT" and so on. In other words, do not enter the entire HTTP request here. For example, entering "get/index.html http/1.0\r\n\r\n" is incorrect.

Note:

Do not use before you determine the method that the server supports for this custom request.

Curlopt_egdsocket

Similar to Curlopt_random_file, except for a entropy gathering daemon socket.

Curlopt_encoding

The value of "accept-encoding:" in the HTTP request header. The supported encodings are "identity", "deflate" and "gzip". If an empty string "", the request header sends all supported encoding types.

was added in Curl 7.10.

Curlopt_ftpport

This value will be used to obtain the IP address required for the FTP "POST" instruction. The "POST" command tells the remote server to connect to the IP address we specify. This string can be a plain text IP address, host name, a network interface name (under Unix), or just a '-' to use the default IP address.

Curlopt_interface

The network send interface name, which can be an interface name, an IP address, or a host name.

Curlopt_krb4level

KRB4 (Kerberos 4) security level. Any of the following values are valid (from low to High): "Clear", "safe", "confidential", "private". If the string does not match these, "private" will be used. When this option is set to NULL, KRB4 security authentication is disabled. Currently KRB4 security authentication can only be used for FTP transmission.

Curlopt_postfields

All data is sent using the "POST" action in the HTTP protocol. To send a file, precede the file name with the @ prefix and use the full path. This parameter can be urlencoded after the string resembles ' para1=val1¶2=val2& ... ' or using a field named as the key value, the field data is an array of values. If value is an array, the Content-type header will be set to Multipart/form-data.

Curlopt_proxy

HTTP proxy channel.

Curlopt_proxyuserpwd

A string that is used to connect to the agent's "[Username]:[password]" format.

Curlopt_random_file

A file name that is used to generate an SSL random number seed.

Curlopt_range

In the "X-y" form, where X and Y are options to get the range of data, in bytes. The HTTP transport thread also supports several such duplicates separated by commas such as "x-y,n-m".

Curlopt_referer

The contents of "Referer:" in the HTTP request header.

Curlopt_ssl_cipher_list

A list of SSL encryption algorithms. For example, Rc4-sha and TLSV1 are available encrypted lists.

Curlopt_sslcert

A file name that contains the PEM format certificate.

curlopt_sslcertpasswd

The password required to use the Curlopt_sslcert certificate.

Curlopt_sslcerttype

The type of certificate. The supported formats are "PEM" (the default), "DER" and "ENG".

Added in Curl 7.9.3.

Curlopt_sslengine

The cryptographic engine variable used to specify the SSL private key in Curlopt_sslkey.

Curlopt_sslengine_default

A variable used for asymmetric cryptographic operations.

Curlopt_sslkey

The file name that contains the SSL private key.

curlopt_sslkeypasswd

The password for the SSL private key specified in Curlopt_sslkey.

Note:

Since this option contains sensitive password information, remember to keep this PHP script secure.

Curlopt_sslkeytype

The encryption type of the private key specified in Curlopt_sslkey, supported by the key type "PEM" (the default), "DER" and "ENG".

Curlopt_url

The URL address you need to get, or you can set it in the Curl_init () function.

Curlopt_useragent

A string that contains a "User-agent:" header in the HTTP request.

Curlopt_userpwd

Pass in a connection the user name and password required in the format: "[Username]:[password]".

Class Fourth

For the following optional parameters of option, value should be set to an array:

Options

Optional value values

Note

Curlopt_http200aliases

200 response Code Array, the response in the array is considered to be the correct response, otherwise it is considered to be wrong.

Added in Curl 7.10.3.

Curlopt_httpheader

An array that is used to set the HTTP header field. The array is set using the following form: Array (' Content-type:text/plain ', ' content-length:100′ ')

Curlopt_postquote

After the FTP request execution completes, a set of FTP commands are executed on the server.

Curlopt_quote

A set of FTP commands that are executed on the server prior to FTP requests.

For the following optional parameters of option, value should be set to a stream resource (for example, using fopen ()):

Options

Optional value values

Curlopt_file

Sets the location of the output file, the value is a resource type, and the default is stdout (browser).

Curlopt_infile

The file address to be read when uploading the file, the value is a resource type.

Curlopt_stderr

Sets an error output address, which is a resource type that supersedes the default stderr.

Curlopt_writeheader

Sets the file address to write the header portion of the content, and the value is a resource type.

For the following optional parameters of option, value should be set to a callback function name:

Options

Optional value values

Curlopt_headerfunction

Set a callback function that has two parameters, the first is the resource handle for curl, and the second is the header data for the output. The output of the header data must rely on this function to return the size of the data that has been written.

Curlopt_passwdfunction

Set a callback function with three parameters, the first one is the resource handle for curl, the second is a password prompt, and the third parameter is the maximum allowed for the password length. Returns the value of the password.

Curlopt_progressfunction

Set a callback function with three parameters, the first is the resource handle for curl, the second is a file descriptor resource, and the third is the length. Returns the contained data.

Curlopt_readfunction

A callback function with two parameters, the first of which is the session handle, and the second is the string of HTTP response header information. With this function, the returned data is processed on its own. The return value is the data size, in bytes. Returns 0 represents the EOF signal.

Curlopt_writefunction

A callback function with two parameters, the first of which is the session handle, and the second is the string of HTTP response header information. With this callback function, the response header information is processed on its own. The response header information is the entire string. Sets the return value to the exact length of the written string. The transport thread terminates when an error occurs.

  • 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.