Completed
Push — develop ( f810d8...9feba0 )
by Lars
06:45
created

CurlHttpAdapter::getContent()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 4.0119

Importance

Changes 0
Metric Value
cc 4
eloc 22
nc 4
nop 2
dl 0
loc 36
ccs 20
cts 22
cp 0.9091
crap 4.0119
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * PHP Domain Parser: Public Suffix List based URL parsing.
5
 *
6
 * @link      http://github.com/jeremykendall/php-domain-parser for the canonical source repository
7
 *
8
 * @copyright Copyright (c) 2014 Jeremy Kendall (http://about.me/jeremykendall)
9
 * @license   http://github.com/jeremykendall/php-domain-parser/blob/master/LICENSE MIT License
10
 */
11
namespace Pdp\HttpAdapter;
12
13
use Pdp\Exception\CurlHttpAdapterException;
14
15
/**
16
 * cURL http adapter.
17
 *
18
 * Lifted pretty much completely from William Durand's excellent Geocoder
19
 * project
20
 *
21
 * @link   https://github.com/willdurand/Geocoder Geocoder on GitHub
22
 *
23
 * @author William Durand <[email protected]>
24
 * @author Jeremy Kendall <[email protected]>
25
 */
26
class CurlHttpAdapter implements HttpAdapterInterface
27
{
28
  /**
29
   * {@inheritdoc}
30
   */
31 4
  public function getContent($url, $timeout = 5)
32
  {
33
    // init
34 4
    $ch = curl_init();
35
36 4
    if (false === $ch) {
37
      throw new \Exception('PHP-Domain-Parser: failed to initialize : cURL');
38
    }
39
40 4
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
41 4
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
42 4
    curl_setopt($ch, CURLOPT_URL, $url);
43 4
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
44 4
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
45 4
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
46 4
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
47 4
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
48
49 4
    curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-Domain-Parser cURL Request');
50
51 4
    $content = curl_exec($ch);
52
53 4
    $errNo = curl_errno($ch);
54 4
    if ($errNo) {
55 3
      throw new CurlHttpAdapterException("CURL error [{$errNo}]: " . curl_error($ch), $errNo);
56
    }
57
58 1
    $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
59 1
    if ($responseCode !== 200) {
60
      throw new CurlHttpAdapterException('Wrong HTTP response code: ' . $responseCode, $responseCode);
61
    }
62
63 1
    curl_close($ch);
64
65 1
    return $content;
66
  }
67
}
68