前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PHP_cURL初始化和执行方法

PHP_cURL初始化和执行方法

作者头像
老高的技术博客
发布2022-12-27 15:33:42
3950
发布2022-12-27 15:33:42
举报
文章被收录于专栏:老高的技术博客

这个是采集基础,最好熟悉一下

代码语言:javascript
复制
$ch = curl_init();
# 设定url和把结果返回,是否返回头部
curl_setopt($ch, CURLOPT_URL, 'http://www.baidu.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->ch, CURLOPT_HEADER, 1);

# cookie文件设定
curl_setopt($this->ch, CURLOPT_COOKIEJAR,  $cookie_file);
curl_setopt($this->ch, CURLOPT_COOKIEFILE, $cookie_file);

# 额外头部
curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('User-Agent: Mozilla/5.0'));

# 设定post
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $poststring);

# 连接、执行过期时间
curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($this->ch, CURLOPT_TIMEOUT, 30);

# 是否跟随301 302
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->ch, CURLOPT_MAXREDIRS, 10);

# refer
curl_setopt($this->ch, CURLOPT_REFERER, $refer);

# http版本和端口重用设置
curl_setopt($this->ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($this->ch, CURLOPT_FORBID_REUSE, 1);

# 支持https
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 0);

# 如果需要进行毫秒超时,需要增加:
curl_setopt($this->ch, CURLOPT_NOSIGNAL, 1);

# 执行
$response = curl_exec($ch);
if(curl_errno($ch)){
    curl_error($ch);
    exit();
}
curl_close($ch);
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-03-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档