This article mainly introduces how php curl implements http and https requests, and describes the PHP access to http webpages and access to https webpages, as well as related precautions, for more information, see
This article mainly introduces how php curl implements http and https requests, and describes the PHP access to http webpages and access to https webpages, as well as related precautions, for more information, see
This article describes how to implement http and https requests using php curl. The details are as follows:
Generally, the php curl function group can help us pretend to be an adult and capture the website. Here are two examples: accessing an http webpage and accessing an https webpage, let's take a look.
Each time you use curl, you always need to check a bunch of data.
Now we can save a few frequently used sentences, saving every time we go to Google.
Common curl requests:
The Code is as follows:
$ 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 );
Use curl to request HTTPS:
The Code is as follows:
$ 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 focus.
$ Data = curl_exec ($ curl );
Curl_close ($ curl );
Var_dump ($ data );
Note:
When requesting https data, the certificate is required. At this time, the following two parameters are added to avoid ssl certificate check.
The Code is as follows:
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https requests do not verify the certificate and hosts
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, FALSE );
I hope this article will help you with PHP programming.