103 |
- |
1 |
<?php
|
|
|
2 |
namespace GuzzleHttp\Handler;
|
|
|
3 |
|
|
|
4 |
use GuzzleHttp\Psr7;
|
|
|
5 |
use Psr\Http\Message\RequestInterface;
|
|
|
6 |
|
|
|
7 |
/**
|
|
|
8 |
* HTTP handler that uses cURL easy handles as a transport layer.
|
|
|
9 |
*
|
|
|
10 |
* When using the CurlHandler, custom curl options can be specified as an
|
|
|
11 |
* associative array of curl option constants mapping to values in the
|
|
|
12 |
* **curl** key of the "client" key of the request.
|
|
|
13 |
*/
|
|
|
14 |
class CurlHandler
|
|
|
15 |
{
|
|
|
16 |
/** @var CurlFactoryInterface */
|
|
|
17 |
private $factory;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Accepts an associative array of options:
|
|
|
21 |
*
|
|
|
22 |
* - factory: Optional curl factory used to create cURL handles.
|
|
|
23 |
*
|
|
|
24 |
* @param array $options Array of options to use with the handler
|
|
|
25 |
*/
|
|
|
26 |
public function __construct(array $options = [])
|
|
|
27 |
{
|
|
|
28 |
$this->factory = isset($options['handle_factory'])
|
|
|
29 |
? $options['handle_factory']
|
|
|
30 |
: new CurlFactory(3);
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
public function __invoke(RequestInterface $request, array $options)
|
|
|
34 |
{
|
|
|
35 |
if (isset($options['delay'])) {
|
|
|
36 |
usleep($options['delay'] * 1000);
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
$easy = $this->factory->create($request, $options);
|
|
|
40 |
|
|
|
41 |
/////////////////////////////////// StartCustom code by seo panel////////////////////////
|
|
|
42 |
// add proxy details to handle. Custom code added by seo panel team
|
|
|
43 |
list($easy->handle, $proxyId) = \ProxyController::addProxyToCurlHandle($easy->handle, SP_ENABLE_PROXY_GOOGLE_API);
|
|
|
44 |
|
|
|
45 |
// Custom code added by seo panel team
|
|
|
46 |
$ret['page'] = curl_exec( $easy->handle );
|
|
|
47 |
$ret['error'] = curl_errno( $easy->handle );
|
|
|
48 |
$ret['errmsg'] = curl_error( $easy->handle );
|
|
|
49 |
$easy->errno = $ret['error'];
|
|
|
50 |
|
|
|
51 |
// update crawl log in database for future reference
|
|
|
52 |
$effectiveUrl = curl_getinfo($easy->handle, CURLINFO_EFFECTIVE_URL);
|
|
|
53 |
$effectiveUrl = preg_replace('/&key=(.*)/', '&key=XXX', $effectiveUrl);
|
|
|
54 |
$crawlLogCtrl = new \CrawlLogController();
|
|
|
55 |
$crawlInfo['crawl_status'] = $ret['error'] ? 0 : 1;
|
|
|
56 |
$crawlInfo['crawl_link'] = $effectiveUrl;
|
|
|
57 |
$crawlInfo['ref_id'] = $crawlInfo['crawl_link'];
|
|
|
58 |
$crawlInfo['crawl_referer'] = $crawlInfo['crawl_link'];
|
|
|
59 |
$crawlInfo['proxy_id'] = intval($proxyInfo['id']);
|
|
|
60 |
$crawlInfo['log_message'] = addslashes($ret['errmsg']);
|
|
|
61 |
$ret['log_id'] = $crawlLogCtrl->createCrawlLog($crawlInfo);
|
|
|
62 |
|
|
|
63 |
// save proxy status according to the results
|
|
|
64 |
\ProxyController::processProxyStatus($ret, $proxyId);
|
|
|
65 |
|
|
|
66 |
//curl_exec($easy->handle);
|
|
|
67 |
//$easy->errno = curl_errno($easy->handle);
|
|
|
68 |
///////////////////////////////////End Custom code by seo panel////////////////////////
|
|
|
69 |
|
|
|
70 |
|
|
|
71 |
return CurlFactory::finish($this, $easy, $this->factory);
|
|
|
72 |
}
|
|
|
73 |
}
|