Home > Backend Development > PHP Tutorial > php之curl实现http与https请求的方法_PHP

php之curl实现http与https请求的方法_PHP

WBOY
Release: 2016-05-31 19:29:04
Original
1053 people have browsed it

本文实例讲述了php之curl实现http与https请求的方法,分享给大家供大家参考。具体如下:

通常来说,php的curl函数组可以帮助我们把机器伪装成人的行为来抓取网站,下面来分享两个例子,一个是访问http网页,一个访问https网页,一起来看一下。

每次要使用curl的时候,总要查一堆资料。
现在将常用的几句保存下来,省的每次都去谷歌。

常规curl请求:

代码如下:

$url = 'http://www.bitsCN.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);


使用curl请求HTTPS:

代码如下:

$url = 'https://www.bitsCN.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);//这个是重点。
$data = curl_exec($curl);
curl_close($curl);
var_dump($data);

注意

当请求https的数据时,会要求证书,这时候,加上下面这两个参数,规避ssl的证书检查

代码如下:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

希望本文所述对大家的PHP程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template