A guide to using Curl in PHP, Phpcurl usage Guide
Many students feel the first two big (including me) when using curl, and look at this curl_setopt function completely puzzled, but after you spend 10 minutes to read my introduction I believe you can easily tease Php curl.
First, take a look at a Curl code (spend 10 seconds, glance over it, and then skip to the following article)
Copy CodeThe 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 the hell is this?
Want to learn this "high-end" usage?
First of all, I'm sure you know that most of the URLs start with HTTP, because they need to be transmitted via HTTP (Hypertext Transfer Protocol Http-hypertext Transfer Protocol), but the data transfer is not simply a "Hello" To the server on the matter, the sender in order to facilitate the recipient to understand the sender's actual intentions and know who the sender is also, the sender is often to send a lot of additional information to the recipient, just as the sender needs in the letter coat an envelope, the envelope is written with various sender information. All of these eventually merge into a thing called message, which forms the basis of the entire Internet.
Curl's job is to send these messages over the HTTP protocol (PHP Libcurl currently supports other protocols such as HTTPS, FTP, Telnet, etc.)
Now look at the code, actually the code did only five things
Curl_init () Initialize Curl
curl_setopt () Setting transfer data and parameters
Curl_exec () performs the transfer and obtains the return data
Curl_errono () return error code
Curl_close () Turn off Curl
Here's how to crawl and submit any page data using the Get and post methods
Copy the Code code as follows:
<?php
Initialization
$curl = Curl_init ();
Set URL
curl_setopt ($curl, Curlopt_url, ' http://www.baidu.com ');
Setting returns the output obtained as a text stream
curl_setopt ($curl, Curlopt_returntransfer, true);
Execute command
$data = curl_exec ($curl);
Close URL Request
Curl_close ($curl);
Show the data obtained
Print_r ($data);
?>
<?php
Initialization
$curl = Curl_init ();
Set URL
curl_setopt ($curl, Curlopt_url, ' http://www.baidu.com ');
Setting returns the output obtained as a text stream
curl_setopt ($curl, Curlopt_returntransfer, true);
Set Post submission
curl_setopt ($curl, Curlopt_post, 1);
Setting up post data
curl_setopt ($curl, Curlopt_postfields, Array ("Data" = "value");
Execute command
$data = curl_exec ($curl);
Close URL Request
Curl_close ($curl);
Print data
Print_r ($data);
?>
Interested students can also refer to the official PHP document, learn more curl usage
http://www.bkjia.com/PHPjc/954100.html www.bkjia.com true http://www.bkjia.com/PHPjc/954100.html techarticle The Curl Usage Guide in PHP, Phpcurl usage Guide Many students feel the first two large (including me) when using curl, and look at the curl_setopt function of this strip completely touch ...