( ! ) Warning: include(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in D:\www\up\php\php_curl.php on line 22
Call Stack
#TimeMemoryFunctionLocation
10.0156361920{main}( )...\php_curl.php:0

( ! ) Warning: include(http://pub.houheaven.com/Nav02/Nav_deep2.htm): failed to open stream: no suitable wrapper could be found in D:\www\up\php\php_curl.php on line 22
Call Stack
#TimeMemoryFunctionLocation
10.0156361920{main}( )...\php_curl.php:0

( ! ) Warning: include(): Failed opening 'http://pub.houheaven.com/Nav02/Nav_deep2.htm' for inclusion (include_path='.;C:\php\pear') in D:\www\up\php\php_curl.php on line 22
Call Stack
#TimeMemoryFunctionLocation
10.0156361920{main}( )...\php_curl.php:0

curl是一个利用URL语法在命令行方式下工作的文件传输工具。它支持很多协议:ftp、ftps、http、https、gopher、telnet、dict、file、ldap。curl的功能有:https认证、http post方法、http put方法、FTP上传、代理服务器、cookie、用户名\密码认证、下载文件、断点续传等等,功能十分强大。


使用配置

在 PHP 中使用 CURL 的功能,需要手动开启。

在 PHP 配置文件(php.ini)中找到 ;extension=php_curl.dll,将前面的注释符号(;)去除,然后重启。


函数说明

函数说明
curl_init()初始化一个链接
curl_setopt( $ch,Name,Value )根据不同需求对链接进行不同配置
curl_exec( $ch )执行配置好的链接
curl_close( $ch )关闭一个链接

$ch:curl_ini() 返回的资源变量。

Name:预定义常量

Value:常量值

预定义常量说明取值
CURLOPT_URL设置要抓取的网址可用网址
CURLOPT_HEADER设置头信息1-显示,0-不显示
CURLOPT_RETURNTRANSFER设置网页返回数据的显示方式1-返回数据保存到字符串中,不显示
0-返回数据直接输出到屏幕上
CURLOPT_POST设置是否使用 POST 提交1-使用,0-不使用
CURLOPT_POSTFIELDS设置 POST 提交的参数提交参数(user=usr&pwd=123)
CURLOPT_REFERER设置模拟文件上传的网址任意网址
CURLOPT_COOKIEJAR保存 Cookie,保存到本地文件1.真实路径("d:\\cookie.txt")
2.相对路径("cookie.txt")
CURLOPT_COOKIEFILE读取 Cookie,从本地文件中读取1.真实路径("d:\\cookie.txt")
2.相对路径("cookie.txt")

官方预定义常量列表


实例演示

①. 简单获取

$ch=curl_init();

curl_setopt($ch,CURLOPT_URL,"www.houheaven.com");

curl_exec($ch);

curl_close($ch);


②. GET 获取

$ch=curl_init();

curl_setopt($ch,CURLOPT_URL,"www.houheaven.com/tmp.php?str=hello");

curl_exec($ch);

curl_close($ch);


③. POST 获取

$post_data="user=guest&pwd=123";

$ch=curl_init();

curl_setopt($ch,CURLOPT_URL,"www.houheaven.com/tmp.php");

curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);

curl_exec($ch);

curl_close($ch);


④. POST 上传文件

$upld_url="www.anywebsite.com";

$post_url="www.houheaven.com/tmp.php";

$post_file=array("upload"=>"@C:\\img.jpg");

$ch=curl_init();

curl_setopt($ch,CURLOPT_HEADER,0);

curl_setopt($ch,CURLOPT_REFERER,$upld_url);

curl_setopt($ch,CURLOPT_URL,$post_url);

curl_setopt($ch,CURLOPT_POST,true);

curl_setopt($ch,CURLOPT_POSTFIELDS,$post_file);

curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

curl_exec($ch);

curl_close($ch);


⑤. 获取 HTTPS 数据

$https_url="https://www.anysite.com?wid=1101";

$ch=curl_init();

curl_setopt($ch,CURLOPT_URL,$httpsurl);

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);

curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);

curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

curl_exec($ch);

curl_close($ch);


⑥. Cookie 模拟登陆

// 设置参数

$cookie_file="cookie.txt";

$post_data="user=guest&pwd=guest";

$url1="https://www.anysite.com/check.php";

$url2="https://www.anysite.com/view.php";

// 验证登录,保存 Cookie

$ch=curl_init();

curl_setopt($ch,CURLOPT_URL,$url1);

curl_setopt($ch,CURLOPT_POST,1);

curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);

curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file);

curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

curl_exec($ch);

curl_close($ch);

// 读取本地 Cookie

$ch=curl_init();

curl_setopt($ch,CURLOPT_URL,$url2);

curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie_file);

curl_setopt($ch,CURLOPT_RETURNTRANSFER,false);

curl_exec($ch);

curl_close($ch);


显示框架
显示框架
显示框架
显示框架