优化:设置curl组件 301跟随2次,解决瓷器、城市等站点的跳转问题

This commit is contained in:
david 2021-09-12 18:23:02 +08:00
parent 4e7dc9c830
commit b2b47055af
7 changed files with 39 additions and 15 deletions

View File

@ -1,6 +1,6 @@
<?php
declare(strict_types=1);
namespace IYUU\Library\Event;
namespace app\common\event;
use Throwable;
@ -8,7 +8,7 @@ use Throwable;
* 事件调度器
* Class EventDispatcher
*/
class EventDispatcher implements ListenerProviderInterface
class EventDispatcher implements ListenerProviderInterface,EventDispatcherInterface
{
/**
* @var EventListenerInterface
@ -33,15 +33,15 @@ class EventDispatcher implements ListenerProviderInterface
/**
* 检出当前事件的所有监听器
*
* @param string $event
* @param object $event
* An event for which to return the relevant listeners.
* @return iterable[callable]
* An iterable (array, iterator, or generator) of callables. Each
* callable MUST be type-compatible with $event.
*/
public function getListenersForEvent(string $event): iterable
public function getListenersForEvent(object $event): iterable
{
$class = $event;
$class = get_class($event);
$listeners = $this->eventListeners[$class] ?? [];
$iterable = [];
foreach ($listeners as $listener) {
@ -53,11 +53,11 @@ class EventDispatcher implements ListenerProviderInterface
/**
* 派发当前事件到所有监听器的process处理方法
*
* @param string $event 当前事件对象
* @param object $event 当前事件对象
* @return string
* The Event that was passed, now modified by listeners.
*/
public function dispatch(string $event)
public function dispatch(object $event)
{
foreach ($this->getListenersForEvent($event) as $callback) {
try {

View File

@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace app\common\event;
/**
* Defines a dispatcher for events.
*/
interface EventDispatcherInterface
{
/**
* Provide all relevant listeners with an event to process.
*
* @param object $event
* The object to process.
*
* @return object
* The Event that was passed, now modified by listeners.
*/
public function dispatch(object $event);
}

View File

@ -1,6 +1,6 @@
<?php
declare(strict_types=1);
namespace IYUU\Library\Event;
namespace app\common\event;
/**
* 事件监听器接口
@ -16,7 +16,7 @@ interface EventListenerInterface
/**
* 处理事件
* @param string $event
* @param object $event
*/
public function process(string $event): void;
public function process(object $event): void;
}

View File

@ -1,6 +1,6 @@
<?php
declare(strict_types=1);
namespace IYUU\Library\Event;
namespace app\common\event;
/**
* Mapper from an event to the listeners that are applicable to that event.
@ -8,11 +8,11 @@ namespace IYUU\Library\Event;
interface ListenerProviderInterface
{
/**
* @param string $event
* @param object $event
* An event for which to return the relevant listeners.
* @return iterable[callable]
* An iterable (array, iterator, or generator) of callables. Each
* callable MUST be type-compatible with $event.
*/
public function getListenersForEvent(string $event) : iterable;
public function getListenersForEvent(object $event) : iterable;
}

View File

@ -1,6 +1,6 @@
<?php
declare(strict_types=1);
namespace IYUU\Library\Event;
namespace app\common\event;
/**
* An Event whose processing may be interrupted when the event has been handled.

View File

@ -1,7 +1,7 @@
<?php
namespace IYUU\Library\EventListen;
use IYUU\Library\Event\EventListenerInterface;
use app\common\event\EventListenerInterface;
class send implements EventListenerInterface
{

View File

@ -168,6 +168,8 @@ abstract class AbstractRss
//$this->curl->setOpt(CURLOPT_SSL_VERIFYHOST, 2); // 检查证书
$this->curl->setOpt(CURLOPT_CONNECTTIMEOUT, self::CONNECTTIMEOUT); // 超时
$this->curl->setOpt(CURLOPT_TIMEOUT, self::TIMEOUT); // 超时
$this->curl->setOpt(CURLOPT_FOLLOWLOCATION, 1); // 自动跳转跟随请求Location
$this->curl->setOpt(CURLOPT_MAXREDIRS, 2); // 递归次数
$this->curl->setUserAgent(static::getUserAgent());
}
@ -290,6 +292,7 @@ abstract class AbstractRss
echo $this->site." 正在请求RSS... {$url}". PHP_EOL;
$url = (stripos($url, 'http://') === 0 || stripos($url, 'https://') === 0) ? $url : $this->host . $url;
$res = $this->curl->get($url);
//cli($res);exit;
if ($res->http_status_code == 200) {
echo "RSS获取信息成功". PHP_EOL;
return $res->response;