This article mainly introduces the curl User Guide in php, which is very detailed. For more information, see
This article mainly introduces the curl User Guide in php, which is very detailed. For more information, see
When I used curl for the first time, many of my colleagues felt like the first two big ones (including me). They were totally confused by the curl_setopt function, however, after reading my introduction for 10 minutes, I believe you will be able to play with php curl easily in the future.
First, read a curl code (it takes 10 seconds, 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.