Home > Article > Backend Development > PHP curl implements http and https request examples_PHP tutorial
Every time you want to use curl, you always have to check a lot of information.
Now save a few frequently used sentences to save you from having to go to Google every time.
Regular curl request:
代码如下 | |
$url = 'http://www.111cn.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); |
Use curl to request HTTPS:
代码如下 | |
$url = 'https://www.111cn.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);//这个是重点。 $data = curl_exec($curl); curl_close($curl); var_dump($data); |
Attention
When requesting https data, a certificate will be required. At this time, add the following two parameters to avoid SSL certificate checking
代码如下 | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); |