How PHP's curl implements http and https requests

The example in this article describes how PHP's curl implements http and https requests, and shares it with you for your reference. details as follows:

Generally speaking, php's curl function group can help us to disguise the behavior of a machine to crawl a website. Let's share two examples, one is to access http pages, the other is to access https pages, let's take a look.

 

Regular curl request:

$url = 'http://www.jb51.net';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);
var_dump ($ data);

Request HTTPS using curl: 

$url = 'https://www.jb51.net';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//This is the point.
$data = curl_exec($curl);
curl_close($curl);
var_dump ($ data);

 

Notice

When requesting https data, a certificate will be required. At this time, add the following two parameters to avoid the certificate check of ssl

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https requests do not verify certificates and hosts
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

 

I hope this article will help you in your PHP programming.

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326396573&siteId=291194637