Home >Backend Development >PHP Tutorial >How to implement http and https requests using curl in php, phpcurlhttps request_PHP tutorial

How to implement http and https requests using curl in php, phpcurlhttps request_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:16:251114browse

How php curl implements http and https requests, phpcurlhttps request

The example in this article describes how curl in PHP implements http and https requests, and is shared with you for your reference. The details are as follows:

Generally speaking, PHP's curl function group can help us disguise machines as human behavior to crawl websites. Let's share two examples, one is to access http web pages, and the other is to access https web pages. Let's take a look.

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 Google them every time.

Regular curl request:

Copy code The code is as follows:
$url = 'http://www.bkjia.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);

Use curl to request HTTPS:

Copy code 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 key point.
$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

Copy code The code is as follows:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https request does not verify the certificate and hosts
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

I hope this article will be helpful to everyone’s PHP programming design.

Problem using CURL in PHP to submit POST data to https website

The error message is that the https protocol cannot be supported.
stackoverflow.com/…-https saved me, thank God.
It says there is an extra space in front of https

For websites with https protocol, you can use PHP’s curl to simulate get and post. Can you get the return value?

Yes.
CURLOPT_PROTOCOLS
Bitfield refers to CURLPROTO_*. If enabled, the bitfield value limits which protocols libcurl can use during transfers. This will allow you to compile libcurl to support many protocols, but only to use a subset of them that are allowed to be used. By default libcurl will use all protocols it supports. See CURLOPT_REDIR_PROTOCOLS.
The available protocol options are: CURLPROTO_HTTP, CURLPROTO_HTTPS, CURLPROTO_FTP, CURLPROTO_FTPS, CURLPROTO_SCP, CURLPROTO_SFTP, CURLPROTO_TELNET, CURLPROTO_LDAP, CURLPROTO_LDAPS, CURLPROTO_DICT, CURLPROTO_FILE, CURLPROTO _TFTP, CURLPROTO_ALL

Yes, you can definitely get it Return value

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/897689.htmlTechArticleHow to implement http and https requests using curl in php, phpcurlhttps requests This article describes how to implement http and https using curl in php The request method is shared with everyone for your reference. The details are as follows:...
Statement:
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