Php curl User Guide _ php instance

Source: Internet
Author: User
This article mainly introduces the curl User Guide in php, which is very detailed. If you need it, you can refer to the first two major cases (including me) when using curl for the first time ), looking at these curl_setopt functions is completely confusing, but after you spend 10 minutes reading my introduction, I believe you will be able to play with php curl easily in the future.

First, read a curl code (it takes 10 seconds to read it again, and then jump to the next article)

The Code is as follows:


<? Php
$ Data =" [...] ";
$ TuCurl = curl_init ();
Curl_setopt ($ tuCurl, CURLOPT_URL, "https://example.com/path/for/soap/url ");
Curl_setopt ($ tuCurl, CURLOPT_PORT, 443 );
Curl_setopt ($ tuCurl, CURLOPT_VERBOSE, 0 );
Curl_setopt ($ tuCurl, CURLOPT_HEADER, 0 );
Curl_setopt ($ tuCurl, CURLOPT_SSLVERSION, 3 );
Curl_setopt ($ tuCurl, CURLOPT_SSLCERT, getcwd (). "/client. pem ");
Curl_setopt ($ tuCurl, CURLOPT_SSLKEY, getcwd (). "/keyout. pem ");
Curl_setopt ($ tuCurl, CURLOPT_CAINFO, getcwd (). "/ca. pem ");
Curl_setopt ($ tuCurl, CURLOPT_POST, 1 );
Curl_setopt ($ tuCurl, CURLOPT_SSL_VERIFYPEER, 1 );
Curl_setopt ($ tuCurl, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ tuCurl, CURLOPT_POSTFIELDS, $ data );
Curl_setopt ($ tuCurl, CURLOPT_HTTPHEADER, array ("Content-Type: text/xml", "SOAPAction: \"/soap/action/query \ "", "Content-length: ". strlen ($ data )));
$ TuData = curl_exec ($ tuCurl );
If (! Curl_errno ($ tuCurl )){
$ Info = curl_getinfo ($ tuCurl );
Echo 'took'. $ info ['total _ time']. 'Seconds to send a request to '. $ info ['url'];
} Else {
Echo 'curl error: '. curl_error ($ tuCurl );
}
Curl_close ($ tuCurl );
Echo $ tuData;
?>

WTF, what is this?

Do you want to learn this "high-end" usage?

First of all, I believe you must know that most websites start with http because they need to transmit data through http (Hypertext transfer protocol HTTP-Hypertext transfer protocol, however, it is not easy to transmit a "Hello" sentence to the server. The sender understands the actual intention of the sender and knows who the sender is, the sender often sends a large amount of additional information to the recipient, just as the sender needs to write a variety of sender information on the envelope. All these are combined into a message, which forms the foundation of the entire internet.

Curl sends these messages over http (php libcurl currently supports https, ftp, telnet, and other protocols)

Now let's look at the code. Actually, the Code only does five things.

Curl_init () initializes curl
Curl_setopt () sets the transmission data and Parameters
Curl_exec () executes transmission and obtains the returned data.
Curl_errono () returns the error code
Curl_close () Close curl
The following describes how to capture and submit any page data using the GET and POST methods.

The Code is as follows:


<? Php
// Initialization
$ Curl = curl_init ();
// Set the url
Curl_setopt ($ curl, CURLOPT_URL, 'HTTP: // www.baidu.com ');
// Set the returned output to a text stream
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, true );
// Execute the command
$ Data = curl_exec ($ curl );
// Close the URL request
Curl_close ($ curl );
// Display the obtained data
Print_r ($ data );
?>
<? Php
// Initialization
$ Curl = curl_init ();
// Set the url
Curl_setopt ($ curl, CURLOPT_URL, 'HTTP: // www.baidu.com ');
// Set the returned output to a text stream
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, true );
// Set the post method for submission
Curl_setopt ($ curl, CURLOPT_POST, 1 );
// Set post Data
Curl_setopt ($ curl, CURLOPT_POSTFIELDS, array ("data" => "value ");
// Execute the command
$ Data = curl_exec ($ curl );
// Close the URL request
Curl_close ($ curl );
// Print data
Print_r ($ data );
?>

For more information about curl usage, see the official php documentation.

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.