Home > Backend Development > PHP Tutorial > Curl usage guide in php, phpcurl usage guide_PHP tutorial

Curl usage guide in php, phpcurl usage guide_PHP tutorial

WBOY
Release: 2016-07-13 10:07:58
Original
888 people have browsed it

Guide to using curl in php, Guide to using phpcurl

Many students feel overwhelmed when they first use curl (including me). They are completely confused looking at the curl_setopt function. But after you spend 10 minutes reading my introduction, I believe you can easily play with PHP’s curl in the future

First, please look at a curl code (take 10 seconds to read it briefly, then skip to the following)

Copy code The code is as follows:

$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 on earth are you doing?

Want to learn this “high-end” usage?

First of all, I believe you must know that most URLs start with http. That is because they need to use http (Hypertext transfer protocol) to transmit data, but transmitting data is not a simple matter of sending a sentence. "Hello" is sent to the server. In order for the recipient to understand the actual intention of the sender and know who the sender is, the sender often sends a lot of additional information to the recipient, like The sender needs to put an envelope around the letter, with various sender's information written on the envelope. All of these were eventually combined into something called a message, which formed the basis of the entire Internet.

The job of curl is to send these messages through the http protocol (php's libcurl currently also supports https, ftp, telnet and other protocols)

Looking at the code now, the code actually only does five things

curl_init() initializes curl
curl_setopt() sets transfer data and parameters
curl_exec() performs the transfer and gets the return data
curl_errono() returns error code
curl_close() close curl
Below is how to crawl and submit data from any page using the GET and POST methods

Copy code The code is as follows:

//Initialization
$curl = curl_init();
//Set url
curl_setopt($curl, CURLOPT_URL, 'http://www.baidu.com');
//Set the returned output as a text stream
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//Execute command
$data = curl_exec($curl);
//Close URL request
curl_close($curl);
//Display the obtained data
Print_r($data);
?>
//Initialization
$curl = curl_init();
//Set url
curl_setopt($curl, CURLOPT_URL, 'http://www.baidu.com');
//Set the returned output as a text stream
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//Set the post method to submit
curl_setopt($curl, CURLOPT_POST, 1);
//Set 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 documentation to learn more curl usage

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/954100.htmlTechArticlecurl usage guide in php, phpcurl usage guide. Many students feel confused when they use curl for the first time. Everyone (including me), looked at this curl_setopt function and was completely touched...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template