<?php
class Crawl{
public static $lastErrorNo;
public static $lastError;
public static function getCurl($url, $data=array(),$isPost = true, $header = array()){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$header && curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POST, false);
if($isPost){
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
}
$response = curl_exec($curl);
if(self::$lastErrorNo = curl_errno($curl)){
self::$lastError = curl_error($curl);
}
curl_close($curl);
return $response ;
}
public static function getStream($url,$data=array(),$type="POST",$timeout=60){
$data = http_build_query($data);
$options = array(
'http' => array(
'method' => $type,
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $data,
'timeout' => $timeout,// 超时时间(单位:s)
),
);
$context = stream_context_create($options,array());
$result = file_get_contents($url, false, $context);
return $result;
}
}Crawl 相关方法
最新推荐文章于 2025-04-17 13:42:09 发布
本文深入探讨了PHP爬虫的基本原理、实现方法及常见错误处理,包括使用cURL和stream_get_contents进行网络请求,解析响应内容,以及如何避免被目标网站封禁。
1562

被折叠的 条评论
为什么被折叠?



