PHP4 User manual: Function->curl

Source: Internet
Author: User
Tags curl http post
PHP4 User manual: Function->curl
[This page recommends browsing under 1024x768 resolution]
Article Category: Other development languages
Reproduced from: www.csdn.net

XI. CURL, Customer URL library function

PHP supports Libcurl (allows you to connect and communicate with different servers in different protocols). , Libcurl currently supports HTTP, HTTPS, FTP, Gopher, Telnet, dict, file, and LDAP protocols. Libcurl also supports HTTPS certificate authorization, HTTP POST, HTTP put, FTP upload (of course you can also use PHP FTP extensions), HTTP basic form uploads, proxies, cookies, and user authentication.

In order to use the Curl function you need to install the Curl package. PHP requires you to use a curl 7.0.2-beta or higher version. If the curl version is lower than the 7.0.2-beta,php will not work.

To use PHP's curl support, you must recompile PHP with the--with-curl[=dir parameter (DIR is the directory that contains the library and header files).

These functions are added to the PHP 4.0.2.

Once you have compiled PHP with Curl support, you can use the Curl function. The basic idea is that you use the curl_init () function to initialize the curl session, and then you can set all your options, run it through the curl_exec () function, and finally you can function curl_close () function to end your session. Here is an example: the PHP home page is retrieved and placed in a file.

Example 1. Use PHP's Curl module to retrieve the PHP home page

<?php

$ch = Curl_init ("http://www.php.net/");
$fp = fopen ("Php_homepage.txt", "w");

curl_setopt ($ch, Curlopt_file, $fp);
curl_setopt ($ch, Curlopt_header, 0);

Curl_exec ($ch);
Curl_close ($ch);
Fclose ($FP);
? >
Directory Listings curl_init-Initialization of a curl sessioncurl_setopt-set an option for the curl callCurl_exec-executes a curl sessioncurl_close-closes a curl sessioncurl_version-returns the current Curl version

Curl_init

(PHP 4 >= 4.0.2) Curl_init--Initializes a Curl session description

 

int curl_init ([string URL])

 

The curl_init () function Initializes a new session, returns a curl handle for curl_setopt (), curl_exec (), and the curl_close () function is used. If an optional parameter is provided, the CURLOPT_URL option is set to the value of this parameter. You can use the curl_setopt () function to manually set.

Example 1. Initialize a new Curl session and retrieve a Web page

<?php
$ch = Curl_init ();

curl_setopt ($ch, Curlopt_url, "http://www.zend.com/");
curl_setopt ($ch, Curlopt_header, 0);

Curl_exec ($ch);

Curl_close ($ch);
? >

curl_setopt

(PHP 4 >= 4.0.2) curl_setopt--Set an option description for Curl invocation

 

BOOL curl_setopt (INT-ch, string option, mixed value)

 

The curl_setopt () function sets the option for a curl session. The option parameter is the setting you want, and value is the values given by this choice.

The values of the following options will be used as long reshaping (specified in the option argument):

 

Curlopt_infilesize: When you upload a file to a remote site, this option tells PHP the size of the file you uploaded.

Curlopt_verbose: If you want to curl report every unexpected thing, set this option to a value other than 0.

Curlopt_header: If you want to include a header in the output, set this option to a value other than 0.

Curlopt_noprogress: If you don't have PHP to display a process bar for curl transmissions, set this option to a non 0 value.

Note: PHP automatically sets this option to a value other than 0, and you should only change this option for debugging purposes.

Curlopt_nobody: If you don't want to include the body part in the output, set this option to a value other than 0.

Curlopt_failonerror: If you want PHP to be in error (HTTP code returns greater than or equal to 300), do not display, set this option to a person not 0 value. The default behavior is to return a normal page, ignoring the code.

Curlopt_upload: If you want PHP to be ready for upload, set this option to a value other than 0.

Curlopt_post: If you want PHP to do a regular HTTP POST, set this option to a non 0 value. This post is an ordinary application/x-www-from-urlencoded type, most of which is used by HTML forms.

Curlopt_ftplistonly: Set this option to a value other than 0, PHP will list the list of directory names for FTP.

Curlopt_ftpappend: Set this option to a value other than 0, PHP will apply the remote file instead of overwriting it.

CURLOPT_NETRC: Set this option to a value other than 0, PHP will find the username and password of the remote site you want to connect to in your ~./netrc file.

Curlopt_followlocation: Set this option to a non 0 value (like "Location:"), the server will send it as a part of the HTTP header (note that this is recursive, PHP will send the shape as "Location:" the head).

Curlopt_put: Set this option for a non 0 value to upload a file with HTTP. To upload this file, you must set the Curlopt_infile and Curlopt_infilesize options.

Curlopt_mute: Set this option to a value other than 0, PHP will be completely silent for the curl function.

Curlopt_timeout: Sets a long shaping number, as the maximum continuation of how many seconds.

Curlopt_low_speed_limit: Sets a long shaping number to control how many bytes are transferred.

Curlopt_low_speed_time: Set a long shaping number, control how many seconds to transfer curlopt_low_speed_limit The specified number of bytes.

Curlopt_resume_from: Passes a long shaping parameter that contains the byte offset address (the start form you want to transfer to).

Curlopt_sslversion: Passes a long parameter containing the SSL version. The default PHP will be determined by its own efforts, and in more security you must set it manually.

Curlopt_timecondition: Passes a long parameter specifying how to handle the Curlopt_timevalue parameter. You can set this parameter to Timecond_ifmodsince or timecond_isunmodsince. This is for HTTP only.

Curlopt_timevalue: Passes a number of seconds from 1970-1-1 to the present. This time is used by the Curlopt_timevalue option as the specified value or by default timecond_ifmodsince.

The values of the following options will be used as strings:

 

Curlopt_url: This is the URL you want to retrieve with PHP. You can also set this option when initializing with the curl_init () function.

Curlopt_userpwd: Pass a string in a form like [Username]:[password] style, the function of PHP to connect.

Curlopt_proxyuserpwd: Passes a string in a form like [Username]:[password] to connect to the HTTP proxy.

Curlopt_range: Pass a range that you want to specify. It should be "XY" format, X or Y is excepted. HTTP transmissions also support several intervals, separated by a x-y,n-m.

Curlopt_postfields: Passes a string of all data that is an HTTP "POST" operation.

Curlopt_referer: Contains a string of "REFERER" headers in the HTTP request.

Curlopt_useragent: Contains a string of "user-agent" headers in the HTTP request.

Curlopt_ftpport: Passing a containing by F

curl_exec

(PHP 4 >= 4.0.2) curl_exec--Perform a Curl session description

BOOL curl_exec (int ch)

After you initialize a curl session and set all the options for this session, this function will be invoked. It is intended only to perform a predetermined curl session (with the given CH parameter).

curl_version

(PHP 4 >= 4.0.2) Curl_version--Returns the current Curl version description

 

String curl_version (void)

 

The curl_version () function returns a string containing the curl version.

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.