Method for realizing http and https requests by curl of php

  • 2021-07-22 09:19:05
  • OfStack

This article describes the example of php curl implementation of http and https request method, to share for your reference. The details are as follows:

Generally speaking, curl function group of php can help us disguise the machine as human behavior to crawl websites. Here are two examples, one is to visit http webpage, one is to visit https webpage, and one is to look at one.

Every time you want to use curl, you always need to check a pile of data.
Now save a few commonly used words, so that you can go to Google every time.

General curl request:

$url = 'https://www.ofstack.com';
$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.ofstack.com';
$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);

Attention

When https data is requested, the certificate will be required. At this time, the following two parameters are added to avoid ssl certificate checking

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

I hope this article is helpful to everyone's PHP programming.


Related articles: