1 | <?php |
||
24 | class CurlClient implements ClientInterface |
||
25 | { |
||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | */ |
||
29 | public function makeCall( |
||
30 | $url, |
||
31 | array $headers = array(), |
||
32 | array $options = array(), |
||
33 | $method = 'GET', |
||
34 | $content = null |
||
35 | ) { |
||
36 | $curlOptions = array( |
||
37 | CURLOPT_URL => $url, |
||
38 | CURLOPT_HTTPHEADER => $headers, |
||
39 | CURLOPT_FOLLOWLOCATION => true, |
||
40 | CURLOPT_HEADER => true, |
||
41 | CURLOPT_TIMEOUT => 30, |
||
42 | CURLOPT_USERAGENT => ContentApiSdk::USERAGENT, |
||
43 | CURLOPT_RETURNTRANSFER => true, |
||
44 | ); |
||
45 | |||
46 | if (strtoupper($method) == 'POST') { |
||
47 | $curlOptions[CURLOPT_POST] = true; |
||
48 | $curlOptions[CURLOPT_POSTFIELDS] = $content; |
||
49 | } |
||
50 | |||
51 | $curlHandler = curl_init(); |
||
52 | if (!$curlHandler) { |
||
53 | throw new ClientException('Could not instantiate cURL.'); |
||
54 | } |
||
55 | |||
56 | curl_setopt_array($curlHandler, $curlOptions); |
||
57 | $response = curl_exec($curlHandler); |
||
58 | |||
59 | if ($response === false) { |
||
60 | throw new ClientException(sprintf('%s (%s)', 'A cURL error occured.', curl_error($curlHandler)), curl_errno($curlHandler)); |
||
61 | } |
||
62 | |||
63 | $header_size = curl_getinfo($curlHandler, CURLINFO_HEADER_SIZE); |
||
64 | |||
65 | $responseArray = array( |
||
66 | 'headers' => $this->parse_http_headers(mb_substr($response, 0, $header_size)), |
||
67 | 'status' => curl_getinfo($curlHandler, CURLINFO_HTTP_CODE), |
||
68 | 'body' => (mb_strlen($response) > $header_size) ? mb_substr($response, $header_size) : '' |
||
69 | ); |
||
70 | |||
71 | curl_close($curlHandler); |
||
72 | |||
73 | return $responseArray; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Parse header string from Curl request and returns an array. Based on: |
||
78 | * http://php.net/manual/en/function.http-parse-headers.php#112986 |
||
79 | * http://php.net/manual/en/function.http-parse-headers.php#112987 |
||
80 | * |
||
81 | * @param string $headerString |
||
82 | * |
||
83 | * @return array |
||
84 | */ |
||
85 | private function parse_http_headers($headerString) |
||
114 | } |
||
115 |