PHP curl_PHP tutorial

Source: Internet
Author: User
PHP curl. PHP curl when I first came into contact with curl, I read the documentation and various online search materials, official (cn2.php. netmanualenintro. curl. php) the explanation is that this is a PHP curl written by a Daniel.
When I first came into contact with curl, I read the documentation, as well as various online search materials, the official (http://cn2.php.net/manual/en/intro.curl.php) explanation is that this is a libcurl library written by a Daniel, PHP supports this extension Library, allow us to use different protocols (http, https, ftp, telnet, file ...) access different servers, support post, put, ftp or form-based file upload, and support for cookies and proxies.

I think the simplest explanation is that curl is a tool set supported by the libcurl extension library and contains some functions. using these functions, we can simulate access to some addresses, that is, we manually enter the http://www.baidu.com in the browser address bar to access Baidu, curl can replace this manual operation, in the form of a program to achieve this process, the result of this operation and process is that the browser gave us a homepage for Baidu search.

Since curl is a tool set and many functions are available for calling, you can imagine entering a URL in the address bar (some parameters may be included). when you press enter, it is equivalent to calling some functions. these functions are written in scripts on one or more servers of Baidu. after a function is run, some results will be returned, such as a value or no return value, you can also print some things and present a page here for you. curl provides much more functions than simply displaying a page, these different functions are implemented through the curl_setopt function. For example, when you connect to the SDK, if someone else provides an API in the form of an access address, then curl will show itself.

1. get implementation

$ Url = 'http: // www.somesite.com ';

$ Data = array ('username' => 'Peter ', 'password' => 12345 );

Function get ($ url, $ data = array ()){

$ Ch = curl_init (); // initialize a curl resource type variable

/* Set access options */

Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, true); // When enabled, the Location returned by the server is recursively returned to the server in the header.

Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // return the obtained data instead of outputting it directly on the page.

Curl_setopt ($ ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP); // Set the protocol type of the access address to HTTP

Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 15); // The access timeout period is limited to 15 s.

$ Url = url .'? '. Http_build_query ($ data );

Curl_setopt ($ ch, CURLOPT_URL, $ url); // you can specify the URL to be accessed.

$ Result = curl_exec ($ ch); // execute this access and return a result.

//... // Perform operations on whether the result is correct or not.

Return $ result;

}

// $ Result = get (% url, $ data); // call

2. post implementation

Function post ($ url, $ data = array ()){

$ Ch = curl_init ();

Curl_setopt ($ ch, CURLOPT_POST, true); // It is set to the post transmission form.

Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); // sets the data transmitted by post.

Curl_setopt ($ ch, CURLOPT_URL, $ url );

Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // sets the data to be returned as a file stream

Curl_setopt ($ ch, CURLOPT_USERAGENT, ''); // leave the user agent blank

Curl_setopt ($ ch, CURLOPT_HEADER, false); // set not to display header information

$ Result = curl_exec ($ ch );

$ Info = curl_getinfo ($ ch); // obtain information about the resource to be accessed.

Return $ result;

}

When I first came into contact with curl, I read the documentation and various online search materials. the official (http://cn2.php.net/manual/en/intro.curl.php) explanation was that this was written by a Daniel...

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.