Home >php教程 >php手册 >php curl实现http与https请求实例

php curl实现http与https请求实例

WBOY
WBOYOriginal
2016-06-13 09:23:521472browse

php curl实现http与https请求实例

 每次要使用curl的时候,总要查一堆资料。

现在将常用的几句保存下来,省的每次都去谷歌。

常规curl请求:

 

 代码如下  
$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);

使用curl请求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);

注意

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

 代码如下  
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
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