作者: | 来源:互联网 | 2023-09-11 11:42
http.php<?phpnamespaceappluckycommon;***ClassHttp*classHttp{privatestatic$info[];***@pa
http.php
register("curl.after_request", function ($headers, $info) {
self::$info = !is_array($info) ? [$info] : $info;
});
return $hooks;
}
private static function setOptions($options)
{
if (!isset($options["hooks"])) {
$options["hooks"] = self::getHooks();
}
$options["idn"] = false;
return $options;
}
private static function call($name, $args)
{
try {
$type = $name;
if ("request" === $name) {
$type = isset($args[3]) ? $args[3] : $name;
}
$type = strtolower($type);
//设置url
if ($type !== "request_multiple") {
$args[0] = new RequestsIRI($args[0]);
}
$respOnse= call_user_func_array(["Requests", $name], $args);
if ($response instanceof Requests_Response && is_object($response->url)) {
$response->url = $response->url->uri;
}
return $response;
} catch (Exception $e) {
Log::error($e->getMessage());
$respOnse= new Requests_Response();
$response->body = "";
return $response;
}
}
}
//业务代码
// www.cwl.gov.cn 请求头
private $cwlHeader = [
"X-Requested-With"=> "XMLHttpRequest",
"Referer" => "http://www.cwl.gov.cn/kjxx/"
];
protected function doGet($url, $urlParams = [], $headers = [], $optiOns= [])
{
$respOnse= appluckycommonHttp::request($url, $headers, $urlParams, "GET", $options);
return $response->body;
}
/**
* @param $num
* @return array
* @description 按期号爬(福彩3D)
*/
public function fcsdCrawlerByNum($num){
$url = "http://www.cwl.gov.cn/cwl_admin/kjxx/findKjxx/forIssue?name=3d&code=".$num;
$data = $this->doGet($url,[],$this->cwlHeader);
$data = json_decode($data,true);
if($data["state"] != _MSG_SUCCESS){
Redis::getInstance()->redisHSetTrue(_REDIS_CRAWLER_STATUS,"fcsdCrawlerByNum");
Log::warning("现在已经爬不到 福彩3D 开奖结果了(中国福利彩票发行管理中心官方网站 www.cwl.gov.cn) fcsdCrawlerByNum()", []);
return ["code"=>_MSG_SYSTEM_ERROR, "msg"=>$data["message"]];
}
Redis::getInstance()->redisHSetFalse(_REDIS_CRAWLER_STATUS,"fcsdCrawlerByNum");
return $this->fcsdDataFormat($data["result"]);
}
另外一种的业务逻辑PHP代码块
/**
* @param $lotteryId
* @return array
* @description 地方彩往期爬取
*/
public function dfCrawlerM500List($lotteryId)
{
$id500 = $this->fu51ToWb[$lotteryId];
$url = "https://m.500.com/info/kaijiang/moreexpect/" . $id500 . "/?from=";
$result = $this->doGet($url, [], $this->m500Header);
$encode = mb_detect_encoding($result, array("ASCII", "UTF-8", "GB2312", "GBK", "BIG5"));
$result = mb_convert_encoding($result, "UTF-8", $encode);
$pattern = "/.*?([0-9]{5,10}).*?([0-9-]{10}).*?s*